Class: Chartmogul::V1::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/chartmogul/v1/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, userpwd: nil, method: :get, **params) ⇒ Request

Public: Constructor.

url - The String url. userpwd - The String with credentials for Basic Authentication. method - The Symbol request method (default: :get). params - The Hash query params.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chartmogul/v1/request.rb', line 16

def initialize(url, userpwd: nil, method: :get, **params)
  @url      = url
  @userpwd  = userpwd
  @params   = params
  @response = Typhoeus::Request.new(url,
    userpwd:        userpwd,
    method:         method,
    params:         params,
    connecttimeout: 5,
    timeout:        10
  ).run
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/chartmogul/v1/request.rb', line 8

def params
  @params
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/chartmogul/v1/request.rb', line 8

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/chartmogul/v1/request.rb', line 8

def url
  @url
end

#userpwdObject (readonly)

Returns the value of attribute userpwd.



8
9
10
# File 'lib/chartmogul/v1/request.rb', line 8

def userpwd
  @userpwd
end

Instance Method Details

#bodyObject

Public: Get body.

Returns the instance of Hashie::Mash.



58
59
60
# File 'lib/chartmogul/v1/request.rb', line 58

def body
  @body ||= Hashie::Mash.new(MultiJson.load response.body || '')
end

#codeObject

Public: Get response code.

Returns Integer code of response.



44
45
46
# File 'lib/chartmogul/v1/request.rb', line 44

def code
  response.code
end

#next_pageObject

Public: Get next page.

Returns the instance of Chartmogul::V1::Request.



32
33
34
# File 'lib/chartmogul/v1/request.rb', line 32

def next_page
  Request.new url, params.merge(page: params[:page] + 1, userpwd: userpwd)
end

#ok?Boolean

Returns true at successful request , false otherwise.

Returns:

  • (Boolean)


37
38
39
# File 'lib/chartmogul/v1/request.rb', line 37

def ok?
  [200, 201].include? code
end

#return_codeObject

Public: Get return code.

Returns Integer return code.



51
52
53
# File 'lib/chartmogul/v1/request.rb', line 51

def return_code
  response.return_code
end