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, body: nil, headers: {}, method: :get, userpwd: nil, **params) ⇒ Request

Public: Constructor.

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chartmogul/v1/request.rb', line 18

def initialize(url, body: nil, headers: {}, method: :get, userpwd: nil, **params)
  @url     = url
  @userpwd = userpwd
  @params  = params

  if body
    body = MultiJson.dump(body)
    headers.merge!('Content-Type' => 'application/json')
  end

  @response = Typhoeus::Request.new(url,
    body:           body,
    connecttimeout: 5,
    headers:        headers,
    method:         method,
    params:         params,
    timeout:        10,
    userpwd:        userpwd,
  ).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.



68
69
70
# File 'lib/chartmogul/v1/request.rb', line 68

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

#codeObject

Public: Get response code.

Returns Integer code of response.



54
55
56
# File 'lib/chartmogul/v1/request.rb', line 54

def code
  response.code
end

#next_pageObject

Public: Get next page.

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



42
43
44
# File 'lib/chartmogul/v1/request.rb', line 42

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)


47
48
49
# File 'lib/chartmogul/v1/request.rb', line 47

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

#return_codeObject

Public: Get return code.

Returns Integer return code.



61
62
63
# File 'lib/chartmogul/v1/request.rb', line 61

def return_code
  response.return_code
end