Class: Dynect::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Client

log in, set auth token



11
12
13
14
15
16
# File 'lib/dynect4r.rb', line 11

def initialize(params)
  @base_url = 'https://api2.dynect.net'
  @headers = { :content_type => :json, :accept => :json }
  response = rest_call(:post, 'Session', params)
  @headers['Auth-Token'] = response['data']['token']
end

Instance Method Details

#rest_call(action, resource, arguments = nil) ⇒ Object

do a rest call



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dynect4r.rb', line 19

def rest_call(action, resource, arguments = nil)

  # set up retry loop
  max_tries = 12
  for try_counter in (1..max_tries)

    # pause between retries
    if try_counter > 1
      sleep(5)
    end

    resource_url = resource_to_url(resource)

    # do rest call
    begin
      response = case action
      when :post, :put
        RestClient.send(action, resource_url, arguments.to_json, @headers) do |res,req|
          Dynect::Response.new(res)
        end
      else
        RestClient.send(action, resource_url, @headers) do |res,req|
          Dynect::Response.new(res)
        end
      end

      # if we got this far, then it's safe to break out of the retry loop
      break

    # on redirect, rewrite rest call params and retry
    rescue RedirectError
      if try_counter < max_tries
        action = :get
        resource = $!.message
        arguments = nil
      else
        raise OperationTimedOut, "Maximum number of tries (%d) exceeded on resource: %s" % [max_tries, resource]
      end
    end

  end

  # return a response object
  response
end