Class: LcApi::Resource
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ErrorCodes
error_code_check
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
51
52
53
54
55
56
|
# File 'lib/lc-api/resource.rb', line 51
def initialize attributes = {}
raise Error, "#{self.class} is an abstract class and cannot be instantiated" if instance_of? Resource
@attributes = {}
self.attributes = attributes end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
49
50
51
|
# File 'lib/lc-api/resource.rb', line 49
def attributes
@attributes
end
|
Class Method Details
.all(options = {}) ⇒ Object
16
17
18
19
|
# File 'lib/lc-api/resource.rb', line 16
def all(options={})
uri = member_name parse_response(API.get(uri, options), true)
end
|
.build_record(response) ⇒ Object
33
34
35
36
37
|
# File 'lib/lc-api/resource.rb', line 33
def build_record(response)
attributes = {}
response.each_pair { |k,v| attributes[k] = v if @attributes.include? k }
new(attributes)
end
|
.define_attribute_methods(attributes) ⇒ Object
43
44
45
46
|
# File 'lib/lc-api/resource.rb', line 43
def define_attribute_methods(attributes)
@attributes = attributes
@attributes.each { |name| define_method(name) { self[name] }}
end
|
.find(id, options = {}) ⇒ Object
10
11
12
13
14
|
# File 'lib/lc-api/resource.rb', line 10
def find(id, options={})
uri = member_name uri += "/#{id}" if id
parse_response(API.get(uri, options))
end
|
.member_name ⇒ Object
39
40
41
|
# File 'lib/lc-api/resource.rb', line 39
def member_name
name.split('::').last.downcase.pluralize
end
|
.parse_response(response, multiple = false) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/lc-api/resource.rb', line 21
def parse_response(response, multiple=false)
error_code_check(response)
return build_record(response.parsed_response) unless multiple
resources = []
response.parsed_response.each { |rec| resources.push build_record(rec) }
return resources
end
|
Instance Method Details
#[](k) ⇒ Object
“self[]” called externally, like so: “message.title” – getter for keys and values (self[])
58
59
60
|
# File 'lib/lc-api/resource.rb', line 58
def [](k) @attributes[k]
end
|
#[]=(k, v) ⇒ Object
called internally – setter for keys and values
62
63
64
|
# File 'lib/lc-api/resource.rb', line 62
def []=(k,v) @attributes[k] = v if self.respond_to?(k)
end
|