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
1 change: 1 addition & 0 deletions lib/ruber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Ruber
autoload :Client, "ruber/client"
autoload :Error, "ruber/error"
autoload :Authenticator, "ruber/authenticator"
autoload :Object, "ruber/object"

DEFAULT_API_BASE = "https://api.uber.com/v1"

Expand Down
35 changes: 35 additions & 0 deletions lib/ruber/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Ruber
class Object
def initialize(attributes)
@data = to_data_object(attributes)
end

def to_data_object(obj)
if obj.is_a?(Hash)
wrapped = obj.transform_values do |val|
result = to_data_object(val)

result.is_a?(Data) ? self.class.new(result.to_h) : result
end

Data.define(*wrapped.keys).new(**wrapped)
elsif obj.is_a?(Array)
obj.map { |o| to_data_object(o) }
else
obj
end
end

private

def method_missing(name, *args)
super unless @data.respond_to?(name)

@data.public_send(name, *args)
end

def respond_to_missing?(name, *args) = @data.respond_to?(name) || super
end
end
36 changes: 36 additions & 0 deletions test/ruber/object_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require File.expand_path("../test_helper", __dir__)

module Ruber
class ObjectTest < Minitest::Test
def test_creating_object_from_hash
assert_equal "bar", Ruber::Object.new(foo: "bar").foo
end

def test_nested_hash
assert_equal "foobar", Ruber::Object.new(foo: { bar: { baz: "foobar" } }).foo.bar.baz
end

def test_nested_number
assert_equal 1, Ruber::Object.new(foo: { bar: 1 }).foo.bar
end

def test_array
object = Ruber::Object.new(foo: [{ bar: :baz }])
assert_equal :baz, object.foo.first.bar
end

def test_respond_to_missing
assert_respond_to Ruber::Object.new(foo: "bar"), :foo
end

def test_invalid_method
assert_raises(NoMethodError) { Ruber::Object.new(foo: "bar").invalid }
end

def test_respond_with_ruber_object
assert_instance_of Ruber::Object, Ruber::Object.new(foo: { bar: "baz" }).foo
end
end
end