Class: Chord::Base
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/chord.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id, attributes = {}) ⇒ Base
Returns a new instance of Base.
122
123
124
125
|
# File 'lib/chord.rb', line 122
def initialize(id, attributes = {})
@id = id
@attributes = attributes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/chord.rb', line 148
def method_missing(method, *args, &block)
if attributes.include?(method.to_s)
attributes[method.to_s]
else
super
end
end
|
Class Attribute Details
.per_page ⇒ Object
45
|
# File 'lib/chord.rb', line 45
def per_page; 99999; end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
120
121
122
|
# File 'lib/chord.rb', line 120
def attributes
@attributes
end
|
#id ⇒ Object
Returns the value of attribute id.
119
120
121
|
# File 'lib/chord.rb', line 119
def id
@id
end
|
Class Method Details
.all ⇒ Object
47
48
49
50
|
# File 'lib/chord.rb', line 47
def all
check_for_config!
@all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) }
end
|
.base_url ⇒ Object
64
65
66
|
# File 'lib/chord.rb', line 64
def base_url
Chord.base_url
end
|
.find(id) ⇒ Object
57
58
59
60
61
62
|
# File 'lib/chord.rb', line 57
def find(id)
check_for_config!
return nil if id.nil? or id == ''
attrs = fetch_attributes(id)
attrs.include?('error') ? nil : new(attrs[id_attribute], attrs)
end
|
.http_options ⇒ Object
68
69
70
71
72
73
|
# File 'lib/chord.rb', line 68
def http_options
{headers: {
'Authorization' => "Bearer #{Chord.api_key}",
'Content-Type' => 'application/json'
}}
end
|
.where(query_options = {}) ⇒ Object
52
53
54
55
|
# File 'lib/chord.rb', line 52
def where(query_options = {})
check_for_config!
fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) }
end
|
Instance Method Details
#base_path ⇒ Object
111
112
113
|
# File 'lib/chord.rb', line 111
def base_path
self.class.base_path
end
|
#base_url ⇒ Object
107
108
109
|
# File 'lib/chord.rb', line 107
def base_url
self.class.base_url
end
|
#delete ⇒ Object
138
139
140
|
# File 'lib/chord.rb', line 138
def delete
self.class.delete(base_url + "#{base_path}/#{id}", http_options).parsed_response
end
|
#expand! ⇒ Object
fetch all attributes, but don’t overwrite existing ones, in case changes have been made
144
145
146
|
# File 'lib/chord.rb', line 144
def expand!
@attributes = self.class.send(:fetch_attributes, id)
end
|
#http_options ⇒ Object
115
116
117
|
# File 'lib/chord.rb', line 115
def http_options
self.class.http_options
end
|
#update(new_attributes) ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/chord.rb', line 127
def update(new_attributes)
response = self.class.patch(base_url + "#{base_path}/#{id}",
http_options.merge(body: new_attributes.to_json)
).parsed_response
if response.include?('error')
raise APIError, "Chord API error (status #{response['status']}): #{response['error']}"
else
@attributes = response
end
end
|