tddium_client 0.0.4 → 0.0.5

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.
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ nbproject
6
6
  *.swp
7
7
  coverage/
8
8
  .rvmrc
9
+ doc/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tddium_client (0.0.4)
4
+ tddium_client (0.0.5)
5
5
  httparty
6
6
  json
7
7
 
@@ -12,13 +12,29 @@ module TddiumClient
12
12
 
13
13
  class Error < RuntimeError; end
14
14
  class TimeoutError < TddiumClient::Error; end
15
-
15
+
16
16
  class APIError < TddiumClient::Error
17
17
  attr_accessor :tddium_result
18
18
 
19
19
  def initialize(result)
20
20
  self.tddium_result = result
21
21
  end
22
+
23
+ def http_code
24
+ self.tddium_result.http_code if self.tddium_result
25
+ end
26
+
27
+ def http_result
28
+ self.tddium_result.http_result if self.tddium_result
29
+ end
30
+
31
+ def to_s
32
+ "#{http_code} #{http_result} (#{self.tddium_result.status}) #{self.tddium_result.explanation}"
33
+ end
34
+
35
+ def message
36
+ "API Error: #{to_s}"
37
+ end
22
38
  end
23
39
 
24
40
  class ServerError < TddiumClient::Error
@@ -28,6 +44,14 @@ module TddiumClient
28
44
  self.http_code = http_code
29
45
  self.http_result = http_result
30
46
  end
47
+
48
+ def to_s
49
+ "#{http_code} #{http_result}"
50
+ end
51
+
52
+ def message
53
+ "Server Error: #{to_s}"
54
+ end
31
55
  end
32
56
 
33
57
  class Result
@@ -40,15 +64,35 @@ module TddiumClient
40
64
  end
41
65
 
42
66
  def success?
43
- !response.nil? && response.is_a?(Hash) && response["status"] == 0
67
+ has_response? && response["status"] == 0
68
+ end
69
+
70
+ def has_response?
71
+ !response.nil? && response.is_a?(Hash)
72
+ end
73
+
74
+ def status
75
+ has_response? ? response["status"] : nil
76
+ end
77
+
78
+ def explanation
79
+ has_response? ? response["explanation"] : nil
44
80
  end
45
81
  end
46
82
 
47
83
  class Client
48
- attr_accessor :environment
84
+ attr_reader :environment
49
85
 
50
86
  def initialize(env = :development)
51
- self.environment = env
87
+ @all_config = YAML.load(File.read(config_path))
88
+ self.environment = env.to_s
89
+ end
90
+
91
+ def environment=(new_environment)
92
+ env = new_environment.to_s
93
+ raise ArgumentError, "Invalid environment #{env}" unless @all_config[env]
94
+ @tddium_config = @all_config[env]
95
+ @environment = env
52
96
  end
53
97
 
54
98
  def call_api(method, api_path, params = {}, api_key = nil, retries = 5)
@@ -83,17 +127,20 @@ module TddiumClient
83
127
 
84
128
  private
85
129
 
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
130
+ def tddium_uri(path)
131
+ uri = URI.parse("")
132
+ uri.host = tddium_config["api"]["host"]
133
+ uri.port = tddium_config["api"]["port"]
134
+ uri.scheme = tddium_config["api"]["scheme"]
135
+ URI.join(uri.to_s, "#{tddium_config["api"]["version"]}/#{path}").to_s
136
+ end
93
137
 
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
138
+ def config_path
139
+ File.join(File.dirname(__FILE__), "..", "config", "environment.yml")
140
+ end
141
+
142
+ def tddium_config
143
+ @tddium_config
144
+ end
98
145
  end
99
146
  end
@@ -3,5 +3,5 @@ Copyright (c) 2011 Solano Labs All Rights Reserved
3
3
  =end
4
4
 
5
5
  module TddiumClient
6
- VERSION = "0.0.4"
6
+ VERSION = "0.0.5"
7
7
  end
@@ -6,6 +6,23 @@ require "spec_helper"
6
6
 
7
7
  module TddiumClient
8
8
  describe Result do
9
+ describe "#has_response?" do
10
+ it "should be true with a reponse" do
11
+ result = TddiumClient::Result.new 200, "OK", {"status" => 0}
12
+ result.should be_has_response
13
+ end
14
+
15
+ it "should be false without a response" do
16
+ result = TddiumClient::Result.new 200, "OK", nil
17
+ result.should_not be_has_response
18
+ end
19
+
20
+ it "should be false with a nonhash response" do
21
+ result = TddiumClient::Result.new 200, "OK", "a"
22
+ result.should_not be_has_response
23
+ end
24
+ end
25
+
9
26
  describe "#success?" do
10
27
  context "with successful params" do
11
28
  it "should be true" do
@@ -28,6 +45,27 @@ module TddiumClient
28
45
  end
29
46
  end
30
47
 
48
+ describe ServerError do
49
+ it "should stringify with code and result" do
50
+ code = 404
51
+ message = 'Not Found'
52
+ e = TddiumClient::ServerError.new(code, message)
53
+ e.message.should =~ /#{code}/
54
+ e.message.should =~ /#{message}/
55
+ end
56
+ end
57
+
58
+ describe APIError do
59
+ it "should stringify with explanation" do
60
+ obj = {"status" => 1, "explanation"=>"oops"}
61
+ result = TddiumClient::Result.new(200, "OK", obj)
62
+ result.should_not be_success
63
+ e = TddiumClient::APIError.new(result)
64
+ e.message.should =~ /#{obj["status"]}/
65
+ e.message.should =~ /#{obj["explanation"]}/
66
+ end
67
+ end
68
+
31
69
  describe Client do
32
70
  include FakeFS::SpecHelpers
33
71
  include TddiumSpecHelpers
@@ -48,14 +86,21 @@ module TddiumClient
48
86
 
49
87
  let(:tddium_client) { TddiumClient::Client.new }
50
88
 
51
- it "should set the default environment to :development" do
52
- tddium_client.environment.should == :development
53
- end
54
-
55
89
  describe "#environment" do
90
+ before(:each) do
91
+ stub_tddium_client_config
92
+ end
93
+
94
+ it "should raise on init if environment can't be found" do
95
+ expect { TddiumClient::Client.new('foobar') }.to raise_error(ArgumentError)
96
+ end
97
+
98
+ it "should set the default environment to :development" do
99
+ tddium_client.environment.should == 'development'
100
+ end
56
101
  it "should set the environment" do
57
102
  tddium_client.environment = :production
58
- tddium_client.environment.should == :production
103
+ tddium_client.environment.should == 'production'
59
104
  end
60
105
  end
61
106
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tddium_client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jay Moorthi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-25 00:00:00 -07:00
13
+ date: 2011-03-28 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -112,6 +112,7 @@ extra_rdoc_files: []
112
112
 
113
113
  files:
114
114
  - .gitignore
115
+ - .rspec
115
116
  - Gemfile
116
117
  - Gemfile.lock
117
118
  - README