Class: ThreeSixty::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/three-sixty/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, opts = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
# File 'lib/three-sixty/client.rb', line 14

def initialize(api_key, api_secret, opts = {})
  opts        = default_options.update(opts)

  @api_key    = api_key
  @api_secret = api_secret
  @endpoint   = opts[:endpoint]
  @version    = opts[:version]
  @format     = opts[:format]
  @logger     = opts[:logger]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



12
13
14
# File 'lib/three-sixty/client.rb', line 12

def access_token
  @access_token
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/three-sixty/client.rb', line 11

def api_key
  @api_key
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



11
12
13
# File 'lib/three-sixty/client.rb', line 11

def endpoint
  @endpoint
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/three-sixty/client.rb', line 11

def logger
  @logger
end

#session_tokenObject (readonly)

Returns the value of attribute session_token.



12
13
14
# File 'lib/three-sixty/client.rb', line 12

def session_token
  @session_token
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/three-sixty/client.rb', line 11

def version
  @version
end

Instance Method Details

#authenticate!(username, password) ⇒ Object



25
26
27
28
29
30
# File 'lib/three-sixty/client.rb', line 25

def authenticate!(username, password)
   = ThreeSixty::Core::Account.new(self)
  tokens = .(username, generate_passwd(password))
  @access_token = tokens['accessToken']
  @session_token = tokens['sessionToken']
end

#request(resource_url, params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/three-sixty/client.rb', line 32

def request(resource_url, params = {})
  params[:format] = @format
  uri = URI(generate_url(resource_url, params))
  @logger.debug "Resquest url #{uri.to_s}"
  req = Net::HTTP::Post.new(uri, headers = generate_headers)
  retry_counter = 0
  begin
    @logger.debug "Sending headers #{headers}"
    res = Net::HTTP.start(uri.host) do |http|
      http.request(req)
    end
    process_response(res)
  rescue Net::ReadTimeout => e # Some requests can take a while, so just retry a few times
    raise e unless retry_counter < 5
    retry_counter += 1
    retry
  end
end