Class: LcApi::Resource

Inherits:
Object
  • Object
show all
Extended by:
ErrorCodes
Defined in:
lib/lc-api/resource.rb

Direct Known Subclasses

Category, Location, Message, Series, Speaker, Staff

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.

Raises:

  • (Error)


51
52
53
54
55
56
# File 'lib/lc-api/resource.rb', line 51

def initialize attributes = {}
  # raise error if user attempts to initialize a Resource by calling new
  raise Error, "#{self.class} is an abstract class and cannot be instantiated" if instance_of? Resource
  @attributes = {}
  self.attributes = attributes # "attributes=" called
end

Instance Attribute Details

#attributesObject

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 # get name of resource
  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 # ie 'message'
  uri += "/#{id}" if id
  parse_response(API.get(uri, options))
end

.member_nameObject



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) # from ErrorCodes mixin
  
  # build a single record and return if there is only one resource (via ".find" method)
  return build_record(response.parsed_response) unless multiple
  
  # otherwise, build multiple records for multiple resources (via ".all" method)
  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) # "self[]" called externally, like so: "message.title" -- getter for keys and values (self[])
  @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) # called internally -- setter for keys and values
  @attributes[k] = v if self.respond_to?(k)
end