Class: E621::API

Inherits:
Object
  • Object
show all
Defined in:
lib/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(mod = nil) ⇒ API

Variable mod is the module (Post, Pool, Set,…) this API is called for.



26
27
28
29
30
# File 'lib/api.rb', line 26

def initialize(mod=nil)
  @http   = HTTP.new
  @mod    = mod
  @user   = 
end

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,cookie = args
    r,tries = request2body(request,cookie),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