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.
52
53
54
55
56
57
|
# File 'lib/lc-api/resource.rb', line 52
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.
50
51
52
|
# File 'lib/lc-api/resource.rb', line 50
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
44
45
46
47
|
# File 'lib/lc-api/resource.rb', line 44
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
42
|
# File 'lib/lc-api/resource.rb', line 39
def member_name
ActiveSupport::Inflector.inflections { |inflect| inflect.irregular 'staff', 'staff' } 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[])
59
60
61
|
# File 'lib/lc-api/resource.rb', line 59
def [](k) @attributes[k]
end
|
#[]=(k, v) ⇒ Object
called internally – setter for keys and values
63
64
65
|
# File 'lib/lc-api/resource.rb', line 63
def []=(k,v) @attributes[k] = v if self.respond_to?(k)
end
|