phocoder-rb 0.1.0

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,33 @@
1
+ module Phocoder
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
data/lib/phocoder.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'cgi'
2
+ require 'net/https'
3
+ require 'timeout'
4
+
5
+ # ActiveSupport 3.0 with fallback to 2.0
6
+ begin
7
+ require 'active_support/all' # Screw it
8
+ rescue LoadError
9
+ require 'active_support' # JSON and XML parsing/encoding
10
+ end
11
+
12
+ require 'phocoder/extensions'
13
+ require 'phocoder/phocoder'
14
+ require 'phocoder/base'
15
+ require 'phocoder/http/net_http'
16
+ require 'phocoder/http/typhoeus'
17
+ require 'phocoder/http'
18
+ require 'phocoder/errors'
19
+ require 'phocoder/job'
20
+ require 'phocoder/response'
@@ -0,0 +1,99 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{phocoder-rb}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jeremy Green"]
12
+ s.date = %q{2010-12-20}
13
+ s.description = %q{The ruby client for the phocoder.com API.}
14
+ s.email = %q{jagthedrummer@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/phocoder.rb",
29
+ "lib/phocoder/base.rb",
30
+ "lib/phocoder/errors.rb",
31
+ "lib/phocoder/extensions.rb",
32
+ "lib/phocoder/http.rb",
33
+ "lib/phocoder/http/net_http.rb",
34
+ "lib/phocoder/http/typhoeus.rb",
35
+ "lib/phocoder/job.rb",
36
+ "lib/phocoder/phocoder.rb",
37
+ "lib/phocoder/response.rb",
38
+ "phocoder-rb.gemspec",
39
+ "spec/phocoder/http/net_http_spec.rb",
40
+ "spec/phocoder/http/typhoeus_spec.rb",
41
+ "spec/phocoder/http_spec.rb",
42
+ "spec/phocoder/job_spec.rb",
43
+ "spec/phocoder/phocoder_spec.rb",
44
+ "spec/phocoder/response_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+ s.homepage = %q{http://github.com/jagthedrummer/phocoder-rb}
48
+ s.licenses = ["MIT"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = %q{1.3.7}
51
+ s.summary = %q{The ruby client for the phocoder.com API.}
52
+ s.test_files = [
53
+ "spec/phocoder/http/net_http_spec.rb",
54
+ "spec/phocoder/http/typhoeus_spec.rb",
55
+ "spec/phocoder/http_spec.rb",
56
+ "spec/phocoder/job_spec.rb",
57
+ "spec/phocoder/phocoder_spec.rb",
58
+ "spec/phocoder/response_spec.rb",
59
+ "spec/spec_helper.rb"
60
+ ]
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 3
65
+
66
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
67
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
68
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
69
+ s.add_runtime_dependency(%q<builder>, [">= 0"])
70
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
71
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
72
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
73
+ s.add_development_dependency(%q<rcov>, [">= 0"])
74
+ s.add_development_dependency(%q<mocha>, [">= 0"])
75
+ s.add_development_dependency(%q<webmock>, ["~> 1.6.0"])
76
+ else
77
+ s.add_dependency(%q<activesupport>, [">= 0"])
78
+ s.add_dependency(%q<i18n>, [">= 0"])
79
+ s.add_dependency(%q<builder>, [">= 0"])
80
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
81
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
82
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
83
+ s.add_dependency(%q<rcov>, [">= 0"])
84
+ s.add_dependency(%q<mocha>, [">= 0"])
85
+ s.add_dependency(%q<webmock>, ["~> 1.6.0"])
86
+ end
87
+ else
88
+ s.add_dependency(%q<activesupport>, [">= 0"])
89
+ s.add_dependency(%q<i18n>, [">= 0"])
90
+ s.add_dependency(%q<builder>, [">= 0"])
91
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
92
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
93
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
94
+ s.add_dependency(%q<rcov>, [">= 0"])
95
+ s.add_dependency(%q<mocha>, [">= 0"])
96
+ s.add_dependency(%q<webmock>, ["~> 1.6.0"])
97
+ end
98
+ end
99
+
@@ -0,0 +1,100 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "Phocoder::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.expects(:timeout).with(0.001)
9
+ Phocoder::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.stubs(:timeout).raises(Exception)
15
+ lambda { Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
43
+ ::Net::HTTP.expects(:new).returns(http_stub)
44
+ Phocoder::HTTP::NetHTTP.any_instance.expects(:locate_root_cert_path).returns('/fake/path')
45
+ Phocoder::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.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
51
+ ::Net::HTTP.expects(:new).returns(http_stub)
52
+ Phocoder::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.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
58
+ ::Net::HTTP.expects(:new).returns(http_stub)
59
+ Phocoder::HTTP::NetHTTP.any_instance.expects(:locate_root_cert_path).returns(nil)
60
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:post).
67
+ with('https://example.com',:body => '{}').
68
+ returns(Phocoder::Response.new)
69
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:put).
76
+ with('https://example.com',:body => '{}').
77
+ returns(Phocoder::Response.new)
78
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:get).
85
+ with('https://example.com').
86
+ returns(Phocoder::Response.new)
87
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:delete).
94
+ with('https://example.com').
95
+ returns(Phocoder::Response.new)
96
+ Phocoder::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 "Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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 "Phocoder::HTTP" do
4
+
5
+ it "should have a default_options hash" do
6
+ Phocoder::HTTP.default_options.is_a?(Hash).should be_true
7
+ end
8
+
9
+ it "should have a default HTTP backend" do
10
+ Phocoder::HTTP.http_backend.should_not be_nil
11
+ end
12
+
13
+
14
+ it "should allow the changing of the HTTP backend" do
15
+ Phocoder::HTTP.http_backend.should_not == Phocoder::HTTP::Typhoeus
16
+ lambda { Phocoder::HTTP.http_backend = Phocoder::HTTP::Typhoeus }.should_not raise_error(Exception)
17
+ Phocoder::HTTP.http_backend.should == Phocoder::HTTP::Typhoeus
18
+ end
19
+
20
+ it "should raise a Phocoder::HTTPError when there is an HTTP error" do
21
+ Phocoder::HTTP.http_backend.expects(:get).
22
+ with('https://example.com', Phocoder::HTTP.default_options).
23
+ at_least_once.
24
+ raises(Errno::ECONNREFUSED)
25
+ lambda { Phocoder::HTTP.get('https://example.com') }.should raise_error(Phocoder::HTTPError)
26
+
27
+ begin
28
+ Phocoder::HTTP.get('https://example.com')
29
+ rescue Phocoder::HTTPError => e
30
+ e.backtrace.first.should_not =~ /perform_method/
31
+ end
32
+ end
33
+
34
+ it "should return a Phocoder::Response" do
35
+ Phocoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}'))
36
+ Phocoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": "hash"}'))
37
+ Phocoder::HTTP.http_backend.stubs(:get).returns(stub(:code => 200, :body => '{"some": "hash"}'))
38
+ Phocoder::HTTP.http_backend.stubs(:delete).returns(stub(:code => 200, :body => '{"some": "hash"}'))
39
+
40
+ Phocoder::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Phocoder::Response).should be_true
41
+ Phocoder::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Phocoder::Response).should be_true
42
+ Phocoder::HTTP.get('https://example.com').is_a?(Phocoder::Response).should be_true
43
+ Phocoder::HTTP.delete('https://example.com').is_a?(Phocoder::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
+ Phocoder::HTTP.http_backend.stubs(:post).returns(post_stub)
49
+ Phocoder::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
+ Phocoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}'))
54
+ Phocoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_body.should == '{"some": "hash"}'
55
+ end
56
+
57
+ it "should store the response code" do
58
+ Phocoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}'))
59
+ Phocoder::HTTP.post('https://example.com', '{"some": "hash"}').code.should == 200
60
+ end
61
+
62
+ it "should JSON parse the response body" do
63
+ Phocoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": "hash"}'))
64
+ Phocoder::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
+ Phocoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": bad json'))
69
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:post).
75
+ with('https://example.com', Phocoder::HTTP.default_options.merge(:body => '{}')).
76
+ returns(Phocoder::Response.new)
77
+
78
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:put).
85
+ with('https://example.com', Phocoder::HTTP.default_options.merge(:body => '{}')).
86
+ returns(Phocoder::Response.new)
87
+
88
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:get).
95
+ with('https://example.com', Phocoder::HTTP.default_options).
96
+ returns(Phocoder::Response.new)
97
+
98
+ Phocoder::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
+ Phocoder::HTTP.http_backend.expects(:delete).
105
+ with('https://example.com', Phocoder::HTTP.default_options).
106
+ returns(Phocoder::Response.new)
107
+
108
+ Phocoder::HTTP.delete('https://example.com').should_not be_nil
109
+ end
110
+ end
111
+
112
+
113
+
114
+
115
+
116
+
117
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Phocoder::Job" do
4
+
5
+ before(:each) do
6
+ @api_key = 'abc123'
7
+ end
8
+
9
+ describe ".create" do
10
+ before(:each) do
11
+ @url = "#{Phocoder.base_url}/jobs"
12
+ @params = {:api_key => @api_key,:input => "s3://bucket-name/file-name.avi" }
13
+ @params_as_json = Phocoder::Base.encode(@params, :json)
14
+ @params_as_xml = Phocoder::Base.encode(@params, :xml)
15
+ end
16
+
17
+ it "should POST to the correct url and return a response" do
18
+ Phocoder::HTTP.expects(:post).with(@url, @params_as_json, {}).returns(Phocoder::Response.new)
19
+ r = Phocoder::Job.create(@params)
20
+ r.class.should == Phocoder::Response
21
+ end
22
+
23
+ it "should apply the global API key when JSON and no api_key is passed" do
24
+ Phocoder.api_key = 'asdfasdf'
25
+ Phocoder::HTTP.expects(:post).with(@url,@params_as_json,{}) do |url, params, options|
26
+ Phocoder::Base.decode(params)['api_key'].should == Phocoder.api_key
27
+ end.returns(Phocoder::Response.new)
28
+ Phocoder::Job.create(:input => @params[:input])
29
+ Phocoder.api_key = nil
30
+ end
31
+
32
+ it "should apply the global API key when XML and no api_key is passed" do
33
+ Phocoder.api_key = 'asdfasdf'
34
+ Phocoder::HTTP.expects(:post).with(@url,@params_as_xml,{}) do |url, params, options|
35
+ Phocoder::Base.decode(params, :xml)['api_request']['api_key'].should == Phocoder.api_key
36
+ end.returns(Phocoder::Response.new)
37
+ Phocoder::Job.create({:api_request => {:input => @params[:input]}}, {:format => :xml})
38
+ Phocoder.api_key = nil
39
+ end
40
+
41
+ it "should apply the global API key when an XML string is passed and no api_key is passed" do
42
+ Phocoder.api_key = 'asdfasdf'
43
+ Phocoder::HTTP.expects(:post).with(@url,@params_as_xml,{}) do |url, params, options|
44
+ Phocoder::Base.decode(params, :xml)['api_request']['api_key'] == Phocoder.api_key
45
+ end.returns(Phocoder::Response.new)
46
+ Phocoder::Job.create({:input => @params[:input]}.to_xml(:root => :api_request), {:format => :xml})
47
+ Phocoder.api_key = nil
48
+ end
49
+
50
+ end
51
+
52
+
53
+ describe ".list" do
54
+ before(:each) do
55
+ @url = "#{Phocoder.base_url}/jobs"
56
+ end
57
+
58
+ it "should GET the correct url and return a response" do
59
+ Phocoder::HTTP.stubs(:get).with(@url, {:params => {:api_key => @api_key,
60
+ :page => 1,
61
+ :per_page => 50,
62
+ :state => nil}}).returns(Phocoder::Response.new)
63
+ Phocoder::Response.should == Phocoder::Job.list(:api_key => @api_key).class
64
+ end
65
+
66
+ it "should merge params well" do
67
+ Phocoder::HTTP.stubs(:get).with(@url, {:params => {:api_key => @api_key,
68
+ :page => 1,
69
+ :per_page => 50,
70
+ :some => 'param',
71
+ :state => nil}}).returns(Phocoder::Response.new)
72
+ Phocoder::Response.should == Phocoder::Job.list(:api_key => @api_key, :params => {:some => 'param'}).class
73
+ end
74
+ end
75
+
76
+ describe ".details" do
77
+ before(:each) do
78
+ @job_id = 1
79
+ @url = "#{Phocoder.base_url}/jobs/#{@job_id}"
80
+ end
81
+
82
+ it "should GET the correct url and return a response" do
83
+ Phocoder::HTTP.stubs(:get).with(@url, {:params => {:api_key => @api_key}}).returns(Phocoder::Response.new)
84
+ Phocoder::Response.should == Phocoder::Job.details(1, :api_key => @api_key).class
85
+ end
86
+ end
87
+
88
+ describe ".resubmit" do
89
+ before(:each) do
90
+ @job_id = 1
91
+ @url = "#{Phocoder.base_url}/jobs/#{@job_id}/resubmit"
92
+ end
93
+
94
+ it "should GET the correct url and return a response" do
95
+ Phocoder::HTTP.stubs(:get).with(@url, {:params => {:api_key => @api_key}}).returns(Phocoder::Response.new)
96
+ Phocoder::Response.should == Phocoder::Job.resubmit(1, :api_key => @api_key).class
97
+ end
98
+ end
99
+
100
+ describe ".cancel" do
101
+ before(:each) do
102
+ @job_id = 1
103
+ @url = "#{Phocoder.base_url}/jobs/#{@job_id}/cancel"
104
+ end
105
+
106
+ it "should GET the correct url and return a response" do
107
+ Phocoder::HTTP.stubs(:get).with(@url, {:params => {:api_key => @api_key}}).returns(Phocoder::Response.new)
108
+ Phocoder::Response.should == Phocoder::Job.cancel(1, :api_key => @api_key).class
109
+ end
110
+ end
111
+
112
+ describe ".delete" do
113
+ before(:each) do
114
+ @job_id = 1
115
+ @url = "#{Phocoder.base_url}/jobs/#{@job_id}"
116
+ end
117
+
118
+ it "should DELETE the correct url and return a response" do
119
+ Phocoder::HTTP.stubs(:delete).with(@url, {:params => {:api_key => @api_key}}).returns(Phocoder::Response.new)
120
+ Phocoder::Response.should == Phocoder::Job.delete(1, :api_key => @api_key).class
121
+ end
122
+ end
123
+
124
+
125
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Phocoder" do
4
+
5
+
6
+ it "should allow ENV variable to set an api key" do
7
+ Phocoder.api_key = nil
8
+ ENV['PHOCODER_API_KEY'] = "envkey"
9
+ Phocoder.api_key.should == "envkey"
10
+ Phocoder::Job.api_key.should == "envkey"
11
+ end
12
+
13
+ it "should allow user to set API key" do
14
+ Phocoder.api_key = "testkey"
15
+ Phocoder.api_key.should == "testkey"
16
+ Phocoder::Job.api_key.should == "testkey"
17
+ end
18
+
19
+ it "should take user-supplie api key over ENV-supplied key" do
20
+ Phocoder.api_key = "testkey"
21
+ ENV['PHOCODER_API_KEY'] = "envkey"
22
+ Phocoder.api_key.should == "testkey"
23
+ Phocoder::Job.api_key.should == "testkey"
24
+ end
25
+
26
+ it "should encode to json" do
27
+ Phocoder::Base.encode({:api_request => {:input => 'https://example.com'}}, :json).should =~ /"api_request"/
28
+ end
29
+
30
+ it "should encode to xml" do
31
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::Base.encode("api_request").should =~ /^api_request$/
44
+ end
45
+
46
+ it "should decode from xml" do
47
+ Phocoder::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
+ Phocoder::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
+ Phocoder::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
+ Phocoder::Base.decode(1).should == 1
60
+ end
61
+
62
+ it "should have a base url" do
63
+ Phocoder::Base.base_url.should_not be_nil
64
+ end
65
+
66
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Phocoder::Response" do
4
+
5
+
6
+ describe "#success?" do
7
+ it "should return true when code is between 200 and 299" do
8
+ Phocoder::Response.new(:code => 200).success?.should be_true
9
+ Phocoder::Response.new(:code => 299).success?.should be_true
10
+ Phocoder::Response.new(:code => 250).success?.should be_true
11
+ end
12
+
13
+ it "should return false when code it less than 200 or greater than 299" do
14
+ Phocoder::Response.new(:code => 300).success?.should_not be_true
15
+ Phocoder::Response.new(:code => 199).success?.should_not be_true
16
+ end
17
+ end
18
+
19
+ describe "#errors" do
20
+ it "should return an empty array when body is not a Hash" do
21
+ Phocoder::Response.new(:body => 1).errors.should == []
22
+ Phocoder::Response.new(:body => "something").errors.should == []
23
+ Phocoder::Response.new(:body => [1]).errors.should == []
24
+ end
25
+
26
+ it "should return the value of the key 'errors' as a compacted array when body is a Hash" do
27
+ Phocoder::Response.new(:body => {'errors' => ['must be awesome']}).errors.should == ['must be awesome']
28
+ Phocoder::Response.new(:body => {'errors' => 'must be awesome'}).errors.should == ['must be awesome']
29
+ Phocoder::Response.new(:body => {'errors' => ['must be awesome', nil]}).errors.should == ['must be awesome']
30
+ Phocoder::Response.new(:body => {}).errors.should == []
31
+ end
32
+ end
33
+
34
+ describe "#body_without_wrapper" do
35
+ it "should return the body when the body is a string" do
36
+ Phocoder::Response.new(:body => "some text").body_without_wrapper.should == "some text"
37
+ end
38
+
39
+ it "should return the body when the body is not wrapped in api_response and is a hash" do
40
+ Phocoder::Response.new(:body => {'some' => 'hash'}).body_without_wrapper.should == {'some' => 'hash'}
41
+ end
42
+
43
+ it "should return body['api_response'] when body is a hash and body['api_response'] exists" do
44
+ Phocoder::Response.new(:body => {'api_response' => {'some' => 'hash'}}).body_without_wrapper.should == {'some' => 'hash'}
45
+ end
46
+ end
47
+
48
+
49
+ end