Class: Pocus::Resource

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

Overview

Base class for API resources.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Resource

Returns a new instance of Resource.



39
40
41
42
43
44
# File 'lib/pocus/resource.rb', line 39

def initialize(attributes)
  assign_attributes(attributes)
  self.class.associations.each_pair do |name, options|
    send("#{name}=", Association.new(self, name, options))
  end
end

Class Attribute Details

.pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/pocus/resource.rb', line 19

def path
  @path
end

.primary_keyObject

Returns the value of attribute primary_key.



19
20
21
# File 'lib/pocus/resource.rb', line 19

def primary_key
  @primary_key
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/pocus/resource.rb', line 16

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/pocus/resource.rb', line 15

def path
  @path
end

Class Method Details

.associationsObject



21
22
23
# File 'lib/pocus/resource.rb', line 21

def associations
  @associations ||= {}
end

.has_many(name, options = {}) ⇒ Object



25
26
27
28
# File 'lib/pocus/resource.rb', line 25

def has_many(name, options = {})
  attr_accessor(name)
  associations[name] = options
end

.tagObject



30
31
32
# File 'lib/pocus/resource.rb', line 30

def tag
  name.split('::').last.downcase
end

.tag_multipleObject



34
35
36
# File 'lib/pocus/resource.rb', line 34

def tag_multiple
  tag.concat('s')
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



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

def assign_attributes(attributes)
  attributes.each do |ck, v|
    k = underscore(ck)
    self.class.__send__(:attr_accessor, k) unless respond_to?(k)
    send("#{k}=", v)
  end
end

#deleteObject Also known as: destroy



54
55
56
# File 'lib/pocus/resource.rb', line 54

def delete
  session.send_request('DELETE', path) == []
end

#fieldsObject



59
60
61
62
63
# File 'lib/pocus/resource.rb', line 59

def fields
  field_names.reduce(Hash[]) do |hash, field_name|
    hash.tap { |h| h[camelize field_name] = send field_name }
  end
end

#get(request_path, klass) ⇒ Object

Fetch and instantiate a single resource from a path.



66
67
68
69
70
71
72
# File 'lib/pocus/resource.rb', line 66

def get(request_path, klass)
  response = session.send_request('GET', path + request_path)
  data = response.fetch(klass.tag)
  resource = klass.new(data.merge(parent: self))
  resource.assign_errors(response)
  resource
end

#get_multiple(request_path, klass, filters = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/pocus/resource.rb', line 74

def get_multiple(request_path, klass, filters = {})
  request_data = URI.encode_www_form(camelize_fields filters)
  response = session.send_request('GET', path + request_path + '?' + request_data)
  data = response.fetch(klass.tag_multiple) || []
  resources = data.map { |fields| klass.new(fields.merge(parent: self)) }
  ResponseArray.new(resources, response['errors'], response['warnings'])
end

#idObject



82
83
84
# File 'lib/pocus/resource.rb', line 82

def id
  send self.class.primary_key
end

#loggerObject



86
87
88
# File 'lib/pocus/resource.rb', line 86

def logger
  session.logger
end

#marshal_dumpObject



125
126
127
# File 'lib/pocus/resource.rb', line 125

def marshal_dump
  instance_variables.each_with_object({}) { |k, h| h[k[1..-1]] = send(k[1..-1]) }
end

#marshal_load(attributes) ⇒ Object



129
130
131
# File 'lib/pocus/resource.rb', line 129

def marshal_load(attributes)
  assign_attributes(attributes)
end

#postObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pocus/resource.rb', line 94

def post
  response = session.send_request('POST', path, fields)
  assign_attributes(response.fetch(self.class.tag))
  assign_errors(response)
  self
rescue UnexpectedHttpResponse => e
  response = JSON.parse(e.response.body)
  response['errors'] || response['warnings'] or raise
  assign_errors(response)
  self
end

#post_multiple(request_path, klass, fields_multiple) ⇒ Object



106
107
108
109
110
111
# File 'lib/pocus/resource.rb', line 106

def post_multiple(request_path, klass, fields_multiple)
  data = fields_multiple.map { |fields| camelize_fields(fields) }
  response = session.send_request('POST', path + request_path, data)
  resources = response.fetch(klass.tag_multiple).map { |fields| klass.new(fields.merge(parent: self)) }
  ResponseArray.new(resources, response['errors'], response['warnings'])
end

#reloadObject

Fetch and update this resource from a path.



114
115
116
117
118
119
# File 'lib/pocus/resource.rb', line 114

def reload
  response = session.send_request('GET', path)
  assign_attributes(response.fetch(self.class.tag))
  assign_errors(response)
  self
end

#sessionObject



121
122
123
# File 'lib/pocus/resource.rb', line 121

def session
  parent.session
end