cloudhdr 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ module Cloudhdr
2
+ class HTTP < Base
3
+ class Typhoeus
4
+
5
+ def self.post(url, options={})
6
+ perform(:post, url, options)
7
+ end
8
+
9
+ def self.put(url, options={})
10
+ perform(:put, url, options)
11
+ end
12
+
13
+ def self.get(url, options={})
14
+ perform(:get, url, options)
15
+ end
16
+
17
+ def self.delete(url, options={})
18
+ perform(:delete, url, options)
19
+ end
20
+
21
+ def self.perform(method, url, options={})
22
+ if options.delete(:skip_ssl_verify)
23
+ options[:disable_ssl_peer_verification] = true
24
+ end
25
+
26
+ ::Typhoeus::Request.send(method, url, options)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ module Cloudhdr
2
+ class Job < Base
3
+
4
+ def self.pick_format(options)
5
+ options[:format] || "json"
6
+ end
7
+
8
+ def self.create(params={}, options={})
9
+ params = apply_api_key(params, options[:format])
10
+ HTTP.post("#{options[:base_url] || base_url}/api/v1/jobs.#{pick_format options}",
11
+ encode(params, options[:format]),
12
+ options)
13
+ end
14
+
15
+ def self.list(options={})
16
+ params = {:api_key => options.delete(:api_key) || api_key,
17
+ :page => options.delete(:page) || 1,
18
+ :per_page => options.delete(:per_page) || 50,
19
+ :state => options.delete(:state) }
20
+
21
+ HTTP.get("#{options[:base_url] || base_url}/api/v1/jobs.#{pick_format options}", merge_params(options, params))
22
+ end
23
+
24
+ def self.details(job_id, options={})
25
+ params = {:api_key => options.delete(:api_key) || api_key}
26
+ HTTP.get("#{options[:base_url] || base_url}/api/v1/jobs/#{job_id}.#{pick_format options}", merge_params(options, params))
27
+ end
28
+
29
+ def self.resubmit(job_id, options={})
30
+ params = {:api_key => options.delete(:api_key) || api_key}
31
+ HTTP.get("#{options[:base_url] || base_url}/api/v1/jobs/#{job_id}/resubmit.#{pick_format options}", merge_params(options, params))
32
+ end
33
+
34
+ def self.cancel(job_id, options={})
35
+ params = {:api_key => options.delete(:api_key) || api_key}
36
+ HTTP.get("#{options[:base_url] || base_url}/api/v1/jobs/#{job_id}/cancel.#{pick_format options}", merge_params(options, params))
37
+ end
38
+
39
+ def self.delete(job_id, options={})
40
+ params = {:api_key => options.delete(:api_key) || api_key}
41
+ HTTP.delete("#{options[:base_url] || base_url}/api/v1/jobs/#{job_id}.#{pick_format options}", merge_params(options, params))
42
+ end
43
+
44
+
45
+ end
46
+ end
@@ -0,0 +1,33 @@
1
+ module Cloudhdr
2
+ class Response < Base
3
+
4
+ attr_accessor :code, :body, :raw_body, :raw_response
5
+
6
+ def initialize(options={})
7
+ options.each do |k, v|
8
+ send("#{k}=", v) if respond_to?("#{k}=")
9
+ end
10
+ end
11
+
12
+ def success?
13
+ code.to_i > 199 && code.to_i < 300
14
+ end
15
+
16
+ def errors
17
+ if body.is_a?(Hash)
18
+ Array(body['errors']).compact
19
+ else
20
+ []
21
+ end
22
+ end
23
+
24
+ def body_without_wrapper
25
+ if body.is_a?(Hash) && body['api_response']
26
+ body['api_response']
27
+ else
28
+ body
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Cloudhdr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Cloudhdr" do
4
+
5
+
6
+ it "should allow ENV variable to set an api key" do
7
+ Cloudhdr.api_key = nil
8
+ ENV['CLOUDHDR_API_KEY'] = "envkey"
9
+ Cloudhdr.api_key.should == "envkey"
10
+ Cloudhdr::Job.api_key.should == "envkey"
11
+ end
12
+
13
+ it "should allow user to set API key" do
14
+ Cloudhdr.api_key = "testkey"
15
+ Cloudhdr.api_key.should == "testkey"
16
+ Cloudhdr::Job.api_key.should == "testkey"
17
+ end
18
+
19
+ it "should take user-supplie api key over ENV-supplied key" do
20
+ Cloudhdr.api_key = "testkey"
21
+ ENV['CLOUDHDR_API_KEY'] = "envkey"
22
+ Cloudhdr.api_key.should == "testkey"
23
+ Cloudhdr::Job.api_key.should == "testkey"
24
+ end
25
+
26
+ it "should encode to json" do
27
+ Cloudhdr::Base.encode({:api_request => {:input => 'https://example.com'}}, :json).should =~ /"api_request"/
28
+ end
29
+
30
+ it "should encode to xml" do
31
+ Cloudhdr::Base.encode({:api_request => {:input => 'https://example.com'}}, :xml).should =~ /<api-request>/
32
+ end
33
+
34
+ it "should encode to xml with multiple keys" do
35
+ Cloudhdr::Base.encode({:api_request => {:input => 'https://example.com'}, :test=>"testing"}, :xml).should =~ /api-request/
36
+ end
37
+
38
+ it "should default to encoding to json" do
39
+ Cloudhdr::Base.encode({:api_request => {:input => 'https://example.com' } }).should =~ /"api_request"/
40
+ end
41
+
42
+ it "should not encode when the content is a String" do
43
+ Cloudhdr::Base.encode("api_request").should =~ /^api_request$/
44
+ end
45
+
46
+ it "should decode from xml" do
47
+ Cloudhdr::Base.decode("<api-request><input>https://example.com</input></api-request>", :xml)['api_request']['input'].should == "https://example.com"
48
+ end
49
+
50
+ it "should decode from json" do
51
+ Cloudhdr::Base.decode(%@{"api_request": {"input": "https://example.com"}}@, :json)['api_request']['input'].should == "https://example.com"
52
+ end
53
+
54
+ it "should default to decoding from json" do
55
+ Cloudhdr::Base.decode(%@{"api_request": {"input": "https://example.com"}}@)['api_request']['input'].should == "https://example.com"
56
+ end
57
+
58
+ it "should not decode when content is not a String" do
59
+ Cloudhdr::Base.decode(1).should == 1
60
+ end
61
+
62
+ it "should have a base url" do
63
+ Cloudhdr::Base.base_url.should_not be_nil
64
+ end
65
+
66
+ end
@@ -0,0 +1,100 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "Cloudhdr::HTTP::NetHTTP" do
4
+
5
+ describe "call options" do
6
+ it "should request with timeout" do
7
+ stub_request(:post, "https://example.com")
8
+ Timeout.should_receive(:timeout).with(0.001)
9
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com', :timeout => 1)
10
+ end
11
+
12
+ it "should request without timeout" do
13
+ stub_request(:post, "https://example.com")
14
+ Timeout.stub(:timeout).and_raise(Exception)
15
+ lambda { Cloudhdr::HTTP::NetHTTP.post('https://example.com', :timeout => nil) }.should_not raise_error(Exception)
16
+ end
17
+
18
+ it "should add params to the query string if passed" do
19
+ stub_request(:post, "https://example.com/path?some=param")
20
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param' } })
21
+ end
22
+
23
+ it "should add params to the existing query string if passed" do
24
+ stub_request(:post,'https://example.com/path?original=param&some=param')
25
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}})
26
+ end
27
+
28
+ it "should add headers" do
29
+ stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header' })
30
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header' } })
31
+ end
32
+
33
+ it "should add the body to the request" do
34
+ stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}')
35
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'} )
36
+ end
37
+ end
38
+
39
+ describe "SSL verification" do
40
+ it "should verify when the SSL directory is found" do
41
+ http_stub = stub(:use_ssl= => true, :ca_path= => true, :verify_depth= => true, :request => true)
42
+ http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
43
+ ::Net::HTTP.should_receive(:new).and_return(http_stub)
44
+ Cloudhdr::HTTP::NetHTTP.any_instance.should_receive(:locate_root_cert_path).and_return('/fake/path')
45
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path')
46
+ end
47
+
48
+ it "should not verify when set to skip ssl verification" do
49
+ http_stub = stub(:use_ssl= => true, :request => true)
50
+ http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
51
+ ::Net::HTTP.should_receive(:new).and_return(http_stub)
52
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true)
53
+ end
54
+
55
+ it "should not verify when the SSL directory is not found" do
56
+ http_stub = stub(:use_ssl= => true, :ca_path= => true, :verify_depth= => true, :request => true)
57
+ http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
58
+ ::Net::HTTP.should_receive(:new).and_return(http_stub)
59
+ Cloudhdr::HTTP::NetHTTP.any_instance.should_receive(:locate_root_cert_path).and_return(nil)
60
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com/path')
61
+ end
62
+ end
63
+
64
+ describe ".post" do
65
+ it "should POST to specified body to the specified path" do
66
+ Cloudhdr::HTTP::NetHTTP.should_receive(:post).
67
+ with('https://example.com',:body => '{}').
68
+ and_return(Cloudhdr::Response.new)
69
+ Cloudhdr::HTTP::NetHTTP.post('https://example.com',:body => '{}').should_not be_nil
70
+ end
71
+ end
72
+
73
+ describe ".put" do
74
+ it "should PUT to specified body to the specified path" do
75
+ Cloudhdr::HTTP::NetHTTP.should_receive(:put).
76
+ with('https://example.com',:body => '{}').
77
+ and_return(Cloudhdr::Response.new)
78
+ Cloudhdr::HTTP::NetHTTP.put('https://example.com', :body => '{}')
79
+ end
80
+ end
81
+
82
+ describe ".get" do
83
+ it "should GET to specified body to the specified path" do
84
+ Cloudhdr::HTTP::NetHTTP.should_receive(:get).
85
+ with('https://example.com').
86
+ and_return(Cloudhdr::Response.new)
87
+ Cloudhdr::HTTP::NetHTTP.get('https://example.com')
88
+ end
89
+ end
90
+
91
+ describe ".delete" do
92
+ it "should DELETE to specified body to the specified path" do
93
+ Cloudhdr::HTTP::NetHTTP.should_receive(:delete).
94
+ with('https://example.com').
95
+ and_return(Cloudhdr::Response.new)
96
+ Cloudhdr::HTTP::NetHTTP.delete('https://example.com')
97
+ end
98
+ end
99
+ end
100
+
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+
4
+ # Most useless tests ever, but who knows, right?
5
+
6
+ if defined?(Typhoeus)
7
+
8
+ describe "Cloudhdr::HTTP::Typhoeus" do
9
+ describe ".post" do
10
+ it "should POST using Typhoeus" do
11
+ Typhoeus::Request.expects(:post).with('https://example.com', {:some => 'options'})
12
+ Cloudhdr::HTTP::Typhoeus.post('https://example.com', {:some => 'options'})
13
+ end
14
+ end
15
+
16
+ describe ".put" do
17
+ it "should PUT using Typhoeus" do
18
+ Typhoeus::Request.expects(:put).with('https://example.com', {:some => 'options'})
19
+ Cloudhdr::HTTP::Typhoeus.put('https://example.com', {:some => 'options'})
20
+ end
21
+ end
22
+
23
+ describe ".get" do
24
+ it "should GET using Typhoeus" do
25
+ Typhoeus::Request.expects(:get).with('https://example.com', {:some => 'options'})
26
+ Cloudhdr::HTTP::Typhoeus.get('https://example.com', {:some => 'options'})
27
+ end
28
+ end
29
+
30
+ describe ".delete" do
31
+ it "should DELETE using Typhoeus" do
32
+ Typhoeus::Request.expects(:delete).with('https://example.com', {:some => 'options'})
33
+ Cloudhdr::HTTP::Typhoeus.delete('https://example.com', {:some => 'options'})
34
+ end
35
+ end
36
+
37
+ it "should skip ssl verification" do
38
+ Typhoeus::Request.expects(:get).with('https://example.com', {:disable_ssl_peer_verification => true})
39
+ Cloudhdr::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true})
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,117 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Cloudhdr::HTTP" do
4
+
5
+ it "should have a default_options hash" do
6
+ Cloudhdr::HTTP.default_options.is_a?(Hash).should be_true
7
+ end
8
+
9
+ it "should have a default HTTP backend" do
10
+ Cloudhdr::HTTP.http_backend.should_not be_nil
11
+ end
12
+
13
+
14
+ it "should allow the changing of the HTTP backend" do
15
+ Cloudhdr::HTTP.http_backend.should_not == Cloudhdr::HTTP::Typhoeus
16
+ lambda { Cloudhdr::HTTP.http_backend = Cloudhdr::HTTP::Typhoeus }.should_not raise_error(Exception)
17
+ Cloudhdr::HTTP.http_backend.should == Cloudhdr::HTTP::Typhoeus
18
+ end
19
+
20
+ it "should raise a Cloudhdr::HTTPError when there is an HTTP error" do
21
+ Cloudhdr::HTTP.http_backend.should_receive(:get).
22
+ with('https://example.com', Cloudhdr::HTTP.default_options).
23
+ twice.
24
+ and_raise(Errno::ECONNREFUSED)
25
+ lambda { Cloudhdr::HTTP.get('https://example.com') }.should raise_error(Cloudhdr::HTTPError)
26
+
27
+ begin
28
+ Cloudhdr::HTTP.get('https://example.com')
29
+ rescue Cloudhdr::HTTPError => e
30
+ e.backtrace.first.should_not =~ /perform_method/
31
+ end
32
+ end
33
+
34
+ it "should return a Cloudhdr::Response" do
35
+ Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
36
+ Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
37
+ Cloudhdr::HTTP.http_backend.stub(:get).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
38
+ Cloudhdr::HTTP.http_backend.stub(:delete).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
39
+
40
+ Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Cloudhdr::Response).should be_true
41
+ Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Cloudhdr::Response).should be_true
42
+ Cloudhdr::HTTP.get('https://example.com').is_a?(Cloudhdr::Response).should be_true
43
+ Cloudhdr::HTTP.delete('https://example.com').is_a?(Cloudhdr::Response).should be_true
44
+ end
45
+
46
+ it "should store the raw response" do
47
+ post_stub = stub(:code => 200, :body => '{"some": "hash"}')
48
+ Cloudhdr::HTTP.http_backend.stub(:post).and_return(post_stub)
49
+ Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').raw_response.should == post_stub
50
+ end
51
+
52
+ it "should store the raw response body" do
53
+ Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
54
+ Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').raw_body.should == '{"some": "hash"}'
55
+ end
56
+
57
+ it "should store the response code" do
58
+ Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
59
+ Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').code.should == 200
60
+ end
61
+
62
+ it "should JSON parse the response body" do
63
+ Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": "hash"}'))
64
+ Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').body.should == {'some' => 'hash'}
65
+ end
66
+
67
+ it "should store the raw body if the body fails to be JSON parsed" do
68
+ Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": bad json'))
69
+ Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').body.should == '{"some": bad json'
70
+ end
71
+
72
+ describe ".post" do
73
+ it "should call post on the http_backend" do
74
+ Cloudhdr::HTTP.http_backend.should_receive(:post).
75
+ with('https://example.com', Cloudhdr::HTTP.default_options.merge(:body => '{}')).
76
+ and_return(Cloudhdr::Response.new)
77
+
78
+ Cloudhdr::HTTP.post('https://example.com', '{}').should_not be_nil
79
+ end
80
+ end
81
+
82
+ describe ".put" do
83
+ it "should call put on the http_backend" do
84
+ Cloudhdr::HTTP.http_backend.should_receive(:put).
85
+ with('https://example.com', Cloudhdr::HTTP.default_options.merge(:body => '{}')).
86
+ and_return(Cloudhdr::Response.new)
87
+
88
+ Cloudhdr::HTTP.put('https://example.com', '{}').should_not be_nil
89
+ end
90
+ end
91
+
92
+ describe ".get" do
93
+ it "should call post on the http_backend" do
94
+ Cloudhdr::HTTP.http_backend.should_receive(:get).
95
+ with('https://example.com', Cloudhdr::HTTP.default_options).
96
+ and_return(Cloudhdr::Response.new)
97
+
98
+ Cloudhdr::HTTP.get('https://example.com').should_not be_nil
99
+ end
100
+ end
101
+
102
+ describe ".delete" do
103
+ it "should call post on the http_backend" do
104
+ Cloudhdr::HTTP.http_backend.should_receive(:delete).
105
+ with('https://example.com', Cloudhdr::HTTP.default_options).
106
+ and_return(Cloudhdr::Response.new)
107
+
108
+ Cloudhdr::HTTP.delete('https://example.com').should_not be_nil
109
+ end
110
+ end
111
+
112
+
113
+
114
+
115
+
116
+
117
+ end