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



203
204
205
206
207
208
# File 'lib/mixins.rb', line 203

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mixins.rb', line 223

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



213
214
215
# File 'lib/mixins.rb', line 213

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