discogs-wrapper 2.5.0 → 2.5.1

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
  SHA256:
3
- metadata.gz: 452ba1fcbd498273e0b9f108f6e5b7aee3a702ae0a319ccfec159e9970209bcb
4
- data.tar.gz: 552d70e54152bafd4c09e08d8552c99efe718bea936e634b8fbaefc7df55b20b
3
+ metadata.gz: 798cfa307567b4f7357cc2f4885bce6bdb6fc5e6958b3283aa94e4df30eba47b
4
+ data.tar.gz: 65bf1b0b0a6289676ce068458bf059797348e3aa133a16371c75ab5946453762
5
5
  SHA512:
6
- metadata.gz: 0e0449729d5e7b79b6e7f067fa4cbe3cbc0b9fa9f8f7cdfc3a3b384b14f7f0e6614b2fb66efd56cddc5a1505d1b542e02952d7a0b176e48cb5496bbd1266bf99
7
- data.tar.gz: 95b4ebf0d2bc534520b18d7a0b4babb04e444f99ec2eda73ba258a926225018f385af94c876d3f1d6b1da81f89e8ecc03991eb0e5957b901fb36889a0e19e365
6
+ metadata.gz: 5ff421201fc13851d304be663edf3b944e2b7c89c695fd5e6646b7be8760cbe37055cb697db21324d12b20688c4fa77a00faf82a7a319d18218b72ae5a5114cd
7
+ data.tar.gz: 3e81877b8f07e3a455fd233f3ca61210832b7c37be6d903d3ea449486e5edb8bbe136c432186a5cf619149f5f51b185ffa6b336a1819ac013c3170b4ae0888fb
@@ -52,6 +52,8 @@ USAGE
52
52
  To use this library, you must supply the name of your application. For example:
53
53
 
54
54
  ```ruby
55
+ require "discogs"
56
+
55
57
  wrapper = Discogs::Wrapper.new("My awesome web app")
56
58
  ```
57
59
 
@@ -92,11 +94,19 @@ search.results.first.id # => 691078
92
94
 
93
95
  Many of the API endpoints return further URLs that will yield specific data. To cater for this, the library provides a "raw" method that accepts a valid API URL. For example:
94
96
 
95
- sts_records = wrapper.get_label(9800)
96
- sts_releases = wrapper.raw(sts_records.releases_url)
97
- first_sts_release = wrapper.raw(sts_releases.releases[1].resource_url)
97
+ ```ruby
98
+ sts_records = wrapper.get_label(9800)
99
+ sts_releases = wrapper.raw(sts_records.releases_url)
100
+ first_sts_release = wrapper.raw(sts_releases.releases[1].resource_url)
101
+
102
+ first_sts_release.title # => "I'll Nostra Tempo De La Vita / Having The Time Of Your Life"
103
+ ```
98
104
 
99
- first_sts_release.title # => "I'll Nostra Tempo De La Vita / Having The Time Of Your Life"
105
+ You can also add optional querystring overrides to raw calls:
106
+
107
+ ```ruby
108
+ sombre = wrapper.raw("https://api.discogs.com/database/search?q=Sombre+Records&per_page=50&type=release", {"page" => 2})
109
+ ```
100
110
 
101
111
  You can see all implemented methods on [this projects RDoc page](http://rdoc.info/github/buntine/discogs/master/frames).
102
112
 
@@ -15,6 +15,7 @@ module Discogs; end
15
15
  class Discogs::UnknownResource < StandardError; end
16
16
  class Discogs::InternalServerError < StandardError; end
17
17
  class Discogs::AuthenticationError < StandardError; end
18
+ class Discogs::RateLimitError < StandardError; end
18
19
 
19
20
  # Loading sequence.
20
21
  require File.dirname(__FILE__) + "/wrapper/wrapper"
@@ -680,7 +680,7 @@ class Discogs::Wrapper
680
680
  # * +AUD+
681
681
  # * +JPY+
682
682
  def get_fee(price, currency="USD")
683
- query_and_build "marketplace/fee/#{price}/#{currency}"
683
+ query_and_build "marketplace/fee/#{CGI.escape(price)}/#{currency}"
684
684
  end
685
685
 
686
686
  # Retrieve an image by filename.
@@ -718,9 +718,9 @@ class Discogs::Wrapper
718
718
  #
719
719
  # @param [String (Required)] API endpoint
720
720
  # @return [Hash] API response
721
- def raw(url)
721
+ def raw(url, additional_params={})
722
722
  uri = URI.parse(url)
723
- params = CGI.parse(uri.query.to_s)
723
+ params = CGI.parse(uri.query.to_s).merge(additional_params)
724
724
 
725
725
  query_and_build uri.path, params
726
726
  end
@@ -745,6 +745,7 @@ class Discogs::Wrapper
745
745
 
746
746
  raise_unknown_resource(path) if response.code == "404"
747
747
  raise_authentication_error(path) if response.code == "401"
748
+ raise_rate_limit_error(path) if response.code == "429"
748
749
  raise_internal_server_error if response.code == "500"
749
750
 
750
751
  # Unzip the response data, or just read it in directly
@@ -797,7 +798,7 @@ class Discogs::Wrapper
797
798
  parameters = {:f => output_format}.merge(params)
798
799
  querystring = "?" + URI.encode_www_form(prepare_hash(parameters))
799
800
 
800
- URI.parse(File.join(@@root_host, [URI.escape(path), querystring].join))
801
+ URI.parse(File.join(@@root_host, [path, querystring].join))
801
802
  end
802
803
 
803
804
  # Stringifies keys and sorts.
@@ -813,7 +814,7 @@ class Discogs::Wrapper
813
814
 
814
815
  # Replaces known conflicting keys with safe names in a nested hash structure.
815
816
  def sanitize_hash(hash)
816
- conflicts = {"count" => "total"}
817
+ conflicts = {"count" => "total", "type_" => "type"}
817
818
  result = {}
818
819
 
819
820
  for k, v in hash
@@ -833,6 +834,8 @@ class Discogs::Wrapper
833
834
 
834
835
  if v.is_a?(Hash)
835
836
  result[k] = sanitize_hash(result[k])
837
+ elsif v.is_a?(Array)
838
+ result[k] = v.map { |o| o.is_a?(Hash) ? sanitize_hash(o) : o }
836
839
  end
837
840
  end
838
841
 
@@ -843,6 +846,10 @@ class Discogs::Wrapper
843
846
  raise Discogs::UnknownResource, "Unknown Discogs resource: #{path}"
844
847
  end
845
848
 
849
+ def raise_rate_limit_error(path="")
850
+ raise Discogs::RateLimitError, "Rate limit exceeded: #{path}"
851
+ end
852
+
846
853
  def raise_internal_server_error
847
854
  raise Discogs::InternalServerError, "The API server cannot complete the request"
848
855
  end
@@ -146,6 +146,18 @@ describe Discogs::Wrapper do
146
146
  @wrapper.raw("https://api.discogs.com/artists/1000")
147
147
  end
148
148
 
149
+ it "should generate the correct URL to parse when given raw URL and additional params" do
150
+ @search_uri = double("uri")
151
+
152
+ allow(@search_uri).to receive_messages(:host => "api.discogs.com", :query => "q=Sombre+Records&per_page=50&type=release&page=12&new=true", :path => "database/search")
153
+
154
+ mock_http_with_response "200", read_sample("search_results")
155
+ URI.should_receive(:parse).with("https://api.discogs.com/database/search?q=Sombre+Records&per_page=50&type=release&page=11").and_return(@search_uri)
156
+ URI.should_receive(:parse).with("https://api.discogs.com/database/search?f=json&new=true&page=12&per_page=50&q=Sombre+Records&type=release").and_return(@uri)
157
+
158
+ @wrapper.raw("https://api.discogs.com/database/search?q=Sombre+Records&per_page=50&type=release&page=11", {"page" => 12, "new" => true})
159
+ end
160
+
149
161
  end
150
162
 
151
163
  ## NOTE: See ./spec/wrapper_methods/*.rb for indepth tests on valid API requests.
@@ -247,4 +259,13 @@ describe Discogs::Wrapper do
247
259
 
248
260
  end
249
261
 
262
+ describe "when exceeding rate limit" do
263
+
264
+ it "should raise an exception if the rate limit is exceeded" do
265
+ mock_http_with_response "429"
266
+
267
+ lambda { @wrapper.get_master(@master_id) }.should raise_error(Discogs::RateLimitError)
268
+ end
269
+ end
270
+
250
271
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discogs-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Buntine
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-04-12 00:00:00.000000000 Z
12
+ date: 2020-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry