Class: LMRest::Resource
- Inherits:
-
Object
- Object
- LMRest::Resource
- Defined in:
- lib/lm_rest/resource.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(properties) ⇒ Resource
constructor
A new instance of Resource.
- #to_h ⇒ Object
Constructor Details
#initialize(properties) ⇒ Resource
Returns a new instance of Resource.
4 5 6 7 8 9 10 11 12 |
# File 'lib/lm_rest/resource.rb', line 4 def initialize(properties) properties.each do |key, value| instance_variable_set(:"@#{key}", value) define_singleton_method(key.intern) { instance_variable_get("@#{key}") } define_singleton_method("#{key}=".intern) do |new_value| instance_variable_set("@#{key}", new_value) end end end |
Class Method Details
.parse(body) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lm_rest/resource.rb', line 22 def parse(body) begin if body.key? 'items' parse_collection(body['items']) else parse_object(body) end rescue => e puts e puts "Response body: " + body end end |
.parse_collection(items) ⇒ Object
36 37 38 39 40 |
# File 'lib/lm_rest/resource.rb', line 36 def parse_collection(items) items.map do |item| new(item) end end |
.parse_object(item) ⇒ Object
42 43 44 |
# File 'lib/lm_rest/resource.rb', line 42 def parse_object(item) new(item) end |
Instance Method Details
#to_h ⇒ Object
14 15 16 17 18 |
# File 'lib/lm_rest/resource.rb', line 14 def to_h instance_variables.map do |var| [var[1..-1].to_s, instance_variable_get(var)] end.to_h end |