Module: BaseAtlasClient

Included in:
Exercises
Defined in:
lib/client/base_atlas_client.rb

Instance Method Summary collapse

Instance Method Details

#auth_headerHash

Set the API Authorization Header

Returns:

  • (Hash)

    The authorization header



35
36
37
# File 'lib/client/base_atlas_client.rb', line 35

def auth_header
  { "authorization" => 'Token token=' + @api_key }
end

#convert_keys(options) ⇒ Object



39
40
41
42
# File 'lib/client/base_atlas_client.rb', line 39

def convert_keys(options)
  options.keys.each {|k| options[k.to_s] = options.delete(k) if k.kind_of?(Symbol)}
  options
end

#convert_response(response, name) ⇒ Object

This needs a better name



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/client/base_atlas_client.rb', line 14

def convert_response(response, name)
  if success?(response.code)
    body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body

    if body.is_a?(Array)
      body.map {|hash| AtlasClassFactory.build_response_object(hash, name)}
    else
      AtlasClassFactory.build_response_object(body, name)
    end
  else
    return response
  end
end

#handle_timeoutsObject

Handle API timeouts



5
6
7
8
9
10
11
# File 'lib/client/base_atlas_client.rb', line 5

def handle_timeouts
  begin
    yield
  rescue Net::OpenTimeout, Net::ReadTimeout
    {}
  end
end

#success?(code) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/client/base_atlas_client.rb', line 28

def success?(code)
  code.between?(200, 299)
end

#whitelist_params(options, whitelist) ⇒ Object



44
45
46
# File 'lib/client/base_atlas_client.rb', line 44

def whitelist_params(options, whitelist)
  options.select {|k, v| whitelist.include?(k)}
end