Class: Maas::Client::MaasClient

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

Overview

A class that can be used to call MAAS API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, endpoint) ⇒ MaasClient

Returns a new instance of MaasClient.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/maas/client.rb', line 12

def initialize(api_key, endpoint)
  @api_key = api_key
  @consumer_key = @api_key.split(/:/)[0]
  @key = @api_key.split(/:/)[1]
  @secret = @api_key.split(/:/)[2]
  @endpoint = endpoint
  consumer = OAuth::Consumer.new(
    @consumer_key,
    '',
    realm: '',
    site: @endpoint,
    scheme: :header,
    signature_method: 'PLAINTEXT'
  )
  @access_token = OAuth::AccessToken.new(
    consumer,
    @key,
    @secret
  )
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/maas/client.rb', line 10

def access_token
  @access_token
end

Instance Method Details

#request(method, subject, param = nil) ⇒ Object



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

def request(method, subject, param = nil)
  default_param = {
    'Accept' => 'application/json',
    'Content-Type' => 'multipart/form-data'
  }
  uri = '/' + subject.join('/') + '/'
  arguments = [method, uri, param, default_param].compact
  response = access_token.request(*arguments)

  return JSON.parse(response.body) if response.code == '200'

  if response.code == '204'
    puts 'No Content'
  else
    raise "#{response.class} #{response.code} \
      #{response.message} #{response.body}"
  end
end