etsy 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d8467d59605302ecbe6fa8a5c2c78fae8435b2b
4
- data.tar.gz: 9f78e9dd4514d505d5738b19ebc9724184ce6259
3
+ metadata.gz: c0417646d0a3bad8aac70e2fe86cf6904fd5803b
4
+ data.tar.gz: ce49ace0289086a486b2e1a0185232bb444653cb
5
5
  SHA512:
6
- metadata.gz: 80ab0008d0d4087c40b5045a9a38c46a50a4232b6d37e6062e162143641aad1893fc3ce1340f0e3825dabbcfc576312697f3600fd660331039bc1c001cb5418a
7
- data.tar.gz: f7e5fd820fe67797cb8c180ad5f91d58672d9fa7ccbe05cf5766480cfada9c72579dc4637b574badb099999ec9c49f03d736b4891472a01cbc75f9a9dad31fc4
6
+ metadata.gz: 41647ccad160295f3fd4143db22264b3f051c45b7d9254ceabcc8ae65ae130d571a3eae7f2500752fedbcb408d1b05e51e0966ba0f4643091ff211f384c4726d
7
+ data.tar.gz: a9d658cc135a7acdb17b55a086b3c65fdf378afb538c7fc7a4b97f2996d9369604f5dd7b78d95706c248e76649459b8b25b1422d7b3066df100dacdac65b5e6d
data/.travis.yml CHANGED
@@ -4,5 +4,4 @@ rvm:
4
4
  - 1.9.3
5
5
  - jruby-18mode
6
6
  - jruby-19mode
7
- - rbx-18mode
8
- - rbx-19mode
7
+ - rbx-2
data/README.md CHANGED
@@ -45,7 +45,6 @@ The Etsy API has support for both retrieval of extended information and write su
45
45
 
46
46
  For simple authentication from the console, configure the necessary parameters:
47
47
 
48
- require 'rubygems'
49
48
  require 'etsy'
50
49
 
51
50
  Etsy.api_key = 'key'
@@ -72,7 +71,6 @@ Authenticated calls can now be made by passing an access token and secret:
72
71
 
73
72
  The process for authenticating via a web application is similar, but requires the configuration of a callback URL:
74
73
 
75
- require 'rubygems'
76
74
  require 'etsy'
77
75
 
78
76
  Etsy.api_key = 'key'
@@ -102,14 +100,21 @@ to the verification URL. A simple example using Sinatra:
102
100
 
103
101
  ### Environment
104
102
 
105
- The Etsy API has both a sandbox environment and a production environment.
103
+ The Etsy API previously had both a sandbox environment and a production environment. They have recently eliminated the sandbox environment, but the ability to set the environment has been preserved in case it is implemented in v3.
106
104
 
107
- If nothing is set, the default is :sandbox.
105
+ If nothing is set, the default is :production.
108
106
 
109
107
  You can set this using:
110
108
 
111
109
  Etsy.environment = :production
112
110
 
111
+ ## Error handling
112
+
113
+ For legacy reasons, this gem does *not* raise errors when requests are unsuccessful.
114
+ However, you can force errors to be thrown by configuring the `silent_errors` flag.
115
+
116
+ >>> Etsy.silent_errors = false
117
+
113
118
  ## DSL
114
119
 
115
120
  Use the Etsy::Request class to make flexible calls to the API.
@@ -223,7 +228,7 @@ If you want a more fine-grained response, you can specify the associations as an
223
228
 
224
229
  ## Public mode vs authenticated calls
225
230
 
226
- This additional example should make clear the difference between issuing public versus authenticated requests:
231
+ This additional example should make clear the difference between issuing public versus authenticated requests:
227
232
 
228
233
  ### Public workflow
229
234
 
@@ -238,46 +243,7 @@ This additional example should make clear the difference between issuing public
238
243
  >> user = Etsy.myself(token, secret)
239
244
  >> access = { :access_token => user.token, :access_secret => user.secret }
240
245
  >> Etsy::Listing.find_all_by_shop_id(user.shop.id, access.merge(:limit => 5))
241
-
242
- ## Error handling
243
-
244
- Next versions of this gem will raise errors when requests are unsuccessful. The current version does not.
245
- Use either of following workarounds:
246
-
247
- ### Low-level API
248
-
249
- Instead of doing this:
250
-
251
- >> Etsy::Request.get('/users/__SELF__', access).result
252
-
253
- Write this:
254
-
255
- >> Etsy::Request.get('/users/__SELF__', access).to_hash["results"]
256
-
257
- ### Monkey patch
258
-
259
- This is Ruby, reopen the <code>Response</code> class anywhere in your codebase and redefine <code>result</code>:
260
-
261
- class Etsy::Response
262
- def result
263
- if success?
264
- results = to_hash['results'] || []
265
- count == 1 ? results.first : results
266
- else
267
- validate!
268
- end
269
- end
270
- end
271
-
272
- ### Usage
273
-
274
- With the above in place, you can now rescue errors and act upon them:
275
246
 
276
- begin
277
- Etsy.myself(access.token, access.secret)
278
- rescue Etsy::OAuthTokenRevoked, Etsy::InvalidUserID, Etsy::MissingShopID, Etsy::EtsyJSONInvalid, Etsy::TemporaryIssue => e
279
- puts e.message
280
- end
281
247
 
282
248
  ## Contributing
283
249
 
@@ -321,6 +287,7 @@ These people have helped make the Etsy gem what it is today:
321
287
  * [Jimmy Tang](https://github.com/jimmytang)
322
288
  * [Julio Santos](https://github.com/julio)
323
289
  * [Roger Smith](https://github.com/rogsmith)
290
+ * [Daniel Szmulewicz](https://github.com/danielsz)
324
291
 
325
292
  ### Github Flow
326
293
 
data/lib/etsy/response.rb CHANGED
@@ -2,11 +2,18 @@ module Etsy
2
2
 
3
3
  class OAuthTokenRevoked < StandardError; end
4
4
  class MissingShopID < StandardError; end
5
- class EtsyJSONInvalid < StandardError; end
6
5
  class TemporaryIssue < StandardError; end
7
6
  class ResourceUnavailable < TemporaryIssue; end
8
7
  class ExceededRateLimit < TemporaryIssue; end
9
8
  class InvalidUserID < StandardError; end
9
+ class EtsyJSONInvalid < StandardError
10
+ attr_reader :code, :data
11
+ def initialize(args)
12
+ @code = args[:code]
13
+ @data = args[:data]
14
+ end
15
+ end
16
+
10
17
 
11
18
  # = Response
12
19
  #
@@ -48,7 +55,7 @@ module Etsy
48
55
  results = to_hash['results'] || []
49
56
  count == 1 ? results.first : results
50
57
  else
51
- []
58
+ Etsy.silent_errors ? [] : validate!
52
59
  end
53
60
  end
54
61
 
@@ -82,7 +89,7 @@ module Etsy
82
89
  raise TemporaryIssue if temporary_etsy_issue?
83
90
  raise ResourceUnavailable if resource_unavailable?
84
91
  raise ExceededRateLimit if exceeded_rate_limit?
85
- raise EtsyJSONInvalid.new("CODE: #{code}, BODY: #{data}") unless valid_json?
92
+ raise EtsyJSONInvalid.new({:code => code, :data => data}) unless valid_json?
86
93
  true
87
94
  end
88
95
 
data/lib/etsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Etsy
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
data/lib/etsy.rb CHANGED
@@ -67,20 +67,24 @@ module Etsy
67
67
  attr_writer :permission_scopes
68
68
  end
69
69
 
70
- # Make Etsy.api_key and Etsy.api_secret thread safe
70
+ # Make Etsy.api_key and Etsy.api_secret global but also local to threads
71
71
  #
72
72
  def self.api_key
73
- Thread.current[:etsy_api_key]
73
+ Thread.current[:etsy_api_key] || @api_key
74
74
  end
75
75
 
76
- def self.api_key=(val)
77
- Thread.current[:etsy_api_key] = val
76
+ def self.api_key=(key)
77
+ @api_key ||= key
78
+ Thread.current[:etsy_api_key] = key
78
79
  end
80
+
79
81
  def self.api_secret
80
- Thread.current[:etsy_api_secret]
82
+ Thread.current[:etsy_api_secret] || @api_secret
81
83
  end
82
- def self.api_secret=(val)
83
- Thread.current[:etsy_api_secret] = val
84
+
85
+ def self.api_secret=(secret)
86
+ @api_secret ||= secret
87
+ Thread.current[:etsy_api_secret] = secret
84
88
  end
85
89
 
86
90
  SANDBOX_HOST = 'sandbox.openapi.etsy.com'
@@ -104,6 +108,15 @@ module Etsy
104
108
  @protocol = protocol.to_s
105
109
  end
106
110
 
111
+ # Allow throwing API errors
112
+ #
113
+ def self.silent_errors=(bool)
114
+ unless [TrueClass, FalseClass].include?(bool.class)
115
+ raise(ArgumentError, "Silent errors must be set to either true or false'")
116
+ end
117
+ @silent_errors = bool
118
+ end
119
+
107
120
  def self.protocol
108
121
  @protocol || "https"
109
122
  end
@@ -111,11 +124,17 @@ module Etsy
111
124
  # The currently configured environment.
112
125
  #
113
126
  def self.environment
114
- @environment || :sandbox
127
+ @environment || :production
128
+ end
129
+
130
+ # The default will change to false for a 1.0 release (breaking changes)
131
+ #
132
+ def self.silent_errors
133
+ @silent_errors.nil? ? true : @silent_errors
115
134
  end
116
135
 
117
136
  def self.host # :nodoc:
118
- @host || SANDBOX_HOST
137
+ @host || PRODUCTION_HOST
119
138
  end
120
139
 
121
140
  # The configured callback URL or 'oob' if no callback URL is configured. This controls
@@ -80,8 +80,9 @@ module Etsy
80
80
  raw_response.stubs(:body => "I am not JSON", :code => 500)
81
81
  r = Response.new(raw_response)
82
82
 
83
- lambda { r.to_hash }.should raise_error(Etsy::EtsyJSONInvalid)
84
- lambda { r.to_hash }.should raise_error("CODE: 500, BODY: I am not JSON")
83
+ exception = assert_raises(Etsy::EtsyJSONInvalid) { r.to_hash }
84
+ assert_equal( 500, exception.code )
85
+ assert_equal( "I am not JSON", exception.data )
85
86
  end
86
87
 
87
88
  should "raise OAuthTokenRevoked" do
@@ -12,6 +12,7 @@ class EtsyTest < Test::Unit::TestCase
12
12
  Etsy.instance_variable_set(:@api_key, nil)
13
13
  Etsy.instance_variable_set(:@api_secret, nil)
14
14
  Etsy.instance_variable_set(:@permission_scopes, nil)
15
+ Etsy.instance_variable_set(:@silent_errors, nil)
15
16
  end
16
17
 
17
18
  should "be able to set and retrieve the API key" do
@@ -19,6 +20,41 @@ class EtsyTest < Test::Unit::TestCase
19
20
  Etsy.api_key.should == 'key'
20
21
  end
21
22
 
23
+ should "be able to set and retrieve the API key across threads (global)" do
24
+ Etsy.api_key = 'key'
25
+ Thread.new do
26
+ Etsy.api_key.should == 'key'
27
+ end.join
28
+ end
29
+
30
+ should "be able to set and retrieve the API key inside a thread (thread local)" do
31
+ Etsy.api_key = 'key'
32
+ Thread.new do
33
+ Etsy.api_key = 'thread_local_key'
34
+ Etsy.api_key.should == 'thread_local_key'
35
+ end.join
36
+ end
37
+
38
+ should "be able to set and retrieve the API secret" do
39
+ Etsy.api_secret = 'secret'
40
+ Etsy.api_secret.should == 'secret'
41
+ end
42
+
43
+ should "be able to set and retrieve the API secret across threads (global)" do
44
+ Etsy.api_secret = 'secret'
45
+ Thread.new do
46
+ Etsy.api_secret.should == 'secret'
47
+ end.join
48
+ end
49
+
50
+ should "be able to set and retrieve the API secret inside a thread (thread local)" do
51
+ Etsy.api_secret = 'secret'
52
+ Thread.new do
53
+ Etsy.api_secret = 'thread_local_secret'
54
+ Etsy.api_secret.should == 'thread_local_secret'
55
+ end.join
56
+ end
57
+
22
58
  should "be able to find a user by username" do
23
59
  user = stub()
24
60
 
@@ -39,22 +75,30 @@ class EtsyTest < Test::Unit::TestCase
39
75
  lambda { Etsy.protocol = :invalid }.should raise_error(ArgumentError)
40
76
  end
41
77
 
42
- should "use the sandbox environment by default" do
43
- Etsy.environment.should == :sandbox
78
+ should "use silent errors by default" do
79
+ Etsy.silent_errors.should == true
44
80
  end
45
81
 
46
- should "be able to set the environment to a valid value" do
47
- Etsy.environment = :production
82
+ should "be able to set silent errors to a valid value" do
83
+ Etsy.silent_errors = false
84
+ Etsy.silent_errors.should == false
85
+ end
86
+
87
+ should "raise an exception when attempting to set an invalid silent errors value" do
88
+ lambda { Etsy.silent_errors = :invalid }.should raise_error(ArgumentError)
89
+ end
90
+
91
+ should "use the production environment by default" do
48
92
  Etsy.environment.should == :production
49
93
  end
50
94
 
51
- should "raise an exception when attempting to set an invalid environment" do
52
- lambda { Etsy.environment = :invalid }.should raise_error(ArgumentError)
95
+ should "be able to set the environment to a valid value" do
96
+ Etsy.environment = :sandbox
97
+ Etsy.environment.should == :sandbox
53
98
  end
54
99
 
55
- should "be able to set and retrieve the API secret" do
56
- Etsy.api_secret = 'secret'
57
- Etsy.api_secret.should == 'secret'
100
+ should "raise an exception when attempting to set an invalid environment" do
101
+ lambda { Etsy.environment = :invalid }.should raise_error(ArgumentError)
58
102
  end
59
103
 
60
104
  should "know the host for the sandbox environment" do
@@ -67,8 +111,8 @@ class EtsyTest < Test::Unit::TestCase
67
111
  Etsy.host.should == 'openapi.etsy.com'
68
112
  end
69
113
 
70
- should "default to sandbox host" do
71
- Etsy.host.should == 'sandbox.openapi.etsy.com'
114
+ should "default to production host" do
115
+ Etsy.host.should == 'openapi.etsy.com'
72
116
  end
73
117
 
74
118
  should "be able to set the callback url" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Reagan
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-27 00:00:00.000000000 Z
12
+ date: 2014-06-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  version: '0'
215
215
  requirements: []
216
216
  rubyforge_project:
217
- rubygems_version: 2.2.0
217
+ rubygems_version: 2.2.2
218
218
  signing_key:
219
219
  specification_version: 4
220
220
  summary: Provides a friendly ruby-like wrapper for the Etsy API
@@ -268,3 +268,4 @@ test_files:
268
268
  - test/unit/etsy/user_test.rb
269
269
  - test/unit/etsy/verification_request_test.rb
270
270
  - test/unit/etsy_test.rb
271
+ has_rdoc: