Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 7f08e87

Browse files
committed
Exception when rate limit is exceeded
1 parent 738db02 commit 7f08e87

File tree

7 files changed

+23
-12
lines changed

7 files changed

+23
-12
lines changed

lib/thingiverse/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def access_token=(token)
3535
def get_token
3636
auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})
3737

38-
raise ResponseError.new(auth_response) unless auth_response.success?
38+
raise ResponseError.from(auth_response) unless auth_response.success?
3939

4040
response = CGI::parse(auth_response.parsed_response)
4141

lib/thingiverse/pagination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(response, object)
1212
@response = response
1313
@object = object
1414

15-
raise ResponseError.new(@response) unless @response.success?
15+
raise ResponseError.from(@response) unless @response.success?
1616

1717
@objects = @response.parsed_response.collect do |attrs|
1818
@object.new attrs
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class RateLimitExceededError < ResponseError
2+
end

lib/thingiverse/response_error.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module Thingiverse
22
class ResponseError < StandardError
3+
def self.from(response)
4+
case response.code.to_i
5+
when 420, 429
6+
RateLimitExceededError.new(response)
7+
else
8+
new(response)
9+
end
10+
end
11+
312
def initialize(response)
413
@response = response
514
end

lib/thingiverse/tags.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Tags
44

55
def self.find(tag_name)
66
response = Thingiverse::Connection.get("/tags/#{tag_name}")
7-
raise ResponseError.new(response) unless response.success?
7+
raise ResponseError.from(response) unless response.success?
88
self.new response.parsed_response
99
end
1010

lib/thingiverse/things.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Things
44

55
def user
66
response = Thingiverse::Connection.get("/users/#{creator['name']}")
7-
raise ResponseError.new(response) unless response.success?
7+
raise ResponseError.from(response) unless response.success?
88
Thingiverse::Users.new response.parsed_response
99
end
1010

@@ -26,7 +26,7 @@ def ancestors(query = {})
2626

2727
def tags
2828
response = Thingiverse::Connection.get(tags_url)
29-
raise ResponseError.new(response) unless response.success?
29+
raise ResponseError.from(response) unless response.success?
3030
response.parsed_response.collect do |attrs|
3131
Thingiverse::Tags.new attrs
3232
end
@@ -37,7 +37,7 @@ def save
3737
thing = Thingiverse::Things.create(@attributes)
3838
else
3939
response = Thingiverse::Connection.patch("/things/#{id}", :body => @attributes.to_json)
40-
raise ResponseError.new(response) unless response.success?
40+
raise ResponseError.from(response) unless response.success?
4141

4242
thing = Thingiverse::Things.new(response.parsed_response)
4343
end
@@ -63,7 +63,7 @@ def upload(file_or_string, thingiverse_filename=nil)
6363
raise ArgumentError, "Unable to determine filename" if thingiverse_filename.to_s == ""
6464

6565
response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => thingiverse_filename}.to_json)
66-
raise ResponseError.new(response) unless response.success?
66+
raise ResponseError.from(response) unless response.success?
6767

6868
parsed_response = JSON.parse(response.body)
6969
action = parsed_response["action"]
@@ -96,7 +96,7 @@ def upload(file_or_string, thingiverse_filename=nil)
9696
if c.response_code == 303
9797
# finalize it
9898
response = Thingiverse::Connection.post(query['success_action_redirect'])
99-
raise ResponseError.new(response) unless response.success?
99+
raise ResponseError.from(response) unless response.success?
100100
Thingiverse::Files.new(response.parsed_response)
101101
else
102102
raise "#{c.response_code}: #{c.body_str}"
@@ -108,7 +108,7 @@ def publish
108108
raise "Cannot publish until thing is saved"
109109
else
110110
response = Thingiverse::Connection.post("/things/#{id}/publish")
111-
raise ResponseError.new(response) unless response.success?
111+
raise ResponseError.from(response) unless response.success?
112112

113113
thing = Thingiverse::Things.new(response.parsed_response)
114114
end
@@ -120,7 +120,7 @@ def publish
120120

121121
def self.find(thing_id)
122122
response = Thingiverse::Connection.get("/things/#{thing_id}")
123-
raise ResponseError.new(response) unless response.success?
123+
raise ResponseError.from(response) unless response.success?
124124
self.new response.parsed_response
125125
end
126126

@@ -132,7 +132,7 @@ def self.create(params)
132132
thing = self.new(params)
133133

134134
response = Thingiverse::Connection.post('/things', :body => thing.attributes.to_json)
135-
raise ResponseError.new(response) unless response.success?
135+
raise ResponseError.from(response) unless response.success?
136136

137137
self.new(response.parsed_response)
138138
end

lib/thingiverse/users.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Users
44

55
def self.find(user_name)
66
response = Thingiverse::Connection.get("/users/#{user_name}")
7-
raise ResponseError.new(response) unless response.success?
7+
raise ResponseError.from(response) unless response.success?
88
self.new response.parsed_response
99
end
1010
end

0 commit comments

Comments
 (0)