@@ -4,7 +4,7 @@ class Things
44
55 def user
66 response = Thingiverse ::Connection . get ( "/users/#{ creator [ 'name' ] } " )
7- raise " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } " unless response . success?
7+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } " unless response . success?
29+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } " unless response . success?
40+ raise ResponseError . new ( response ) unless response . success?
4141
4242 thing = Thingiverse ::Things . new ( response . parsed_response )
4343 end
@@ -46,7 +46,7 @@ def save
4646 send ( "#{ name } =" , value )
4747 end
4848 end
49-
49+
5050 # file_or_string can be a File or a String.
5151 # thingiverse_filename is optional if using a File (the File filename will be used by default) but is required if using a String
5252 def upload ( file_or_string , thingiverse_filename = nil )
@@ -59,11 +59,11 @@ def upload(file_or_string, thingiverse_filename=nil)
5959 else
6060 raise ArgumentError , "file_or_string not of accepted type. Expected File or String. Actual: #{ file_or_string . class } "
6161 end
62-
62+
6363 raise ArgumentError , "Unable to determine filename" if thingiverse_filename . to_s == ""
64-
64+
6565 response = Thingiverse ::Connection . post ( "/things/#{ id } /files" , :body => { :filename => thingiverse_filename } . to_json )
66- raise " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } #{ response . headers [ 'x-error' ] } " unless response . success?
66+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } #{ response . headers [ 'x-error' ] } " unless response . success?
99+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } " unless response . success?
111+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } #{ response . headers [ 'x-error' ] } " unless response . success?
123+ raise ResponseError . new ( 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 " #{ response . code } : #{ JSON . parse ( response . body ) [ 'error' ] } #{ response . headers [ 'x-error' ] } " unless response . success?
135+ raise ResponseError . new ( response ) unless response . success?
136136
137137 self . new ( response . parsed_response )
138138 end
0 commit comments