Class: MC2P::ResourceMixin

Inherits:
Object
  • Object
show all
Defined in:
lib/mixins.rb

Overview

Basic info of the resource

Instance Method Summary collapse

Constructor Details

#initialize(api_request, path, object_item_class, paginator_class) ⇒ ResourceMixin

Initializes a resource Params:

api_request

Api request used to make all the requests to the API

path

Path used to make all the requests to the API

object_item_class

Object item class used to return values

paginator_class

Paginator class used to return values



188
189
190
191
192
193
# File 'lib/mixins.rb', line 188

def initialize(api_request, path, object_item_class, paginator_class)
  @api_request = api_request
  @path = path
  @object_item_class = object_item_class
  @paginator_class = paginator_class
end

Instance Method Details

#_one_item(func, data = nil, resource_id = nil) ⇒ Object

Help function to make a request that return one item Params:

func

function to make the request

data

data passed in the request

resource_id

id to use on the requested url

Returns: an object item that represent the item returned



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mixins.rb', line 208

def _one_item(func, data = nil, resource_id = nil)
  url = resource_id.nil? ? @path : detail_url(resource_id)

  obj_data = @api_request.send(
    func,
    url,
    data,
    nil,
    self,
    resource_id
  )

  @object_item_class.new(obj_data, self)
end

#detail_url(resource_id) ⇒ Object

Params:

resource_id

id used on the url returned

Returns: url to request or change an item



198
199
200
# File 'lib/mixins.rb', line 198

def detail_url(resource_id)
  "#{@path}#{resource_id}/"
end