cloudhdr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/cloudhdr.gemspec +30 -0
- data/lib/cloudhdr.rb +22 -0
- data/lib/cloudhdr/base.rb +85 -0
- data/lib/cloudhdr/cloudhdr.rb +17 -0
- data/lib/cloudhdr/errors.rb +43 -0
- data/lib/cloudhdr/extensions.rb +39 -0
- data/lib/cloudhdr/http.rb +90 -0
- data/lib/cloudhdr/http/net_http.rb +142 -0
- data/lib/cloudhdr/http/typhoeus.rb +31 -0
- data/lib/cloudhdr/job.rb +46 -0
- data/lib/cloudhdr/response.rb +33 -0
- data/lib/cloudhdr/version.rb +3 -0
- data/spec/cloudhdr/cloudhdr_spec.rb +66 -0
- data/spec/cloudhdr/http/net_http_spec.rb +100 -0
- data/spec/cloudhdr/http/typhoeus_spec.rb +43 -0
- data/spec/cloudhdr/http_spec.rb +117 -0
- data/spec/cloudhdr/job_spec.rb +128 -0
- data/spec/cloudhdr/response_spec.rb +49 -0
- data/spec/spec_helper.rb +22 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a3036def6a619677ffd69024da48637829b1914b
|
4
|
+
data.tar.gz: e77342a103971dbd7d3a0cbfad6c566b4c100923
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5370100a148b2d47ca5657c61a347f4a2f50a58e269a3b7d4807c451cc9b7e9ced19fd4e6e4e39fe6114b0c953865a3fe85edbd3c601d6013a07b4e9e7ef3c64
|
7
|
+
data.tar.gz: 80351437144c162fbfe9d5a1c8ef0933149243bcdfcaf5cf0e58525f5dfef69f30a9eb12ed9459ff990bea53a31e9089d74a77be3e18c000bf1d93bcd6c56388
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jeremy Green
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Cloudhdr
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cloudhdr'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cloudhdr
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/cloudhdr.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cloudhdr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cloudhdr"
|
8
|
+
spec.version = Cloudhdr::VERSION
|
9
|
+
spec.authors = ["Jeremy Green"]
|
10
|
+
spec.email = ["jeremy@octolabs.com"]
|
11
|
+
spec.description = %q{Ruby wrapper for the CloudHdr Processing API}
|
12
|
+
spec.summary = %q{Ruby wrapper for the CloudHdr Processing API}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport", "> 3.0.0"
|
22
|
+
spec.add_dependency "json", "~> 1.8.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.13.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 1.11.0"
|
27
|
+
#spec.add_development_dependency "mocha", "~> 0.14.0"
|
28
|
+
spec.add_development_dependency "builder", "~> 3.2.0"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
end
|
data/lib/cloudhdr.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# ActiveSupport 3.0 with fallback to 2.0
|
2
|
+
begin
|
3
|
+
require 'active_support/all' # Screw it
|
4
|
+
rescue LoadError
|
5
|
+
require 'active_support' # JSON and XML parsing/encoding
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
require "cloudhdr/version"
|
10
|
+
require 'cloudhdr/extensions'
|
11
|
+
require 'cloudhdr/cloudhdr'
|
12
|
+
require 'cloudhdr/base'
|
13
|
+
require 'cloudhdr/http/net_http'
|
14
|
+
require 'cloudhdr/http/typhoeus'
|
15
|
+
require 'cloudhdr/http'
|
16
|
+
require 'cloudhdr/errors'
|
17
|
+
require 'cloudhdr/job'
|
18
|
+
require 'cloudhdr/response'
|
19
|
+
|
20
|
+
module Cloudhdr
|
21
|
+
# Your code goes here...
|
22
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Cloudhdr
|
2
|
+
class Base
|
3
|
+
|
4
|
+
def self.api_key
|
5
|
+
Cloudhdr.api_key
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.base_url
|
9
|
+
Cloudhdr.base_url
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.encode(content, format=nil)
|
13
|
+
if content.is_a?(String)
|
14
|
+
content
|
15
|
+
elsif format.to_s == 'xml'
|
16
|
+
if content.is_a?(Hash) && content.keys.size == 1
|
17
|
+
content[content.keys.first].to_xml(:root => content.keys.first)
|
18
|
+
else
|
19
|
+
content.to_xml
|
20
|
+
end
|
21
|
+
else
|
22
|
+
content.to_json
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def encode(content, format=nil)
|
27
|
+
self.class.encode(content, format)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.decode(content, format=nil)
|
31
|
+
begin
|
32
|
+
if content.is_a?(String)
|
33
|
+
if format.to_s == 'xml'
|
34
|
+
Hash.from_xml(content)
|
35
|
+
else
|
36
|
+
ActiveSupport::JSON.decode(content)
|
37
|
+
end
|
38
|
+
else
|
39
|
+
content
|
40
|
+
end
|
41
|
+
rescue Exception => e
|
42
|
+
content
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def decode(content, format=nil)
|
47
|
+
self.class.decode(content, format)
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def self.apply_api_key(params, format=nil)
|
53
|
+
if api_key
|
54
|
+
decoded_params = decode(params, format).with_indifferent_access
|
55
|
+
|
56
|
+
if decoded_params[:api_request]
|
57
|
+
decoded_params[:api_request] = decoded_params[:api_request].with_indifferent_access
|
58
|
+
end
|
59
|
+
|
60
|
+
if format.to_s == 'xml'
|
61
|
+
if !(decoded_params[:api_request] && decoded_params[:api_request][:api_key])
|
62
|
+
decoded_params[:api_request] ||= {}.with_indifferent_access
|
63
|
+
decoded_params[:api_request][:api_key] = api_key
|
64
|
+
end
|
65
|
+
else
|
66
|
+
decoded_params['api_key'] = api_key unless decoded_params['api_key']
|
67
|
+
end
|
68
|
+
|
69
|
+
decoded_params
|
70
|
+
else
|
71
|
+
params
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.merge_params(options, params)
|
76
|
+
if options[:params]
|
77
|
+
options[:params] = options[:params].merge(params)
|
78
|
+
options
|
79
|
+
else
|
80
|
+
options.merge(:params => params)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Cloudhdr
|
2
|
+
|
3
|
+
mattr_writer :api_key
|
4
|
+
mattr_writer :base_url
|
5
|
+
|
6
|
+
self.api_key = nil
|
7
|
+
self.base_url = 'http://processing.cloudhdr.com/'
|
8
|
+
|
9
|
+
def self.api_key
|
10
|
+
@@api_key || ENV['CLOUDHDR_API_KEY']
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.base_url(env=nil)
|
14
|
+
@@base_url
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Cloudhdr
|
2
|
+
class Error < StandardError
|
3
|
+
|
4
|
+
def initialize(error_or_message)
|
5
|
+
if error_or_message.is_a?(Exception)
|
6
|
+
@error = error_or_message
|
7
|
+
else
|
8
|
+
@message = error_or_message
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
@message || "#{@error.class} (wrapped in a #{self.class}) - #{@error.message}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def backtrace
|
17
|
+
if @error
|
18
|
+
@error.backtrace
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def inspect
|
25
|
+
if @error
|
26
|
+
"#{@error.inspect} (wrapped in a #{self.class})"
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
if @error
|
34
|
+
"#{@error.class} (wrapped in a #{self.class}) - #{@error}"
|
35
|
+
else
|
36
|
+
super
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class HTTPError < Error; end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
unless Hash.method_defined?(:recursive_with_indifferent_access)
|
2
|
+
class Hash
|
3
|
+
def recursive_with_indifferent_access
|
4
|
+
hash = with_indifferent_access
|
5
|
+
|
6
|
+
hash.each do |key, value|
|
7
|
+
if value.is_a?(Hash) || value.is_a?(Array)
|
8
|
+
hash[key] = value.recursive_with_indifferent_access
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
unless Array.method_defined?(:recursive_with_indifferent_access)
|
18
|
+
class Array
|
19
|
+
def recursive_with_indifferent_access
|
20
|
+
array = dup
|
21
|
+
|
22
|
+
array.each_with_index do |value, index|
|
23
|
+
if value.is_a?(Hash) || value.is_a?(Array)
|
24
|
+
array[index] = value.recursive_with_indifferent_access
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
array
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
#
|
33
|
+
#unless String.method_defined?(:shell_escape)
|
34
|
+
# class String
|
35
|
+
# def shell_escape
|
36
|
+
# empty? ? "''" : gsub(/([^A-Za-z0-9_\-.,:\/@\n])/n, '\\\\\\1').gsub(/\n/, "'\n'")
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
#end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Cloudhdr
|
2
|
+
class HTTP < Base
|
3
|
+
|
4
|
+
attr_accessor :body, :url, :options, :method, :format
|
5
|
+
|
6
|
+
cattr_accessor :default_options
|
7
|
+
cattr_accessor :http_backend
|
8
|
+
|
9
|
+
self.http_backend = NetHTTP
|
10
|
+
|
11
|
+
self.default_options = {:timeout => 10000,
|
12
|
+
:headers => {'Accept' => 'application/json',
|
13
|
+
'Content-Type' => 'application/json'}}.recursive_with_indifferent_access
|
14
|
+
|
15
|
+
def initialize(method, url, options={})
|
16
|
+
self.method = method
|
17
|
+
self.url = url
|
18
|
+
self.format = options.delete(:format)
|
19
|
+
self.options = options
|
20
|
+
self.body = options.delete(:body)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.post(url, body, options={})
|
24
|
+
new(:post, url, options.merge(:body => body)).perform_method
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.put(url, body, options={})
|
28
|
+
new(:put, url, options.merge(:body => body)).perform_method
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.get(url, options={})
|
32
|
+
new(:get, url, options).perform_method
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.delete(url, options={})
|
36
|
+
new(:delete, url, options).perform_method
|
37
|
+
end
|
38
|
+
|
39
|
+
def perform_method
|
40
|
+
process(http_backend.send(method, url, options))
|
41
|
+
rescue StandardError => e
|
42
|
+
raise HTTPError.new(e)
|
43
|
+
end
|
44
|
+
|
45
|
+
def options=(value)
|
46
|
+
@options = default_options.recursive_with_indifferent_access.merge(value || {})
|
47
|
+
|
48
|
+
options[:headers] ||= {}
|
49
|
+
options[:headers]['Accept'] = "application/#{format}"
|
50
|
+
options[:headers]['Content-Type'] = "application/#{format}"
|
51
|
+
|
52
|
+
options
|
53
|
+
end
|
54
|
+
|
55
|
+
def options
|
56
|
+
@options || self.options = default_options
|
57
|
+
end
|
58
|
+
|
59
|
+
def format
|
60
|
+
@format ||= :json
|
61
|
+
end
|
62
|
+
|
63
|
+
def http_backend
|
64
|
+
self.class.http_backend
|
65
|
+
end
|
66
|
+
|
67
|
+
def default_options
|
68
|
+
self.class.default_options.recursive_with_indifferent_access
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def process(http_response)
|
75
|
+
response = Response.new
|
76
|
+
response.code = http_response.code
|
77
|
+
|
78
|
+
begin
|
79
|
+
response.body = decode(http_response.body.to_s, format)
|
80
|
+
rescue StandardError # Hack! Returns different exceptions depending on the decoding engine
|
81
|
+
response.body = http_response.body
|
82
|
+
end
|
83
|
+
|
84
|
+
response.raw_body = http_response.body
|
85
|
+
response.raw_response = http_response
|
86
|
+
response
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# Ruby's Net/HTTP Sucks
|
2
|
+
# Borrowed root cert checking from http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html
|
3
|
+
|
4
|
+
module Cloudhdr
|
5
|
+
class HTTP < Base
|
6
|
+
class NetHTTP
|
7
|
+
|
8
|
+
require 'cgi'
|
9
|
+
|
10
|
+
attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options
|
11
|
+
|
12
|
+
cattr_accessor :root_cert_paths
|
13
|
+
|
14
|
+
self.root_cert_paths = ['/etc/ssl/certs',
|
15
|
+
'/var/ssl',
|
16
|
+
'/usr/share/ssl',
|
17
|
+
'/usr/ssl',
|
18
|
+
'/etc/ssl',
|
19
|
+
'/usr/local/openssl',
|
20
|
+
'/usr/lib/ssl',
|
21
|
+
'/System/Library/OpenSSL',
|
22
|
+
'/etc/openssl',
|
23
|
+
'/usr/local/ssl',
|
24
|
+
'/etc/pki/tls']
|
25
|
+
|
26
|
+
def initialize(method, url, options)
|
27
|
+
@method = method
|
28
|
+
@url = url
|
29
|
+
@body = options.delete(:body)
|
30
|
+
@params = options.delete(:params)
|
31
|
+
@headers = options.delete(:headers)
|
32
|
+
@timeout = options.delete(:timeout)
|
33
|
+
@skip_ssl_verify = options.delete(:skip_ssl_verify)
|
34
|
+
@options = options
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.post(url, options={})
|
38
|
+
new(:post, url, options).perform
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.put(url, options={})
|
42
|
+
new(:put, url, options).perform
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.get(url, options={})
|
46
|
+
new(:get, url, options).perform
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.delete(url, options={})
|
50
|
+
new(:delete, url, options).perform
|
51
|
+
end
|
52
|
+
|
53
|
+
def perform
|
54
|
+
deliver(http, request)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def deliver(http, request)
|
61
|
+
if timeout
|
62
|
+
Timeout.timeout(timeout / 1000.0) do
|
63
|
+
http.request(request)
|
64
|
+
end
|
65
|
+
else
|
66
|
+
http.request(request)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def http
|
71
|
+
u = uri
|
72
|
+
|
73
|
+
http = Net::HTTP.new(u.host, u.port)
|
74
|
+
|
75
|
+
if u.scheme == 'https'
|
76
|
+
http.use_ssl = true
|
77
|
+
|
78
|
+
if !skip_ssl_verify && root_cert_path = locate_root_cert_path
|
79
|
+
http.ca_path = root_cert_path
|
80
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
81
|
+
http.verify_depth = 5
|
82
|
+
else
|
83
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
http
|
88
|
+
end
|
89
|
+
|
90
|
+
def request
|
91
|
+
r = request_class.new(path)
|
92
|
+
r.body = body if body
|
93
|
+
if headers
|
94
|
+
headers.each do |header, value|
|
95
|
+
r.add_field(header.to_s, value.to_s)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
r
|
99
|
+
end
|
100
|
+
|
101
|
+
def uri
|
102
|
+
u = URI.parse(url)
|
103
|
+
|
104
|
+
if params
|
105
|
+
params_as_query = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
|
106
|
+
if u.query.to_s.empty?
|
107
|
+
u.query = params_as_query
|
108
|
+
else
|
109
|
+
u.query = [u.query.to_s, params_as_query].join('&')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
u
|
114
|
+
end
|
115
|
+
|
116
|
+
def path
|
117
|
+
u = uri
|
118
|
+
|
119
|
+
if u.path.empty?
|
120
|
+
u.path = '/'
|
121
|
+
end
|
122
|
+
|
123
|
+
if u.query.to_s.empty?
|
124
|
+
u.path
|
125
|
+
else
|
126
|
+
u.path + '?' + u.query.to_s
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def request_class
|
131
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
132
|
+
end
|
133
|
+
|
134
|
+
def locate_root_cert_path
|
135
|
+
self.class.root_cert_paths.detect do |root_cert_path|
|
136
|
+
File.directory?(root_cert_path)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|