Class: Maas::Client::MaasClient
- Inherits:
-
Object
- Object
- Maas::Client::MaasClient
- Defined in:
- lib/maas/client.rb
Overview
A class that can be used to call MAAS API.
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
-
#initialize(api_key = nil, endpoint = nil) ⇒ MaasClient
constructor
A new instance of MaasClient.
- #request(method, subject, param = nil) ⇒ Object
- #valid_json?(json) ⇒ Boolean
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_token ⇒ Object (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 112 113 114 115 |
# File 'lib/maas/client.rb', line 48 def request(method, subject, param = nil) headers = {Accept: 'application/json'} uri = access_token.consumer.[:site] + '/' + subject.join('/') + '/' oauth_params = { :consumer => access_token.consumer, :token => access_token } hydra = Typhoeus::Hydra.new Hashie.symbolize_keys! param if param = { method: method, headers: headers } # https://github.com/typhoeus/typhoeus#sending-params-in-the-body-with-put if [:get].include? method .merge!({params: param}) elsif [:post, :put].include? method .merge!({body: param}) headers.merge!( {'Content-Type'=> "application/x-www-form-urlencoded"} ) end req = Typhoeus::Request.new(uri, ) oauth_helper = OAuth::Client::Helper.new( req, oauth_params.merge( { request_uri: uri, signature_method: access_token .consumer .[:signature_method] } ) ) req.[:headers].merge!( { "Authorization" => oauth_helper.header } ) hydra.queue(req); hydra.run response = req.response if response.code == 200 if valid_json?(response.body) return JSON.parse(response.body) else return response.body end elsif response.code == 204 puts 'No Content' else raise "#{response.class} #{response.code} \ #{response.} #{response.body}" end end |
#valid_json?(json) ⇒ Boolean
117 118 119 120 121 122 |
# File 'lib/maas/client.rb', line 117 def valid_json?(json) JSON.parse(json) return true rescue JSON::ParserError => e return false end |