invisiblehand 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -71,11 +71,23 @@ module InvisibleHand
71
71
  query = url_params_from opts
72
72
  url = "#{@config[:protocol]}#{@config[:endpoint]}#{path}?#{query}"
73
73
 
74
- api_raw_request method, url
74
+ if opts[:debug]
75
+ debug { api_raw_request method, url }
76
+ else
77
+ api_raw_request method, url
78
+ end
75
79
  end
76
80
 
77
81
  private
78
82
 
83
+ def debug &block
84
+ old_log_level = logger.level
85
+ logger.level = ::Logger::DEBUG
86
+ result = block.call
87
+ logger.level = old_log_level
88
+ result
89
+ end
90
+
79
91
  def api_raw_request method, url
80
92
  logger.debug "API call URL: #{url}"
81
93
 
@@ -91,15 +103,21 @@ module InvisibleHand
91
103
  logger.debug "API call took #{elapsed.round(3)} seconds."
92
104
  logger.debug "API json response: #{json.inspect}"
93
105
 
94
- raise Error::APIError.new(json["error"], url) if json["error"]
106
+ raise Error::APIError.new(json["error"], url, response) if json["error"]
95
107
 
96
108
  json
97
109
  end
98
110
 
99
111
  def url_params_from hash
100
- hash.map do |key, value|
112
+ params = hash.map do |key, value|
113
+ # There are some parameters that will be passed to an API call that we
114
+ # don't want to include in the query string. Filter them out here.
115
+ next if [:debug].include? key.to_sym
116
+
101
117
  "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
102
- end.join("&")
118
+ end
119
+
120
+ params.compact.join('&')
103
121
  end
104
122
 
105
123
  def valid_config?
@@ -11,12 +11,13 @@ module InvisibleHand
11
11
  end
12
12
 
13
13
  class APIError < StandardError
14
- attr_accessor :url
14
+ attr_accessor :url, :raw_response
15
15
 
16
- def initialize message, url
16
+ def initialize message, url, raw_response
17
17
  super message
18
18
 
19
- @url = url
19
+ @url = url
20
+ @raw_response = raw_response
20
21
  end
21
22
  end
22
23
  end
@@ -1,5 +1,5 @@
1
1
  module InvisibleHand
2
2
  unless const_defined? :VERSION
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
data/spec/api_spec.rb CHANGED
@@ -34,6 +34,14 @@ describe InvisibleHand::API do
34
34
  end
35
35
  end
36
36
 
37
+ describe "ad-hoc debug flag" do
38
+ specify "the debug option to a single call should not break things" do
39
+ expect do
40
+ api.live_price(page["original_url"], :debug => true)
41
+ end.to_not raise_error
42
+ end
43
+ end
44
+
37
45
  describe "invalid config" do
38
46
  specify "no app_id or api_key should throw error" do
39
47
  expect do
@@ -59,4 +67,24 @@ describe InvisibleHand::API do
59
67
  end
60
68
  end
61
69
  end
70
+
71
+ describe "errors" do
72
+ describe InvisibleHand::Error::APIError do
73
+ subject do
74
+ error = nil
75
+
76
+ begin
77
+ api.live_price "not a real url"
78
+ rescue InvisibleHand::Error::APIError => e
79
+ error = e
80
+ end
81
+
82
+ error
83
+ end
84
+
85
+ its(:url) { should be_a String }
86
+ its(:raw_response) { should be_a String }
87
+ its(:message) { should be_a String }
88
+ end
89
+ end
62
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invisiblehand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: 1889337055537833862
113
+ hash: -3726614589269803000
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements:
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  segments:
121
121
  - 0
122
- hash: 1889337055537833862
122
+ hash: -3726614589269803000
123
123
  requirements: []
124
124
  rubyforge_project:
125
125
  rubygems_version: 1.8.24