Class: Frenchy::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Client

Create a new client instance



10
11
12
13
14
15
16
17
18
19
# File 'lib/frenchy/client.rb', line 10

def initialize(name, options={})
  options.stringify_keys!

  @name           = name.to_s
  @host           = options.fetch("host")           { "http://127.0.0.1:8080" }
  @timeout        = options.fetch("timeout")        { 30 }
  @retries        = options.fetch("retries")        { 0 }
  @backoff_delay  = options.fetch("backoff_delay")  { 1.0 }
  @headers        = options.fetch("headers")        { {} }
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/frenchy/client.rb', line 7

def host
  @host
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/frenchy/client.rb', line 7

def name
  @name
end

#retriesObject

Returns the value of attribute retries.



7
8
9
# File 'lib/frenchy/client.rb', line 7

def retries
  @retries
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/frenchy/client.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#get(path, params) ⇒ Object

Issue a get request with the given path and query parameters. Get requests can be retried.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/frenchy/client.rb', line 23

def get(path, params)
  try = 0
  err = nil

  while try <= @retries
    sleep (@backoff_delay * (try*try)) if try > 0

    begin
      return perform("GET", path, params)
    rescue Frenchy::ServerError => err
      try += 1
    end
  end

  raise err
end