Class: Daruma::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/daruma.rb', line 12

def initialize(base_url)
  uri = URI.parse(base_url)

  @host = uri.host
  @port = uri.port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/daruma.rb', line 10

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/daruma.rb', line 10

def port
  @port
end

Class Method Details

.define_http_method(method_name, http_class) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/daruma.rb', line 20

def define_http_method(method_name, http_class)
  define_method(method_name) do |url, params: nil, tries: 0, sleep_seconds: 0|
    req = http_class.new(url)
    req.set_form_data(params) unless params.nil?

    Kudan.retryable(tries: tries, sleep_seconds: sleep_seconds) do
      response = Net::HTTP.start(@host, @port) { |http| http.request(req) }
      response.value
      response
    end
  end
end