Class: WCC::API::RestClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/api/rest_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, endpoint, model, options) ⇒ Resource

Returns a new instance of Resource.


155
156
157
158
159
160
# File 'lib/wcc/api/rest_client.rb', line 155

def initialize(client, endpoint, model, options)
  @client = client
  @endpoint = endpoint
  @model = model
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


153
154
155
# File 'lib/wcc/api/rest_client.rb', line 153

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint


153
154
155
# File 'lib/wcc/api/rest_client.rb', line 153

def endpoint
  @endpoint
end

#modelObject (readonly)

Returns the value of attribute model


153
154
155
# File 'lib/wcc/api/rest_client.rb', line 153

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options


153
154
155
# File 'lib/wcc/api/rest_client.rb', line 153

def options
  @options
end

Instance Method Details

#create(body) ⇒ Object


179
180
181
182
183
# File 'lib/wcc/api/rest_client.rb', line 179

def create(body)
  resp = client.post(endpoint, body)
  resp.assert_ok!
  maybe_model_from_response(resp)
end

#destroy(id) ⇒ Object


191
192
193
194
195
# File 'lib/wcc/api/rest_client.rb', line 191

def destroy(id)
  resp = client.delete("#{endpoint}/#{id}")
  resp.assert_ok!
  maybe_model_from_response(resp)
end

#find(id, query = {}) ⇒ Object


162
163
164
165
166
167
168
# File 'lib/wcc/api/rest_client.rb', line 162

def find(id, query = {})
  query = (options[:query] || {}).merge(query)
  resp = client.get("#{endpoint}/#{id}", query)
  resp.assert_ok!
  body = options[:key] ? resp.body[options[:key]] : resp.body
  model.new(body, resp.headers.freeze)
end

#list(**filters) ⇒ Object


170
171
172
173
174
175
176
177
# File 'lib/wcc/api/rest_client.rb', line 170

def list(**filters)
  query = extract_params(filters)
  query = (options[:query] || {}).merge(query)
  query = query.merge!(apply_filters(filters, options[:filters]))
  resp = client.get(endpoint, query)
  resp.assert_ok!
  resp.items.map { |s| model.new(s) }
end

#update(id, body) ⇒ Object


185
186
187
188
189
# File 'lib/wcc/api/rest_client.rb', line 185

def update(id, body)
  resp = client.put("#{endpoint}/#{id}", body)
  resp.assert_ok!
  maybe_model_from_response(resp)
end