diff --git a/lib/thingiverse/connection.rb b/lib/thingiverse/connection.rb index da50e8d..e0c0b54 100644 --- a/lib/thingiverse/connection.rb +++ b/lib/thingiverse/connection.rb @@ -35,7 +35,7 @@ def access_token=(token) def get_token auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code}) - raise ResponseError.new(auth_response) unless auth_response.success? + raise ResponseError.from(auth_response) unless auth_response.success? response = CGI::parse(auth_response.parsed_response) diff --git a/lib/thingiverse/pagination.rb b/lib/thingiverse/pagination.rb index 7f7aa17..8bbf8a0 100644 --- a/lib/thingiverse/pagination.rb +++ b/lib/thingiverse/pagination.rb @@ -12,7 +12,7 @@ def initialize(response, object) @response = response @object = object - raise ResponseError.new(@response) unless @response.success? + raise ResponseError.from(@response) unless @response.success? @objects = @response.parsed_response.collect do |attrs| @object.new attrs diff --git a/lib/thingiverse/rate_limit_exceeded_error.rb b/lib/thingiverse/rate_limit_exceeded_error.rb new file mode 100644 index 0000000..75c7d4d --- /dev/null +++ b/lib/thingiverse/rate_limit_exceeded_error.rb @@ -0,0 +1,2 @@ +class RateLimitExceededError < ResponseError +end diff --git a/lib/thingiverse/response_error.rb b/lib/thingiverse/response_error.rb index 2df9a86..6f8f5ef 100644 --- a/lib/thingiverse/response_error.rb +++ b/lib/thingiverse/response_error.rb @@ -1,5 +1,14 @@ module Thingiverse class ResponseError < StandardError + def self.from(response) + case response.code.to_i + when 420, 429 + RateLimitExceededError.new(response) + else + new(response) + end + end + def initialize(response) @response = response end diff --git a/lib/thingiverse/tags.rb b/lib/thingiverse/tags.rb index 70eec53..95b0009 100644 --- a/lib/thingiverse/tags.rb +++ b/lib/thingiverse/tags.rb @@ -4,7 +4,7 @@ class Tags def self.find(tag_name) response = Thingiverse::Connection.get("/tags/#{tag_name}") - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? self.new response.parsed_response end diff --git a/lib/thingiverse/things.rb b/lib/thingiverse/things.rb index 5a042ee..e5730b3 100644 --- a/lib/thingiverse/things.rb +++ b/lib/thingiverse/things.rb @@ -4,7 +4,7 @@ class Things def user response = Thingiverse::Connection.get("/users/#{creator['name']}") - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? Thingiverse::Users.new response.parsed_response end @@ -26,7 +26,7 @@ def ancestors(query = {}) def tags response = Thingiverse::Connection.get(tags_url) - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? response.parsed_response.collect do |attrs| Thingiverse::Tags.new attrs end @@ -37,7 +37,7 @@ def save thing = Thingiverse::Things.create(@attributes) else response = Thingiverse::Connection.patch("/things/#{id}", :body => @attributes.to_json) - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? thing = Thingiverse::Things.new(response.parsed_response) end @@ -63,7 +63,7 @@ def upload(file_or_string, thingiverse_filename=nil) raise ArgumentError, "Unable to determine filename" if thingiverse_filename.to_s == "" response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => thingiverse_filename}.to_json) - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? parsed_response = JSON.parse(response.body) action = parsed_response["action"] @@ -96,7 +96,7 @@ def upload(file_or_string, thingiverse_filename=nil) if c.response_code == 303 # finalize it response = Thingiverse::Connection.post(query['success_action_redirect']) - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? Thingiverse::Files.new(response.parsed_response) else raise "#{c.response_code}: #{c.body_str}" @@ -108,7 +108,7 @@ def publish raise "Cannot publish until thing is saved" else response = Thingiverse::Connection.post("/things/#{id}/publish") - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? thing = Thingiverse::Things.new(response.parsed_response) end @@ -120,7 +120,7 @@ def publish def self.find(thing_id) response = Thingiverse::Connection.get("/things/#{thing_id}") - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? self.new response.parsed_response end @@ -132,7 +132,7 @@ def self.create(params) thing = self.new(params) response = Thingiverse::Connection.post('/things', :body => thing.attributes.to_json) - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? self.new(response.parsed_response) end diff --git a/lib/thingiverse/users.rb b/lib/thingiverse/users.rb index 4d3a3a1..2e94196 100644 --- a/lib/thingiverse/users.rb +++ b/lib/thingiverse/users.rb @@ -4,7 +4,7 @@ class Users def self.find(user_name) response = Thingiverse::Connection.get("/users/#{user_name}") - raise ResponseError.new(response) unless response.success? + raise ResponseError.from(response) unless response.success? self.new response.parsed_response end end