Class: Arctic::UI::API

Inherits:
Object
  • Object
show all
Defined in:
lib/arctic/ui/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ API

Returns a new instance of API.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/arctic/ui/api.rb', line 14

def initialize(username, password)
  bearer_token = authenticate!(username, password).token

  # Construct connection object
  @connection = Faraday.new(Arctic::UI.configuration.url) do |conn|
    conn.request :oauth2, bearer_token, token_type: :bearer
    conn.request :json

    conn.response :json

    # If there's a http cache configured, use it
    if Arctic::UI.configuration.cache
      conn.use :http_cache, store: Arctic::UI.configuration.cache
    end

    conn.adapter Faraday.default_adapter
  end

  # Add Client ID to al l requests
  connection.headers['ClientId'] = Arctic::UI.configuration.client_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/arctic/ui/api.rb', line 36

def method_missing(name, *args)
  method = name.to_s.gsub /(get|update|create)_/, 'ui/'

  response = case name.to_s.downcase
    when /get_/ then connection.get method, *args
    when /update_/ then connection.patch method, *args
    when /create_/ then connection.post method, *args
    else super name, *aths
    end

  response.body
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/arctic/ui/api.rb', line 12

def connection
  @connection
end