class Kiik::Resource
Attributes
created[RW]
errors[RW]
id[RW]
Public Class Methods
build(data, error = nil)
click to toggle source
# File lib/kiik/resource.rb, line 27 def build(data, error = nil) if data['result'] && data['total'] instance = Kiik::Paginated.new() instance.result = data['result'].map { |element| self.new(element) } instance.total = data['total'] else instance = self.new(data) end instance.errors = error.errors unless error.nil? instance end
class_name()
click to toggle source
# File lib/kiik/resource.rb, line 9 def class_name self.name.split('::')[-1] end
new(attributes = {})
click to toggle source
# File lib/kiik/resource.rb, line 67 def initialize(attributes = {}) attributes.each{ |name, value| self.instance_variable_set("@#{name}", value) } self.errors = [] end
opts(headers={})
click to toggle source
# 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={})
click to toggle source
# File lib/kiik/resource.rb, line 39 def request(path=nil, params={}, method=:GET, header={}) options = opts(header).merge(body: JSON.generate(params)) url_abs = path.nil? ? url : "#{url}/#{path}" response = case method when :GET get(url_abs, options) when :POST post(url_abs, options) when :PUT put(url_abs, options) else raise StandardError.new("Method #{method} not implemented") end case response.code when 200, 402 build(JSON.parse(response.body)) when 422, 404 result = JSON.parse(response.body) KiikError.new(result) else raise StandardError.new(response.message) end end
url()
click to toggle source
# File lib/kiik/resource.rb, line 13 def url "#{Kiik.host}/#{Kiik::Util.underscore(class_name)}s" end
Public Instance Methods
to_json(attrs = [:id, :created])
click to toggle source
# File lib/kiik/resource.rb, line 76 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?()
click to toggle source
# File lib/kiik/resource.rb, line 72 def valid? self.errors.empty? end