module LightSide::Resources::ClassMethods

Public Instance Methods

all() click to toggle source
# File lib/lightside/resources.rb, line 69
def all
  resources
end
attributes() click to toggle source
# File lib/lightside/resources.rb, line 73
def attributes
  @readonly_attributes.to_a + @writable_attributes.to_a + ["errors"]
end
create(data) click to toggle source
# File lib/lightside/resources.rb, line 77
def create(data)
  create_resource(data) { |hash| new(hash) }
end
create_resource(data) { |parse| ... } click to toggle source
# File lib/lightside/resources.rb, line 81
def create_resource(data)
  RestClient.post(resource_url, data, Config.headers) do |response, request, result|
    case response.code
    when 201
      yield JSON.parse(response)
    when 400
      yield JSON.parse(data).merge!("errors" => JSON.parse(response))
    else
      raise response
    end
  end
end
delete(id) click to toggle source
# File lib/lightside/resources.rb, line 94
def delete(id)
  delete_resource(id)
end
delete_resource(id) click to toggle source
# File lib/lightside/resources.rb, line 98
def delete_resource(id)
  RestClient.delete(resource_url(id), Config.headers) do |response, request, result|
    case response.code
    when 204
      return true
    when 404
      raise ResourceNotFound, "could not find #{name} with ID=#{id}"
    else
      raise response
    end
  end
end
find(id) click to toggle source
# File lib/lightside/resources.rb, line 111
def find(id)
  resource(id) { |hash| new(hash) }
end
page(page_num) click to toggle source
# File lib/lightside/resources.rb, line 115
def page(page_num)
  LightSide::Pager.new(self, { page: Integer(page_num) }).fetch
end
query(query_params) click to toggle source
# File lib/lightside/resources.rb, line 119
def query(query_params)
  resources(query_params)
end
resource(id) { |parse| ... } click to toggle source
# File lib/lightside/resources.rb, line 132
def resource(id)
  RestClient.get(resource_url(id), Config.headers) do |response, request, result|
    case response.code
    when 200
      yield JSON.parse(response)
    when 404
      raise ResourceNotFound, "could not find #{name} with ID=#{id}"
    else
      raise response
    end
  end
end
resource_url(id=nil) click to toggle source
# File lib/lightside/resources.rb, line 123
def resource_url(id=nil)
  raise LightSide::NotConfigured unless Config.configured?
  [Config.base_url, "#{resource_base}/#{id}"].join("/")
end
resources(params={}) click to toggle source
# File lib/lightside/resources.rb, line 128
def resources(params={})
  LightSide::Pager.new(self, params).fetch
end
setup() { || ... } click to toggle source
# File lib/lightside/resources.rb, line 145
def setup
  yield
  attributes.each { |attr| attr_accessor attr.to_s }
end
update(id, data) click to toggle source
# File lib/lightside/resources.rb, line 150
def update(id, data)
  update_resource(id, data) { |hash| new(hash) }
end
update_resource(id, data) { |parse| ... } click to toggle source
# File lib/lightside/resources.rb, line 154
def update_resource(id, data)
  RestClient.put(resource_url(id), data, Config.headers) do |response, request, result|
    case response.code
    when 200
      yield JSON.parse(response)
    when 400
      yield ({"errors" => JSON.parse(response)})
    when 404
      raise ResourceNotFound, "could not find #{name} with ID=#{id}"
    else
      raise response
    end
  end
end