Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3.9"
bundler-cache: true
- run: bundle exec rake
- run: bundle exec ruby -Ilib -e 'require "hive_api"; HiveAPI::Client; HiveAPI::Base; HiveAPI::Resource; HiveAPI::Error; HiveAPI::APIError; abort "Rails loaded" if defined?(Rails); abort "Zeitwerk loaded" if defined?(Zeitwerk)'
- run: gem build hive_api.gemspec
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.gem
Gemfile.lock
.env
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--require spec_helper
--format documentation
1 change: 1 addition & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby_version: 3.3
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.3.9
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

## [Unreleased]

### Added

- Rails-independent Hive API client foundation with fixed production and staging environments, defaulting safely to staging.
- Bounded Faraday connections with injectable adapters and safe JSON middleware.
- Shared resource and response-object foundations.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2026 PostCo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# hive_api
# HiveAPI

Rails-independent Ruby client foundation for the [Hive Merchant API](https://developers.hive.app/).

## Installation

Add the gem to your Gemfile:

```ruby
gem "hive_api", "~> 0.1.0"
```

Then run `bundle install`.

## Usage

Require the gem and build a client. It defaults to Hive's staging API:

```ruby
require "hive_api"

client = HiveAPI::Client.new

# Use the production API explicitly:
HiveAPI::Client.new(sandbox: false)
```

The client does not accept a custom base URL and it does not use global configuration. Connections use bounded connect and request timeouts, defaulting to 5 and 15 seconds respectively:

```ruby
HiveAPI::Client.new(
sandbox: true,
open_timeout: 2,
timeout: 10
)
```

Authentication and concrete Hive resources will be added in subsequent releases.

### Response objects

Response objects expose provider keys as snake-case Ruby methods, including nested hashes and arrays. The original provider payload remains available as a deeply frozen snapshot through `raw`, while `to_h` returns plain recursive hashes and arrays.

```ruby
response = HiveAPI::Base.new(
"returnId" => "return-123",
"lineItems" => [{"merchantSKU" => "SKU-1"}]
)

response.return_id # => "return-123"
response.line_items.first.merchant_sku # => "SKU-1"
response.raw.frozen? # => true
response.to_h # => {"return_id" => "return-123", ...}
```

## Development

```sh
bundle install
bundle exec rake
gem build hive_api.gemspec
```

## License

MIT License. See [LICENSE.txt](LICENSE.txt).
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "standard/rake"

RSpec::Core::RakeTask.new(:spec)

task default: %i[spec standard]
task release: %i[spec standard]
32 changes: 32 additions & 0 deletions hive_api.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative "lib/hive_api/version"

Gem::Specification.new do |spec|
spec.name = "hive_api"
spec.version = HiveAPI::VERSION
spec.authors = ["PostCo"]
spec.email = ["engineering@postco.co"]

spec.summary = "Rails-independent Ruby client for the Hive Merchant API"
spec.description = "A small Ruby client foundation for integrating with Hive without depending on Rails."
spec.homepage = "https://github.com/PostCo/hive_api"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.3.0"

spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
spec.metadata["rubygems_mfa_required"] = "true"

spec.files = Dir["lib/**/*", "CHANGELOG.md", "README.md", "LICENSE.txt"]
spec.require_paths = ["lib"]

spec.add_dependency "faraday", "~> 2.0"
spec.add_dependency "faraday-net_http", ">= 2.0"
spec.add_dependency "activesupport", ">= 7.0"

spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 3.0"
spec.add_development_dependency "standard"
end
14 changes: 14 additions & 0 deletions lib/hive_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative "hive_api/version"

module HiveAPI
autoload :Client, "hive_api/client"
autoload :Base, "hive_api/object"
autoload :Resource, "hive_api/resource"
autoload :Error, "hive_api/errors"
autoload :APIError, "hive_api/errors"

module Objects
end
end
40 changes: 40 additions & 0 deletions lib/hive_api/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require "faraday"

module HiveAPI
class Client
LIVE_BASE_URL = "https://app.hive.app/merchant_api/v2/"
TEST_BASE_URL = "https://staging.app.hive.app/merchant_api/v2/"
DEFAULT_OPEN_TIMEOUT = 5
DEFAULT_TIMEOUT = 15

attr_reader :adapter, :open_timeout, :timeout

def initialize(sandbox: true, adapter: Faraday.default_adapter,
open_timeout: DEFAULT_OPEN_TIMEOUT, timeout: DEFAULT_TIMEOUT)
@sandbox = sandbox
@adapter = adapter
@open_timeout = open_timeout
@timeout = timeout
end

def connection
@connection ||= Faraday.new do |connection|
connection.url_prefix = sandbox? ? TEST_BASE_URL : LIVE_BASE_URL
connection.options.open_timeout = open_timeout
connection.options.timeout = timeout
connection.headers["Accept"] = "application/json"
connection.request :json
connection.response :json, content_type: /\bjson/
connection.adapter adapter
end
end

private

def sandbox?
@sandbox
end
end
end
7 changes: 7 additions & 0 deletions lib/hive_api/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module HiveAPI
class Error < StandardError; end

class APIError < Error; end
end
71 changes: 71 additions & 0 deletions lib/hive_api/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require "ostruct"
require "active_support/core_ext/string/inflections"

module HiveAPI
class Base < OpenStruct
attr_reader :original_response

def self.new(attributes)
return attributes.map { |item| super(item) } if attributes.is_a?(Array)

super
end

def initialize(attributes)
@original_response = deep_freeze(attributes)
super(to_ostruct(attributes))
end

def to_hash
ostruct_to_hash(self)
end

alias_method :to_h, :to_hash

def raw
@original_response
end

private

def to_ostruct(object)
case object
when Hash
OpenStruct.new(
object.transform_keys { |key| key.to_s.underscore }
.transform_values { |value| to_ostruct(value) }
)
when Array
object.map { |value| to_ostruct(value) }
else
object
end
end

def deep_freeze(object)
case object
when Hash then object.transform_values { |value| deep_freeze(value) }.freeze
when Array then object.map { |item| deep_freeze(item) }.freeze
else object.respond_to?(:freeze) ? object.freeze : object
end
end

def ostruct_to_hash(object)
case object
when OpenStruct
object.each_pair.to_h
.transform_keys(&:to_s)
.transform_values { |value| ostruct_to_hash(value) }
when Array
object.map { |value| ostruct_to_hash(value) }
when Hash
object.transform_keys(&:to_s)
.transform_values { |value| ostruct_to_hash(value) }
else
object
end
end
end
end
35 changes: 35 additions & 0 deletions lib/hive_api/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "json"

module HiveAPI
class Resource
attr_reader :client

def initialize(client)
@client = client
end

private

def get_request(path, params: {}, headers: {})
handle_response(client.connection.get(path, params, headers))
end

def post_request(path, body:, headers: {})
handle_response(client.connection.post(path, body, headers))
end

def handle_response(response)
parse_body(response.body)
end

def parse_body(body)
return body unless body.is_a?(String)

JSON.parse(body)
rescue JSON::ParserError
body
end
end
end
5 changes: 5 additions & 0 deletions lib/hive_api/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module HiveAPI
VERSION = "0.1.0"
end
29 changes: 29 additions & 0 deletions spec/gemspec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

RSpec.describe "hive_api.gemspec" do
subject(:specification) do
Gem::Specification.load(File.expand_path("../hive_api.gemspec", __dir__))
end

it "requires compatible Ruby and the PostCo runtime dependencies" do
expect(specification.required_ruby_version).to be_satisfied_by(Gem::Version.new("3.3.0"))
expect(specification.runtime_dependencies.map(&:name).sort)
.to eq(%w[activesupport faraday faraday-net_http])
end

it "uses the PostCo development toolchain" do
expect(specification.development_dependencies.map(&:name).sort)
.to eq(%w[rake rspec standard webmock])
end

it "packages library files and project documentation" do
expect(specification.files).to include(
"lib/hive_api.rb",
"lib/hive_api/client.rb",
"README.md",
"LICENSE.txt",
"CHANGELOG.md"
)
expect(specification.files).not_to include(a_string_starting_with("spec/"))
end
end
Loading
Loading