Module: LightSide::Resources::ClassMethods

Defined in:
lib/lightside/resources.rb

Instance Method Summary collapse

Instance Method Details

#allObject



69
70
71
# File 'lib/lightside/resources.rb', line 69

def all
  resources
end

#attributesObject



73
74
75
# File 'lib/lightside/resources.rb', line 73

def attributes
  @readonly_attributes.to_a + @writable_attributes.to_a + ["errors"]
end

#create(data) ⇒ Object



77
78
79
# File 'lib/lightside/resources.rb', line 77

def create(data)
  create_resource(data) { |hash| new(hash) }
end

#create_resource(data) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



94
95
96
# File 'lib/lightside/resources.rb', line 94

def delete(id)
  delete_resource(id)
end

#delete_resource(id) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



111
112
113
# File 'lib/lightside/resources.rb', line 111

def find(id)
  resource(id) { |hash| new(hash) }
end

#page(page_num) ⇒ Object



115
116
117
# File 'lib/lightside/resources.rb', line 115

def page(page_num)
  LightSide::Pager.new(self, { page: Integer(page_num) }).fetch
end

#query(query_params) ⇒ Object



119
120
121
# File 'lib/lightside/resources.rb', line 119

def query(query_params)
  resources(query_params)
end

#resource(id) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



123
124
125
126
# 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 = {}) ⇒ Object



128
129
130
# File 'lib/lightside/resources.rb', line 128

def resources(params={})
  LightSide::Pager.new(self, params).fetch
end

#setupObject



145
146
147
148
# File 'lib/lightside/resources.rb', line 145

def setup
  yield
  attributes.each { |attr| attr_accessor attr.to_s }
end

#update(id, data) ⇒ Object



150
151
152
# File 'lib/lightside/resources.rb', line 150

def update(id, data)
  update_resource(id, data) { |hash| new(hash) }
end

#update_resource(id, data) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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