tddium_client 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,6 +4,5 @@ pkg/*
4
4
  nbproject
5
5
  .DS_Store
6
6
  *.swp
7
- Gemfile.lock
8
7
  coverage/
9
8
  .rvmrc
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tddium_client (0.0.4)
5
+ httparty
6
+ json
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ crack (0.1.8)
12
+ diff-lcs (1.1.2)
13
+ fakefs (0.3.1)
14
+ fakeweb (1.3.0)
15
+ httparty (0.7.4)
16
+ crack (= 0.1.8)
17
+ json (1.5.1)
18
+ rack (1.2.2)
19
+ rack-test (0.5.7)
20
+ rack (>= 1.0)
21
+ rake (0.8.7)
22
+ rspec (2.5.0)
23
+ rspec-core (~> 2.5.0)
24
+ rspec-expectations (~> 2.5.0)
25
+ rspec-mocks (~> 2.5.0)
26
+ rspec-core (2.5.1)
27
+ rspec-expectations (2.5.0)
28
+ diff-lcs (~> 1.1.2)
29
+ rspec-mocks (2.5.0)
30
+ simplecov (0.4.1)
31
+ simplecov-html (~> 0.4.3)
32
+ simplecov-html (0.4.3)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ fakefs
39
+ fakeweb
40
+ rack-test
41
+ rake
42
+ rspec
43
+ simplecov
44
+ tddium_client!
data/Rakefile CHANGED
@@ -9,3 +9,25 @@ RSpec::Core::RakeTask.new(:spec) do |t|
9
9
  t.rspec_opts = "--color"
10
10
  end
11
11
  task :default => :spec
12
+
13
+ namespace :spec do
14
+ RUBY_VERSIONS = ["1.9.2-p180", "1.8.7-p302"]
15
+ GEMSET = "tddium_client"
16
+ desc "Runs the specs across Ruby 1.8.7 and 1.9.2"
17
+ task :xruby do
18
+ commands = []
19
+ gemsets = []
20
+ RUBY_VERSIONS.each do |ruby_version|
21
+ current_gemset = "ruby-#{ruby_version}@#{GEMSET}"
22
+ gemsets << current_gemset
23
+ commands << "rvm use #{ruby_version}" << "gem install bundler" << "bundle" << "rvm gemset create #{GEMSET}" <<
24
+ "rvm use #{current_gemset}" << "gem install bundler --no-rdoc --no-ri" << "bundle"
25
+ end
26
+ puts ""
27
+ puts "Attempting to run the specs across ruby #{RUBY_VERSIONS.join(" and ")}..."
28
+ puts "If you get an error, try running the following commands to get your environment set up:"
29
+ puts commands.join(" && ")
30
+ puts ""
31
+ Kernel.exec("rvm #{gemsets.join(",")} rake spec")
32
+ end
33
+ end
@@ -6,56 +6,94 @@ require 'rubygems'
6
6
  require 'httparty'
7
7
  require 'json'
8
8
 
9
- class TddiumClient
9
+ module TddiumClient
10
10
  API_KEY_HEADER = "X-tddium-api-key"
11
11
  API_ERROR_TEXT = "An error occured: "
12
12
 
13
- attr_accessor :environment
13
+ class Error < RuntimeError; end
14
+ class TimeoutError < TddiumClient::Error; end
15
+
16
+ class APIError < TddiumClient::Error
17
+ attr_accessor :tddium_result
14
18
 
15
- def initialize(env = :development)
16
- self.environment = env
19
+ def initialize(result)
20
+ self.tddium_result = result
21
+ end
17
22
  end
18
23
 
19
- def call_api(method, api_path, params = {}, api_key = nil, retries = 5, &block)
20
- headers = { API_KEY_HEADER => api_key } if api_key
21
-
22
- done = false
23
- tries = 0
24
- while (retries < 0 || tries <= retries) && !done
25
- begin
26
- http = HTTParty.send(method, tddium_uri(api_path), :body => params, :headers => headers)
27
- done = true
28
- rescue Timeout::Error
29
- ensure
30
- tries += 1
31
- end
24
+ class ServerError < TddiumClient::Error
25
+ attr_accessor :http_code, :http_result
26
+
27
+ def initialize(http_code, http_result)
28
+ self.http_code = http_code
29
+ self.http_result = http_result
32
30
  end
31
+ end
33
32
 
34
- raise Timeout::Error if tries > retries
33
+ class Result
34
+ attr_accessor :http_code, :http_result, :response
35
35
 
36
- response = JSON.parse(http.body) rescue {}
36
+ def initialize(http_code, http_result, response)
37
+ self.http_code = http_code
38
+ self.http_result = http_result
39
+ self.response = response
40
+ end
37
41
 
38
- if http.success?
39
- (response["status"] == 0 && block_given?) ? yield(response) : message = API_ERROR_TEXT + response["explanation"].to_s
40
- else
41
- message = API_ERROR_TEXT + http.response.header.msg.to_s
42
- message << " #{response["explanation"]}" if response["status"].to_i > 0
42
+ def success?
43
+ !response.nil? && response.is_a?(Hash) && response["status"] == 0
43
44
  end
44
- [response["status"], http.code, message]
45
45
  end
46
46
 
47
- private
47
+ class Client
48
+ attr_accessor :environment
48
49
 
49
- def tddium_uri(path)
50
- uri = URI.parse("")
51
- uri.host = tddium_config["api"]["host"]
52
- uri.port = tddium_config["api"]["port"]
53
- uri.scheme = tddium_config["api"]["scheme"]
54
- URI.join(uri.to_s, "#{tddium_config["api"]["version"]}/#{path}").to_s
55
- end
50
+ def initialize(env = :development)
51
+ self.environment = env
52
+ end
53
+
54
+ def call_api(method, api_path, params = {}, api_key = nil, retries = 5)
55
+ headers = { API_KEY_HEADER => api_key } if api_key
56
+
57
+ done = false
58
+ tries = 0
59
+ while (retries < 0 || tries <= retries) && !done
60
+ begin
61
+ http = HTTParty.send(method, tddium_uri(api_path), :body => params, :headers => headers)
62
+ done = true
63
+ rescue Timeout::Error
64
+ ensure
65
+ tries += 1
66
+ end
67
+ end
68
+
69
+ raise TimeoutError if tries > retries
56
70
 
57
- def tddium_config
58
- @tddium_config = YAML.load(File.read(File.join(File.dirname(__FILE__), "..", "config", "environment.yml")))[environment.to_s] unless @tddium_config
59
- @tddium_config
71
+ response = JSON.parse(http.body) rescue {}
72
+
73
+ http_message = http.response.header.msg.to_s
74
+
75
+ raise ServerError.new(http.code, http_message) unless response.is_a?(Hash) && response.include?("status")
76
+
77
+ result = Result.new(http.code, http.response.header.msg.to_s, response)
78
+
79
+ raise APIError.new(result) if !result.success?
80
+
81
+ result
82
+ end
83
+
84
+ private
85
+
86
+ def tddium_uri(path)
87
+ uri = URI.parse("")
88
+ uri.host = tddium_config["api"]["host"]
89
+ uri.port = tddium_config["api"]["port"]
90
+ uri.scheme = tddium_config["api"]["scheme"]
91
+ URI.join(uri.to_s, "#{tddium_config["api"]["version"]}/#{path}").to_s
92
+ end
93
+
94
+ def tddium_config
95
+ @tddium_config = YAML.load(File.read(File.join(File.dirname(__FILE__), "..", "config", "environment.yml")))[environment.to_s] unless @tddium_config
96
+ @tddium_config
97
+ end
60
98
  end
61
99
  end
@@ -2,6 +2,6 @@
2
2
  Copyright (c) 2011 Solano Labs All Rights Reserved
3
3
  =end
4
4
 
5
- module TddiumClientVersion
6
- VERSION = "0.0.3"
5
+ module TddiumClient
6
+ VERSION = "0.0.4"
7
7
  end
@@ -4,202 +4,229 @@ Copyright (c) 2011 Solano Labs All Rights Reserved
4
4
 
5
5
  require "spec_helper"
6
6
 
7
- describe TddiumClient do
8
- include FakeFS::SpecHelpers
9
- include TddiumSpecHelpers
10
-
11
- EXAMPLE_HTTP_METHOD = :post
12
- EXAMPLE_TDDIUM_RESOURCE = "suites"
13
- EXAMPLE_PARAMS = {"key" => "value"}
14
- EXAMPLE_API_KEY = "afb12412bdafe124124asfasfabebafeabwbawf1312342erbfasbb"
15
-
16
- def stub_http_response(method, path, options = {})
17
- uri = api_uri(path)
18
- FakeWeb.register_uri(method, uri, register_uri_options(options))
19
- end
7
+ module TddiumClient
8
+ describe Result do
9
+ describe "#success?" do
10
+ context "with successful params" do
11
+ it "should be true" do
12
+ result = TddiumClient::Result.new 200, "OK", {"status" => 0}
13
+ result.should be_success
14
+ end
15
+ end
16
+
17
+ context "with unsuccessful params" do
18
+ it "should handle no response" do
19
+ result = TddiumClient::Result.new 200, "OK", nil
20
+ result.should_not be_success
21
+ end
20
22
 
21
- def parse_request_params
22
- Rack::Utils.parse_nested_query(FakeWeb.last_request.body)
23
+ it "should handle 5xx" do
24
+ result = TddiumClient::Result.new 501, "Internal Server Error", {"status" => 1}
25
+ result.should_not be_success
26
+ end
27
+ end
28
+ end
23
29
  end
24
30
 
25
- let(:tddium_client) { TddiumClient.new }
31
+ describe Client do
32
+ include FakeFS::SpecHelpers
33
+ include TddiumSpecHelpers
26
34
 
27
- it "should set the default environment to :development" do
28
- tddium_client.environment.should == :development
29
- end
35
+ EXAMPLE_HTTP_METHOD = :post
36
+ EXAMPLE_TDDIUM_RESOURCE = "suites"
37
+ EXAMPLE_PARAMS = {"key" => "value"}
38
+ EXAMPLE_API_KEY = "afb12412bdafe124124asfasfabebafeabwbawf1312342erbfasbb"
30
39
 
31
- describe "#environment" do
32
- it "should set the environment" do
33
- tddium_client.environment = :production
34
- tddium_client.environment.should == :production
40
+ def stub_http_response(method, path, options = {})
41
+ uri = api_uri(path)
42
+ FakeWeb.register_uri(method, uri, register_uri_options(options))
35
43
  end
36
- end
37
44
 
38
- describe "#call_api" do
39
- before do
40
- FakeWeb.clean_registry
41
- stub_tddium_client_config
42
- stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
45
+ def parse_request_params
46
+ Rack::Utils.parse_nested_query(FakeWeb.last_request.body)
43
47
  end
44
48
 
45
- context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}')" do
46
- it "should make a '#{EXAMPLE_HTTP_METHOD.to_s.upcase}' request to the api" do
47
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
48
- FakeWeb.last_request.method.downcase.to_sym.should == EXAMPLE_HTTP_METHOD
49
- end
49
+ let(:tddium_client) { TddiumClient::Client.new }
50
50
 
51
- it "should make a request to the correct resource" do
52
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
53
- FakeWeb.last_request.path.should =~ /#{EXAMPLE_TDDIUM_RESOURCE}$/
54
- end
51
+ it "should set the default environment to :development" do
52
+ tddium_client.environment.should == :development
55
53
  end
56
54
 
57
- context "raises an error" do
58
- before do
59
- HTTParty.stub(EXAMPLE_HTTP_METHOD).and_raise(Timeout::Error)
55
+ describe "#environment" do
56
+ it "should set the environment" do
57
+ tddium_client.environment = :production
58
+ tddium_client.environment.should == :production
60
59
  end
60
+ end
61
61
 
62
- it "should retry 5 times by default to contact the API" do
63
- HTTParty.should_receive(EXAMPLE_HTTP_METHOD).exactly(6).times
64
- expect { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE) }.to raise_error(Timeout::Error)
62
+ describe "#call_api" do
63
+ before(:each) do
64
+ FakeWeb.clean_registry
65
+ stub_tddium_client_config
66
+ stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :response => fixture_path("post_suites_201.json"))
65
67
  end
66
68
 
67
- it "should retry as many times as we want to contact the API" do
68
- HTTParty.should_receive(EXAMPLE_HTTP_METHOD).exactly(3).times
69
- expect { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil, 2) }.to raise_error(Timeout::Error)
69
+ context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}')" do
70
+ it "should make a '#{EXAMPLE_HTTP_METHOD.to_s.upcase}' request to the api" do
71
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
72
+ FakeWeb.last_request.method.downcase.to_sym.should == EXAMPLE_HTTP_METHOD
73
+ end
74
+
75
+ it "should make a request to the correct resource" do
76
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
77
+ FakeWeb.last_request.path.should =~ /#{EXAMPLE_TDDIUM_RESOURCE}$/
78
+ end
70
79
  end
71
- end
72
80
 
73
- context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}', {}, #{EXAMPLE_API_KEY}) # with api_key" do
74
- it "should include #{TddiumClient::API_KEY_HEADER}=#{EXAMPLE_API_KEY} in the request headers" do
75
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, EXAMPLE_API_KEY)
76
- FakeWeb.last_request[TddiumClient::API_KEY_HEADER].should == EXAMPLE_API_KEY
81
+ context "raises a timeout error" do
82
+ before do
83
+ HTTParty.stub(EXAMPLE_HTTP_METHOD).and_raise(Timeout::Error)
84
+ end
85
+
86
+ it "should retry 5 times by default to contact the API" do
87
+ HTTParty.should_receive(EXAMPLE_HTTP_METHOD).exactly(6).times
88
+ expect { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE) }.to raise_error(TddiumClient::TimeoutError)
89
+ end
90
+
91
+ it "should retry as many times as we want to contact the API" do
92
+ HTTParty.should_receive(EXAMPLE_HTTP_METHOD).exactly(3).times
93
+ expect { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil, 2) }.to raise_error(TddiumClient::TimeoutError)
94
+ end
77
95
  end
78
- end
79
96
 
80
- context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}') # without api_key" do
81
- it "should not include #{TddiumClient::API_KEY_HEADER} in the request headers" do
82
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {})
83
- FakeWeb.last_request[TddiumClient::API_KEY_HEADER].should be_nil
97
+ context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}', {}, #{EXAMPLE_API_KEY}) # with api_key" do
98
+ it "should include #{TddiumClient::API_KEY_HEADER}=#{EXAMPLE_API_KEY} in the request headers" do
99
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, EXAMPLE_API_KEY)
100
+ FakeWeb.last_request[TddiumClient::API_KEY_HEADER].should == EXAMPLE_API_KEY
101
+ end
84
102
  end
85
- end
86
103
 
87
- context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}', #{EXAMPLE_PARAMS}) # with params" do
88
- it "should include #{EXAMPLE_PARAMS} in the request params" do
89
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, EXAMPLE_PARAMS)
90
- parse_request_params.should include(EXAMPLE_PARAMS)
104
+ context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}') # without api_key" do
105
+ it "should not include #{TddiumClient::API_KEY_HEADER} in the request headers" do
106
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {})
107
+ FakeWeb.last_request[TddiumClient::API_KEY_HEADER].should be_nil
108
+ end
91
109
  end
92
- end
93
110
 
94
- context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}') # without params" do
95
- it "should not include any request params" do
96
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
97
- parse_request_params.should == {}
111
+ context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}', #{EXAMPLE_PARAMS}) # with params" do
112
+ it "should include #{EXAMPLE_PARAMS} in the request params" do
113
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, EXAMPLE_PARAMS)
114
+ parse_request_params.should include(EXAMPLE_PARAMS)
115
+ end
98
116
  end
99
- end
100
-
101
- context "results in a successful response" do
102
- before do
103
- stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :response => fixture_path("post_suites_201.json"))
117
+
118
+ context "('#{EXAMPLE_HTTP_METHOD}', '#{EXAMPLE_TDDIUM_RESOURCE}') # without params" do
119
+ it "should not include any request params" do
120
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
121
+ parse_request_params.should == {}
122
+ end
104
123
  end
105
124
 
106
- context "called with a block" do
107
- let(:dummy_block) { Proc.new { |response| @parsed_http_response = response } }
125
+ context "results in a successful response" do
126
+ before do
127
+ stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :response => fixture_path("post_suites_201.json"))
128
+ end
108
129
 
109
130
  it "should try to contact the api only once" do
110
131
  HTTParty.should_receive(EXAMPLE_HTTP_METHOD).exactly(1).times.and_return(mock(HTTParty).as_null_object)
111
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil, &dummy_block)
132
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil) rescue {}
112
133
  end
113
134
 
114
- it "should parse the JSON response" do
115
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil, &dummy_block)
116
- @parsed_http_response.should be_a(Hash)
117
- @parsed_http_response["status"].should == 0
118
- @parsed_http_response["suite"]["id"].should == 19
135
+ it "should return a TddiumClient::Result" do
136
+ result = tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil)
137
+ result.should be_a(TddiumClient::Result)
119
138
  end
120
139
 
121
- it "should return a triple with element[0]==json_status, element[1]==http_status, element[2]==error_message" do
122
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil, &dummy_block).should == [0, 201, nil]
140
+ it "should parse the JSON response" do
141
+ result = tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, {}, nil)
142
+ result.http_code.should == 201
143
+ result.response.should be_a(Hash)
144
+ result.response["status"].should == 0
145
+ result.should be_success
146
+ result.response["suite"]["id"].should == 19
123
147
  end
124
148
  end
125
149
 
126
- context "called without a block" do
127
- it "should not yield" do
128
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
150
+ context "exceptions for an unprocessable response" do
151
+ before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :status => ["501", "Internal Server Error"]) }
152
+ it "should raise a TddiumClient::ServerError" do
153
+ expect do
154
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
155
+ end.to raise_error do |error|
156
+ puts error.inspect
157
+ error.should be_a(TddiumClient::ServerError)
158
+ error.should respond_to(:http_code)
159
+ error.http_code.should == 500
160
+ error.http_message.should =~ /Internal Server Error/
161
+ end
129
162
  end
130
- end
131
- end
132
-
133
- context "results for an unsuccessful response" do
134
- before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :status => ["404", "Not Found"]) }
135
163
 
136
- shared_examples_for "returning that an error occured" do
137
- it "should return that an error occured in the third element" do
138
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[2].should =~ /^#{TddiumClient::API_ERROR_TEXT}/
164
+ context "when no response is present" do
165
+ before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :status => ["200", "OK"]) }
166
+ it "should raise a TddiumClient::ServerError" do
167
+ expect do
168
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
169
+ end.to raise_error do |error|
170
+ error.should be_a(TddiumClient::ServerError)
171
+ error.should respond_to(:http_code)
172
+ error.http_code.should == 200
173
+ error.http_message.should =~ /OK/
174
+ end
175
+ end
139
176
  end
140
177
  end
141
178
 
142
- shared_examples_for("returning the API error") do
143
- it "should return the API error message in the third element" do
144
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[2].should =~ /\{\:suite_name\=\>\[\"has already been taken\"\]\}$/
179
+ shared_examples_for "raising an APIError" do
180
+ # users can set:
181
+ #
182
+ # aproc
183
+ # http_code
184
+ # http_message
185
+ it "should raise the right exception" do
186
+ expect { aproc.call }.to raise_error do |error|
187
+ error.should be_a(TddiumClient::APIError)
188
+ error.should respond_to(:tddium_result)
189
+ error.tddium_result.should be_a(TddiumClient::Result)
190
+ end
145
191
  end
146
- end
147
192
 
148
- shared_examples_for("returning the HTTP status code") do
149
- it "should return the HTTP status code in the second element" do
150
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[1].should == http_status_code
193
+ it "should capture the http response line" do
194
+ expect { aproc.call }.to raise_error do |error|
195
+ error.tddium_result.http_code.should == http_code if http_code
196
+ error.tddium_result.http_message.should == http_message if http_message
197
+ end
151
198
  end
152
199
  end
153
200
 
154
- it "should return an array with three elements" do
155
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE).size.should == 3
156
- end
157
-
158
201
  context "where the http request was successful but API status is not 0" do
159
202
  before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :response => fixture_path("post_suites_269_json_status_1.json")) }
160
- it_should_behave_like("returning that an error occured")
161
- it_should_behave_like("returning the API error")
162
-
163
- it "should return the API error code in the first element" do
164
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[0].should == 1
165
- end
166
203
 
167
- it_should_behave_like("returning the HTTP status code") do
168
- let(:http_status_code) {269}
204
+ it_should_behave_like "raising an APIError" do
205
+ let(:aproc) { Proc.new { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE) } }
206
+ let(:http_code) {269}
169
207
  end
170
208
  end
171
209
 
172
- context "where the http request was unsuccessful" do
173
- before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :status => ["501", "Internal Server Error"]) }
174
- it_should_behave_like("returning that an error occured")
175
-
176
- it_should_behave_like("returning the HTTP status code") do
177
- let(:http_status_code) {501}
178
- end
179
-
180
- it "should return the HTTP error message in the third element" do
181
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[2].should =~ /Internal Server Error/
210
+ context "exceptions for an error response" do
211
+ before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :status => ["404", "Not Found"]) }
212
+ it_should_behave_like "raising an APIError" do
213
+ let(:aproc) { Proc.new { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE) } }
214
+ let(:http_code) {404}
182
215
  end
183
216
 
184
217
  context "and an API error is returned" do
185
218
  before { stub_http_response(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE, :response => fixture_path("post_suites_409.json")) }
186
- it_should_behave_like("returning the API error")
187
- it "should return the API error code in the first element" do
188
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[0].should == 1
219
+ it_should_behave_like "raising an APIError" do
220
+ let(:aproc) { Proc.new { tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE) } }
221
+ let(:http_code) {409}
189
222
  end
190
223
 
191
- it_should_behave_like("returning the HTTP status code") do
192
- let(:http_status_code) {409}
193
- end
194
- end
195
-
196
- context "and no API error is returned" do
197
- it "should return a nil API error code in the first element" do
198
- tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)[0].should be_nil
199
- end
200
-
201
- it_should_behave_like("returning the HTTP status code") do
202
- let(:http_status_code) {501}
224
+ it "should have an api status" do
225
+ expect do
226
+ tddium_client.call_api(EXAMPLE_HTTP_METHOD, EXAMPLE_TDDIUM_RESOURCE)
227
+ end.to raise_error do |error|
228
+ error.tddium_result.response[:status].should == 1
229
+ end
203
230
  end
204
231
  end
205
232
  end
@@ -8,7 +8,7 @@ require "tddium_client/version"
8
8
 
9
9
  Gem::Specification.new do |s|
10
10
  s.name = "tddium_client"
11
- s.version = TddiumClientVersion::VERSION
11
+ s.version = TddiumClient::VERSION
12
12
  s.platform = Gem::Platform::RUBY
13
13
  s.authors = ["Jay Moorthi"]
14
14
  s.email = ["info@tddium.com"]
@@ -31,4 +31,5 @@ Gem::Specification.new do |s|
31
31
  s.add_development_dependency("fakefs")
32
32
  s.add_development_dependency("rack-test")
33
33
  s.add_development_dependency("simplecov")
34
+ s.add_development_dependency("rake")
34
35
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tddium_client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
4
+ prerelease:
5
+ version: 0.0.4
11
6
  platform: ruby
12
7
  authors:
13
8
  - Jay Moorthi
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-03-18 00:00:00 -07:00
13
+ date: 2011-03-25 00:00:00 -07:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,9 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
24
  version: "0"
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -40,9 +32,6 @@ dependencies:
40
32
  requirements:
41
33
  - - ">="
42
34
  - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
35
  version: "0"
47
36
  type: :runtime
48
37
  version_requirements: *id002
@@ -54,9 +43,6 @@ dependencies:
54
43
  requirements:
55
44
  - - ">="
56
45
  - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
46
  version: "0"
61
47
  type: :development
62
48
  version_requirements: *id003
@@ -68,9 +54,6 @@ dependencies:
68
54
  requirements:
69
55
  - - ">="
70
56
  - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
57
  version: "0"
75
58
  type: :development
76
59
  version_requirements: *id004
@@ -82,9 +65,6 @@ dependencies:
82
65
  requirements:
83
66
  - - ">="
84
67
  - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
68
  version: "0"
89
69
  type: :development
90
70
  version_requirements: *id005
@@ -96,9 +76,6 @@ dependencies:
96
76
  requirements:
97
77
  - - ">="
98
78
  - !ruby/object:Gem::Version
99
- hash: 3
100
- segments:
101
- - 0
102
79
  version: "0"
103
80
  type: :development
104
81
  version_requirements: *id006
@@ -110,12 +87,20 @@ dependencies:
110
87
  requirements:
111
88
  - - ">="
112
89
  - !ruby/object:Gem::Version
113
- hash: 3
114
- segments:
115
- - 0
116
90
  version: "0"
117
91
  type: :development
118
92
  version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: rake
95
+ prerelease: false
96
+ requirement: &id008 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id008
119
104
  description: Internal Gem used to call the Tddium API
120
105
  email:
121
106
  - info@tddium.com
@@ -128,6 +113,7 @@ extra_rdoc_files: []
128
113
  files:
129
114
  - .gitignore
130
115
  - Gemfile
116
+ - Gemfile.lock
131
117
  - README
132
118
  - Rakefile
133
119
  - config/environment.yml
@@ -154,23 +140,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
140
  requirements:
155
141
  - - ">="
156
142
  - !ruby/object:Gem::Version
157
- hash: 3
158
- segments:
159
- - 0
160
143
  version: "0"
161
144
  required_rubygems_version: !ruby/object:Gem::Requirement
162
145
  none: false
163
146
  requirements:
164
147
  - - ">="
165
148
  - !ruby/object:Gem::Version
166
- hash: 3
167
- segments:
168
- - 0
169
149
  version: "0"
170
150
  requirements: []
171
151
 
172
152
  rubyforge_project: tddium_client
173
- rubygems_version: 1.3.7
153
+ rubygems_version: 1.5.2
174
154
  signing_key:
175
155
  specification_version: 3
176
156
  summary: tddium Client Gem