Class: JSON_ROA::Client::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/json_roa/client/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, response = nil) ⇒ Resource

Returns a new instance of Resource.



12
13
14
15
# File 'lib/json_roa/client/resource.rb', line 12

def initialize(conn, response = nil)
  @conn = conn
  @response = response || conn.get
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/json_roa/client/resource.rb', line 9

def conn
  @conn
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/json_roa/client/resource.rb', line 10

def response
  @response
end

Instance Method Details

#collectionObject



39
40
41
# File 'lib/json_roa/client/resource.rb', line 39

def collection
  ::JSON_ROA::Client::Collection.new @conn, self
end

#dataObject



27
28
29
30
31
# File 'lib/json_roa/client/resource.rb', line 27

def data
  @response.body.instance_eval do
    self.with_indifferent_access rescue self
  end
end

#json_roa_dataObject



33
34
35
36
37
# File 'lib/json_roa/client/resource.rb', line 33

def json_roa_data
  @response.env.json_roa_data.instance_eval do
    self.with_indifferent_access rescue self
  end
end

#relation(key) ⇒ Object



17
18
19
20
# File 'lib/json_roa/client/resource.rb', line 17

def relation(key)
  relhash = json_roa_data['relations'][key]
  ::JSON_ROA::Client::Relation.new @conn, key, relhash
end

#self_relationObject



22
23
24
25
# File 'lib/json_roa/client/resource.rb', line 22

def self_relation
  ::JSON_ROA::Client::Relation.new( \
    @conn, 'self', json_roa_data['self-relation']) rescue nil
end

#to_sObject



43
44
45
# File 'lib/json_roa/client/resource.rb', line 43

def to_s
  "#{self.class.name}: #{data} #{json_roa_data}"
end