Class: Jirafe::Resource::JirafeResource

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Model, Jirafe::ResponseParser
Defined in:
lib/jirafe/resource/jirafe_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#attributes, #attributes_for_change, #create, included, #initialize, #reinitialize, #update

Methods included from Jirafe::ResponseParser

included

Class Method Details

.create(resource) ⇒ Object



54
55
56
57
58
# File 'lib/jirafe/resource/jirafe_resource.rb', line 54

def create(resource)
  resource_id, parent_identifiers = identifiers_from_resource(resource)
  response = post(resource_url(resource_id, *parent_identifiers), options_with_token({:body => resource.attributes_for_change(:create)}))
  handle_response(resource, response)
end

.list(*parent_identifiers) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/jirafe/resource/jirafe_resource.rb', line 35

def list(*parent_identifiers)
  if parent_identifiers.last.is_a?(Hash)
    options = {:query => parent_identifiers.pop }
  end
  response = get(resource_url(nil, *parent_identifiers), options_with_token(options || {}))
  check_response_for_exception(response)
  response.parsed_response.map do |item|
    self.new(item)
  end
end

.parent(parent = nil) ⇒ Object



21
22
23
24
25
# File 'lib/jirafe/resource/jirafe_resource.rb', line 21

def parent(parent = nil)
  return nil if self == Jirafe::Resource::JirafeResource
  @parent ||= parent
  @parent || Jirafe::Resource::JirafeResource
end

.parent_path(*parent_identifiers) ⇒ Object



27
28
29
# File 'lib/jirafe/resource/jirafe_resource.rb', line 27

def parent_path(*parent_identifiers)
  parent.nil? ? nil : parent.resource_url(*parent_identifiers)
end

.path(path = nil) ⇒ Object Also known as: url



14
15
16
17
18
# File 'lib/jirafe/resource/jirafe_resource.rb', line 14

def path(path = nil)
  return Jirafe.config.url unless superclass == JirafeResource
  @path ||= path
  @path || raise(Jirafe::Error::MissingResourcePath)
end

.resource_url(identifier = nil, *parent_identifiers) ⇒ Object



31
32
33
# File 'lib/jirafe/resource/jirafe_resource.rb', line 31

def resource_url(identifier = nil, *parent_identifiers)
  [parent_path(*parent_identifiers), path, identifier].compact.join("/")
end

.retrieve(identifier = nil, *parent_identifiers) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/jirafe/resource/jirafe_resource.rb', line 46

def retrieve(identifier = nil, *parent_identifiers)
  if parent_identifiers.last.is_a?(Hash)
    options = {:query => parent_identifiers.pop }
  end
  response = get(resource_url(identifier, *parent_identifiers), options_with_token(options || {}))
  handle_response(self.new, response)
end

.update(resource) ⇒ Object



60
61
62
63
64
# File 'lib/jirafe/resource/jirafe_resource.rb', line 60

def update(resource)
  resource_id, parent_identifiers = identifiers_from_resource(resource)
  response = put(resource_url(resource_id, parent_identifiers), options_with_token({:body => resource.attributes_for_change(:update)}))
  handle_response(resource, response)
end

Instance Method Details

#identifierObject



103
104
# File 'lib/jirafe/resource/jirafe_resource.rb', line 103

def identifier
end

#parentObject



87
88
89
90
91
92
93
# File 'lib/jirafe/resource/jirafe_resource.rb', line 87

def parent
  if self.class.parent == Jirafe::Resource::JirafeResource
    self.class.parent
  else
    @parent
  end
end

#parent=(parent_instance) ⇒ Object



95
96
97
# File 'lib/jirafe/resource/jirafe_resource.rb', line 95

def parent=(parent_instance)
  @parent = parent_instance
end

#parent_resource_urlObject



99
100
101
# File 'lib/jirafe/resource/jirafe_resource.rb', line 99

def parent_resource_url
  parent.nil? ? nil : parent.resource_url
end

#resource_urlObject



106
107
108
# File 'lib/jirafe/resource/jirafe_resource.rb', line 106

def resource_url
  [parent_resource_url, self.class.path, self.identifier].compact.join("/")
end