Class: Zuora::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, sandbox = false) ⇒ Zuora::Client

Creates a connection instance. Makes an initial HTTP request to fetch session token. Subsequent requests made with .get, .post, and .put contain the authenticated session id in their headers.

Parameters:

  • username (String)
  • password (String)
  • sandbox (Boolean) (defaults to: false)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zuora/client.rb', line 24

def initialize(username, password, sandbox = false)
  url = api_url sandbox

  response = connection(url).post do |req|
    req.url '/rest/v1/connections'
    req.headers['apiAccessKeyId'] = username
    req.headers['apiSecretAccessKey'] = password
    req.headers['Content-Type'] = 'application/json'
  end

  if response.status == 200
    @auth_cookie = response.headers['set-cookie'].split(' ')[0]
    @connection = connection(url)
  else
    fail ConnectionError, response.body['reasons']
  end
end

Instance Attribute Details

#connection=(value) ⇒ Object

Sets the attribute connection

Parameters:

  • value

    the value to set the attribute connection to.



14
15
16
# File 'lib/zuora/client.rb', line 14

def connection=(value)
  @connection = value
end

Instance Method Details

#get(url) ⇒ Faraday::Response

Returns A response, with .headers, .status & .body.

Parameters:

  • url (String)
    • URL of request

Returns:

  • (Faraday::Response)

    A response, with .headers, .status & .body



44
45
46
47
48
49
50
# File 'lib/zuora/client.rb', line 44

def get(url)
  @connection.get do |req|
    req.url url
    req.headers['Content-Type'] = 'application/json'
    req.headers['Cookie'] = @auth_cookie
  end
end

#post(url, params) ⇒ Faraday::Response

Returns A response, with .headers, .status & .body.

Parameters:

  • url (String)
    • URL for HTTP POST request

  • params (Params)
    • Data to be sent in request body

Returns:

  • (Faraday::Response)

    A response, with .headers, .status & .body



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zuora/client.rb', line 55

def post(url, params)
  response = @connection.post do |req|
    req.url url
    req.headers['Content-Type'] = 'application/json'
    req.headers['Cookie'] = @auth_cookie
    req.body = JSON.generate params
  end

  response
  # if response.body['success']
  #  return response
  # else
  #  raise ErrorResponse.new(response)
  # end
end

#put(url, params) ⇒ Faraday::Response

Returns A response, with .headers, .status & .body.

Parameters:

  • url (String)
    • URL for HTTP PUT request

  • params (Params)
    • Data to be sent in request body

Returns:

  • (Faraday::Response)

    A response, with .headers, .status & .body



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zuora/client.rb', line 74

def put(url, params)
  response = @connection.put do |req|
    req.url url
    req.headers['Content-Type'] = 'application/json'
    req.headers['Cookie'] = @auth_cookie
    req.body = JSON.generate params
  end

  response
  # if response.body['success']
  #  return response
  # else
  #  raise ErrorResponse.new(response)
  # end
end