Skip to content

Commit

Permalink
Merge pull request #19 from inloco/fix/api_resource
Browse files Browse the repository at this point in the history
Fix/api resource
  • Loading branch information
ottony authored Nov 22, 2023
2 parents afcfeef + a4980b4 commit 8b46cbd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
incognia_api (0.5.2)
incognia_api (0.5.3)
faraday (~> 1.10)
faraday_middleware (~> 1.2)

Expand Down
4 changes: 2 additions & 2 deletions lib/incognia_api/resources/api_resource.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'delegate'

module Incognia
class APIResource < SimpleDelegator
class APIResource < OpenStruct
def self.from_hash(hash)
hash = hash.each_with_object({}) do |(k, v), h|
h[k] = v.is_a?(Hash) ? from_hash(v) : v
end

new(OpenStruct.new(hash))
new(hash)
end
end
end
2 changes: 1 addition & 1 deletion lib/incognia_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Incognia
VERSION = "0.5.2"
VERSION = "0.5.3"
end
14 changes: 10 additions & 4 deletions spec/resources/api_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
module Incognia
RSpec.describe APIResource do
context ".from_hash" do
subject { described_class.from_hash(foo: { bar: :val }, list: [:one, :two]) }
subject(:from_hash) { described_class.from_hash(foo: { bar: :val }, list: [:one, :two]) }

it "returns an instance of klass" do
expect(subject).to be_instance_of(described_class)
expect(from_hash).to be_instance_of(described_class)
end

it "builds a loose object from a hash" do
expect(subject.foo).to respond_to(:bar)
expect(from_hash.foo).to respond_to(:bar)
end

it "builds nested objects" do
expect(subject.foo.bar).to eql(:val)
expect(from_hash.foo.bar).to eql(:val)
end

context "when calling an inexistent attribute" do
it "returns nil" do
expect(from_hash.inexistent).to be_nil
end
end
end
end
Expand Down

0 comments on commit 8b46cbd

Please sign in to comment.