Class: Matroid::MatroidClient

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MatroidClient

Returns a new instance of MatroidClient.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/matroid.rb', line 7

def initialize(options)
  @base_url = options[:base_url]
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @request = HTTParty

  @endpoints = {
    token: { method: :post, uri: "#{@base_url}/oauth/token" },
    account_info: { method: :get, uri: "#{@base_url}/account" }
  }
end

Instance Method Details

#account_infoObject



33
34
35
36
37
38
39
40
41
# File 'lib/matroid.rb', line 33

def 
  endpoint = @endpoints[:account_info]
  headers = { "Authorization" => @authorization_header }
  opts = {
    headers: headers
  }
  response = @request.send(endpoint[:method], endpoint[:uri], opts)
  response.parsed_response
end

#retrieve_tokenObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/matroid.rb', line 19

def retrieve_token
  endpoint = @endpoints[:token]
  opts = {
    body: {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials'
    }
  }
  response = @request.send(endpoint[:method], endpoint[:uri], opts)
  @authorization_header = "#{response['token_type']} #{response['access_token']}"
  response.parsed_response
end