enstratus_helper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +41 -0
- data/lib/enstratus_helper.rb +71 -0
- metadata +81 -0
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# enstratus_helper
|
2
|
+
enstratus_helper is a simple ruby class that helps making request to enStratus web service easier. With enstratus_helper, you only have to specify the resource you want to request, and it will handle the signature generation & HTTP header initialization for you.
|
3
|
+
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'enstratus_helper'
|
9
|
+
access_key = "my_access_key"
|
10
|
+
secret_key = "super_secret_key"
|
11
|
+
|
12
|
+
ehelper = EnstratusHelper.new(:access_key => access_key, :secret_key => secret_key)
|
13
|
+
|
14
|
+
# You can specify whether to use json or xml format
|
15
|
+
ehelper = EnstratusHelper.new(:access_key => access_key, :secret_key => secret_key, :accept => 'application/json')
|
16
|
+
|
17
|
+
# Example of doing GET requests
|
18
|
+
puts ehelper.request :get, 'http://api.enstratus.com/api/enstratus/2011-02-24/geography/Region'
|
19
|
+
puts ehelper.request :get, 'http://api.enstratus.com/api/enstratus/2011-02-24/infrastructure/MachineImage?regionId=my_region_id'
|
20
|
+
puts ehelper.request :get, 'http://api.enstratus.com/api/enstratus/2011-02-24/infrastructure/Server'
|
21
|
+
|
22
|
+
# You can explicitly define GET request header. Here, we're explicitly specifying the format to be JSON
|
23
|
+
#puts ehelper.request :get, 'http://api.enstratus.com/api/enstratus/2011-02-24/admin/BillingCode', :header => {'Accept' => 'application/json'}
|
24
|
+
|
25
|
+
# Example of doing POST requests
|
26
|
+
xml_data =<<EOF
|
27
|
+
<addBillingCode>
|
28
|
+
<billingCode>
|
29
|
+
<name>API Dev 1</name>
|
30
|
+
<financeCode>API 1</financeCode>
|
31
|
+
<description>API Development</description>
|
32
|
+
<softQuota>USD50.00</softQuota>
|
33
|
+
</billingCode>
|
34
|
+
</addBillingCode>
|
35
|
+
EOF
|
36
|
+
json_data = {:addBillingCode => {:billingCode => {:name => 'API 3', :financeCode => "API 3", :description => "description", :softQuota => 'USD11.01', :hardQuota => 'USD11.11'}}}.to_json
|
37
|
+
|
38
|
+
ehelper.request :post, 'http://api.enstratus.com/api/enstratus/2011-02-24/admin/BillingCode', :data => xml_data
|
39
|
+
ehelper.request :post, 'http://api.enstratus.com/api/enstratus/2011-02-24/admin/BillingCode', :data => xml_data, :header => {'Accept' => 'application/xml'}
|
40
|
+
ehelper.request :post, 'http://api.enstratus.com/api/enstratus/2011-02-24/admin/BillingCode', :data => json_data, :header => {'Accept' => 'application/json'}
|
41
|
+
```
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'base64'
|
3
|
+
require 'rest_client'
|
4
|
+
require 'digest/sha2'
|
5
|
+
require 'openssl'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
class EnstratusHelper
|
9
|
+
CONFIGURABLE_ATTRIBUTES = [
|
10
|
+
:details,
|
11
|
+
:accept,
|
12
|
+
:secret_key,
|
13
|
+
:access_key
|
14
|
+
]
|
15
|
+
attr_accessor *CONFIGURABLE_ATTRIBUTES
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
|
19
|
+
self.send("#{attribute_name}=", options[attribute_name]) if options.has_key?(attribute_name)
|
20
|
+
end
|
21
|
+
@accept ||= 'application/xml'
|
22
|
+
@details ||= 'extended'
|
23
|
+
end
|
24
|
+
|
25
|
+
# Generates the required headers for talking to enStratus API
|
26
|
+
# signature is of the following format:
|
27
|
+
# BASE64(SHA256(ACCESS_KEY:METHOD:URI:TIMESTAMP:USER_AGENT))
|
28
|
+
def generate_header(uri, http_method, options={})
|
29
|
+
http_method = http_method.to_s.upcase
|
30
|
+
path = URI.parse(uri).path
|
31
|
+
timestamp = Time.now.to_i * 1000
|
32
|
+
signature = "#{@access_key}:#{http_method}:#{path}:#{timestamp}:#{self.class}"
|
33
|
+
signature = EnstratusHelper::sign(@secret_key, signature)
|
34
|
+
{'x-esauth-access' => @access_key, 'x-esauth-signature' => signature,
|
35
|
+
'x-esauth-timestamp' => timestamp, 'User-agent' => self.class,
|
36
|
+
'x-es-details' => details, 'Accept' => accept}
|
37
|
+
end
|
38
|
+
|
39
|
+
# Sends the request to the given URI using the specified method (GET by default)
|
40
|
+
# Returns the response object (of the rest-client library)
|
41
|
+
def request(method, uri, options={})
|
42
|
+
response = nil
|
43
|
+
data = options[:data]
|
44
|
+
header = generate_header(uri, method)
|
45
|
+
header = header.merge(options[:header]) if options[:header]
|
46
|
+
begin
|
47
|
+
case method
|
48
|
+
when :get
|
49
|
+
response = RestClient.send(method, uri, header)
|
50
|
+
when :delete
|
51
|
+
response = RestClient.send(method, uri, header)
|
52
|
+
when :post
|
53
|
+
header[:content_type] = 'application/xml'
|
54
|
+
response = RestClient.send(method, uri, data, header)
|
55
|
+
when :put
|
56
|
+
response = RestClient.send(method, uri, data, header)
|
57
|
+
else
|
58
|
+
raise "HTTP method #{method} not supported"
|
59
|
+
end
|
60
|
+
rescue => e
|
61
|
+
puts e.inspect
|
62
|
+
end
|
63
|
+
response
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generates the signature for talking to enStratus
|
67
|
+
def self.sign(key, str_to_sign)
|
68
|
+
digest = OpenSSL::Digest::Digest.new('sha256')
|
69
|
+
Base64.encode64(OpenSSL::HMAC.digest(digest, key, str_to_sign)).gsub("\n","")
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enstratus_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Darren Dao
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-12-07 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description:
|
35
|
+
email: darrendao@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- README.md
|
44
|
+
- lib/enstratus_helper.rb
|
45
|
+
homepage: http://github.com/darrendao/enstratus_helper
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --inline-source
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.11
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Simple ruby wrapper to make it easier to talk to enStratus API
|
79
|
+
test_files: []
|
80
|
+
|
81
|
+
has_rdoc: true
|