Class: E621::HTTP

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

Instance Method Summary collapse

Constructor Details

#initialize(host = "e621.net", port = 443) ⇒ HTTP

Returns a new instance of HTTP.



24
25
26
27
# File 'lib/standard/http.rb', line 24

def initialize(host="e621.net",port=443)
  @http = Net::HTTP.new(host,port)
  @http.use_ssl = true if port == 443
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/standard/http.rb', line 28

def method_missing(method, *args)
  if [:get,:post].include?(method) then
    tries,url,request = 0, *args
    begin
      response = case method
                 when :get  then @http.get("#{url}?#{request}")
                 when :post then @http.post(url,request)
                 end
    rescue
      sleep 2**tries
      tries += 1
      raise if tries >= 8
      retry
    end
    response = parse_body(response)
  else
    raise NoMethodError, "undefined method #{method} for #{self.class}"
  end
  return response
end