Class: Kiik::Resource
Instance Attribute Summary collapse
-
#created ⇒ Object
Returns the value of attribute created.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .build(data, error = nil) ⇒ Object
- .class_name ⇒ Object
- .opts(headers = {}) ⇒ Object
- .request(path = nil, params = {}, method = :GET, header = {}) ⇒ Object
- .url ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
- #to_json(attrs = [:id, :created]) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
61 62 63 64 |
# File 'lib/kiik/resource.rb', line 61 def initialize(attributes = {}) attributes.each{ |name, value| self.instance_variable_set("@#{name}", value) } self.errors = [] end |
Instance Attribute Details
#created ⇒ Object
Returns the value of attribute created.
5 6 7 |
# File 'lib/kiik/resource.rb', line 5 def created @created end |
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/kiik/resource.rb', line 5 def errors @errors end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/kiik/resource.rb', line 5 def id @id end |
Class Method Details
.build(data, error = nil) ⇒ Object
27 28 29 30 31 |
# File 'lib/kiik/resource.rb', line 27 def build(data, error = nil) instance = self.new(data) instance.errors = error.errors unless error.nil? instance end |
.class_name ⇒ Object
9 10 11 |
# File 'lib/kiik/resource.rb', line 9 def class_name self.name.split('::')[-1] end |
.opts(headers = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/kiik/resource.rb', line 17 def opts(headers={}) { basic_auth: {username: Kiik.api_key, password: ''}, headers: { "Accept-Version" => Kiik.version, "Content-Type" => 'application/json' }.merge(headers) } end |
.request(path = nil, params = {}, method = :GET, header = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/kiik/resource.rb', line 33 def request(path=nil, params={}, method=:GET, header={}) = opts(header).merge(body: JSON.generate(params)) url_abs = path.nil? ? url : "#{url}/#{path}" response = case method when :GET get(url_abs, ) when :POST post(url_abs, ) when :PUT put(url_abs, ) else raise StandardError.new("Method #{method} not implemented") end case response.code when 200 build(JSON.parse(response.body)) when 422, 404 result = JSON.parse(response.body) KiikError.new(result) else raise StandardError.new(response.) end end |
.url ⇒ Object
13 14 15 |
# File 'lib/kiik/resource.rb', line 13 def url "#{Kiik.host}/#{Kiik::Util.underscore(class_name)}s" end |
Instance Method Details
#to_json(attrs = [:id, :created]) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/kiik/resource.rb', line 70 def to_json(attrs = [:id, :created]) result = {} attrs.each{ |attr| value = self.instance_variable_get("@#{attr}") result[attr] = value unless value.nil? or value.empty? } result end |
#valid? ⇒ Boolean
66 67 68 |
# File 'lib/kiik/resource.rb', line 66 def valid? self.errors.empty? end |