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 = nil, endpoint = nil) ⇒ MaasClient

Returns a new instance of MaasClient.



15
16
17
18
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
# File 'lib/maas/client.rb', line 15

def initialize(api_key = nil, endpoint = nil)

  if api_key and endpoint
    @api_key = api_key
    @endpoint = endpoint
  elsif File.exists?(Maas::Client::Config.config[:conf_file])
    Maas::Client::Config.set_config
    @api_key = Maas::Client::Config.config[:maas][:key]
    @endpoint = Maas::Client::Config.config[:maas][:url]
  else
    abort("There is no server Info provided.")
  end

  @consumer_key = @api_key.split(/:/)[0]
  @key = @api_key.split(/:/)[1]
  @secret = @api_key.split(/:/)[2]

  consumer = OAuth::Consumer.new(
    @consumer_key, '',
    {
      :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.



13
14
15
# File 'lib/maas/client.rb', line 13

def access_token
  @access_token
end

Instance Method Details

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/maas/client.rb', line 48

def request(method, subject, param = nil)

  headers = {Accept: 'application/json'}

  uri = access_token.consumer.options[:site] + 
    '/' + 
    subject.join('/') + 
    '/'


  oauth_params = {
    :consumer => access_token.consumer, 
    :token => access_token
  }

  hydra = Typhoeus::Hydra.new

  Hashie.symbolize_keys! param if param

  options = {
    method: method,
    headers: headers
  }

# https://github.com/typhoeus/typhoeus#sending-params-in-the-body-with-put
  if method == :get
    options.merge!({params: param})
  elsif method == :post
    options.merge!({body: param})
    headers.merge!(
      {'Content-Type'=> "application/x-www-form-urlencoded"}
    )
  end

  req = Typhoeus::Request.new(uri, options)

  oauth_helper = OAuth::Client::Helper.new(
    req,
    oauth_params.merge(
      {
        request_uri: uri,
        signature_method: access_token
        .consumer
        .options[:signature_method]
      }
    )
  )

  req.options[:headers].merge!(
    { "Authorization" => oauth_helper.header }
  )

  hydra.queue(req); hydra.run
  response = req.response

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

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