Class: ScalrApiV2::Request
- Inherits:
-
Object
- Object
- ScalrApiV2::Request
- Defined in:
- lib/scalr_api_v2/request.rb
Overview
connection and request methods for interacting with the Scalr API
Instance Method Summary collapse
-
#create(url, data) ⇒ Object
Create item in API.
-
#delete(url) ⇒ Object
Delete item from API.
-
#fetch(url) ⇒ Object
Fetch a single item from API.
-
#initialize ⇒ Request
constructor
A new instance of Request.
-
#list(url) ⇒ Object
List items from API.
-
#patch(url, data) ⇒ Object
Update items in API.
-
#post(url, data) ⇒ Object
Edit items in API.
- #request(method, url, body = '') ⇒ Object
Constructor Details
#initialize ⇒ Request
Returns a new instance of Request.
12 13 14 |
# File 'lib/scalr_api_v2/request.rb', line 12 def initialize @api_config = ScalrApiV2::Config.new.config end |
Instance Method Details
#create(url, data) ⇒ Object
Create item in API
101 102 103 104 105 |
# File 'lib/scalr_api_v2/request.rb', line 101 def create(url, data) response = request('POST', url, data) response['data'] = response['Error'] unless response['Error'].nil? return response['data'] end |
#delete(url) ⇒ Object
Delete item from API
108 109 110 111 112 |
# File 'lib/scalr_api_v2/request.rb', line 108 def delete(url) response = request('DELETE', url) response['data'] = response['Error'] unless response['Error'].nil? return response['data'] end |
#fetch(url) ⇒ Object
Fetch a single item from API
94 95 96 97 98 |
# File 'lib/scalr_api_v2/request.rb', line 94 def fetch(url) response = request('GET', url) response['data'] = response['Error'] unless response['Error'].nil? return response['data'] end |
#list(url) ⇒ Object
List items from API
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/scalr_api_v2/request.rb', line 78 def list(url) data = [] until url.nil? response = request('GET', url) if response['Error'].nil? data.concat response['data'] url = response['pagination']['next'] else data = response['Error'] url = nil end end return data end |
#patch(url, data) ⇒ Object
Update items in API
122 123 124 125 126 |
# File 'lib/scalr_api_v2/request.rb', line 122 def patch(url, data) response = request('PATCH', url, data) response['data'] = response['Error'] unless response['Error'].nil? return response['data'] end |
#post(url, data) ⇒ Object
Edit items in API
115 116 117 118 119 |
# File 'lib/scalr_api_v2/request.rb', line 115 def post(url, data) response = request('POST', url, data) response['data'] = response['Error'] unless response['Error'].nil? return response['data'] end |
#request(method, url, body = '') ⇒ Object
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 47 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 |
# File 'lib/scalr_api_v2/request.rb', line 16 def request(method, url, body = '') # JSON encode body if set body = body.to_json if body != '' # Split URL into components parts = URI.parse(@api_config['url'] + url) path = parts.path # host = parts.host # port = parts.port query = '' unless parts.query.nil? # Convert querystring into an array q = parts.query.split('&') # Sort the querystring array q.sort! # Convert querystring array back to string query = q.join('&') end # Create ISO 8601 date/time string time = Time.now.utc.iso8601 + '+00:00' # Collection of request data for generating signature request = [ method, time, path, query, body ] # Calculate signature based on request data signature = 'V1-HMAC-SHA256 ' + Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @api_config['key_secret'], request.join("\n"))).strip # HTTP request headers headers = { 'X-Scalr-Key-Id' => @api_config['key_id'], 'X-Scalr-Signature' => signature, 'X-Scalr-Date' => time, 'X-Scalr-Debug' => '1' } headers['Content-Type'] = 'application/json' if body != '' begin response = RestClient::Request.execute( method: method, url: @api_config['url'] + url, headers: headers, payload: body ) rescue => e raise "#{e} #{e.response}" end response = '{\n \"Error\": \"A problem occured: Response was empty\"\n}}' if response == '' JSON.parse(response) end |