Class: Blimp::Resource

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

Direct Known Subclasses

Comment, Company, File, Goal, Project, Task, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (Error)


80
81
82
83
84
85
# File 'lib/blimp/resource.rb', line 80

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

#attributesObject

Returns the value of attribute attributes.



78
79
80
# File 'lib/blimp/resource.rb', line 78

def attributes
  @attributes
end

Class Method Details

.build_record(response) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/blimp/resource.rb', line 37

def build_record(response)
  attributes = {}
  response.each_pair do |key, val|
    attributes[key] = val if @attributes.include? key
  end
  new(attributes)
end

.create(attributes) ⇒ Object



70
71
72
73
74
75
# File 'lib/blimp/resource.rb', line 70

def create attributes
  new(attributes)
  uri = "#{self.member_name}/"

  parse_response Blimp::API.post(uri, body: attributes.to_json)
end

.define_attribute_methods(attributes, read_only_attributes) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/blimp/resource.rb', line 50

def define_attribute_methods(attributes, read_only_attributes)
  common_attributes = %w[id date_created date_modified resource_uri]
  read_only_attributes = read_only_attributes | common_attributes
  @attributes = attributes | read_only_attributes

  @attributes.each do |name|
    define_method(name) { self[name] }
    define_method("#{name}?") {!self[name]}
    define_method("#{name}=") {|val| self[name] = val} unless read_only_attributes.include?(name)
  end
end

.define_readonly_attributes(attributes) ⇒ Object



62
63
64
65
66
67
# File 'lib/blimp/resource.rb', line 62

def define_readonly_attributes(attributes)
  attributes.each do |name|
    define_method(name) { self[name] }
    define_method("#{name}?") {!self[name]}
  end
end

.find(id, options = {}) ⇒ Object



7
8
9
10
11
# File 'lib/blimp/resource.rb', line 7

def find(id, options={})
  uri = member_name
  uri += "/#{id}" if id
  parse_response API.get(uri, options)
end

.find_all(options = {}) ⇒ Object



13
14
15
16
# File 'lib/blimp/resource.rb', line 13

def find_all(options={})
  uri = member_name
  parse_response API.get(uri, options), true
end

.member_nameObject



45
46
47
# File 'lib/blimp/resource.rb', line 45

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

.parse_response(response, multiple = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blimp/resource.rb', line 18

def parse_response(response, multiple=false)
  case response.code.to_i
  when 404
    raise Blimp::API::NotFound.new(response), "Resource was not found"
  when 403
    raise Blimp::API::Forbidden.new(response), "Forbidden"
  when 500
    raise Blimp::API::InternalServerError.new(response), "500 Internal Server error. Make sure you're sending a proper request."
  end

  return build_record(response.parsed_response) unless multiple

  resources = []
  response.parsed_response['objects'].each do |project|
    resources.push build_record(project)
  end
  return resources
end

Instance Method Details

#[](key) ⇒ Object



91
92
93
# File 'lib/blimp/resource.rb', line 91

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



87
88
89
# File 'lib/blimp/resource.rb', line 87

def []=(key,value)
  @attributes[key] = value if self.respond_to?(key)
end

#new?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/blimp/resource.rb', line 101

def new?
  self.id?
end

#saveObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/blimp/resource.rb', line 105

def save
  uri = "#{self.class.member_name}/"
  uri += "#{self.id}/" unless self.new?

  attributes = {}
  @attributes.each_pair do |key,val|
    next if self.class::READ_ONLY.include? key
    attributes[key] = val
  end
  method = self.new? ? 'post' : 'put'
  self.attributes = Blimp::API.send(method, uri, body: attributes.to_json).parsed_response
  return self
end