Class: E621::API
- Inherits:
-
Object
- Object
- E621::API
- Defined in:
- lib/api.rb
Instance Method Summary collapse
-
#initialize(mod = nil) ⇒ API
constructor
Variable mod is the module (Post, Pool, Set,…) this API is called for.
-
#method_missing(method, *args) ⇒ Object
Send commands to API and parse all answers, even errors.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Send commands to API and parse all answers, even errors. Also make it work with POST and GET requests.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/api.rb', line 33 def method_missing(method,*args) if [:get,:post].include?(method) then action,request, = args r,tries = request2body(request,),0 begin json = @http.__send__(method,"/#@mod/#{action}.json",r) rescue Timeout::Error sleep 2**tries tries += 1 #E621.log.debug("#@mod/#{action} failed: #{e.class}") raise if tries >= 4 # if we see more than 4 timeout errors, then there # is something wrong retry end #raise APIError,json["reason"] if json.include?("success") && !json["success"] return json else raise NoMethodError, "undefined method #{method} for #{self.class}" end end |