Class: Textmagic::REST::InstanceResource

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/textmagic-ruby/rest/instance_resource.rb

Instance Method Summary collapse

Methods included from Utils

#key_map, #resource, #to_camel_case, #to_underscore_case

Constructor Details

#initialize(path, client, params = {}) ⇒ InstanceResource

Returns a new instance of InstanceResource.



6
7
8
9
10
# File 'lib/textmagic-ruby/rest/instance_resource.rb', line 6

def initialize(path, client, params={})
  @path = path
  @client = client
  load_attributes params
end

Instance Method Details

#inspectObject

:nodoc:



12
13
14
# File 'lib/textmagic-ruby/rest/instance_resource.rb', line 12

def inspect # :nodoc:
  "<#{self.class} @path=#{@path}>"
end

#load_attributes(hash) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/textmagic-ruby/rest/instance_resource.rb', line 16

def load_attributes(hash)
  metaclass = class << self; self; end
  hash.each do |k,v|
    attr = to_underscore_case k
    unless ['client', 'updated'].include? attr
      metaclass.send :define_method, attr.to_sym, &lambda {v}
    end
  end
  @updated = !hash.keys.empty?
end

#refreshObject



27
28
29
30
# File 'lib/textmagic-ruby/rest/instance_resource.rb', line 27

def refresh
  load_attributes(@client.get("#{@path}/#{self.id}", {}))
  self
end