lib/frenchy/client.rb in frenchy-0.2.2 vs lib/frenchy/client.rb in frenchy-0.2.3
- old
+ new
@@ -1,20 +1,21 @@
require "net/http"
require "json"
module Frenchy
class Client
- attr_accessor :host, :timeout, :retries
+ attr_accessor :name, :host, :timeout, :retries
# Create a new client instance
- def initialize(options={})
+ def initialize(name, options={})
options.stringify_keys!
- @host = options.delete("host") || "http://127.0.0.1:8080"
- @timeout = options.delete("timeout") || 30
- @retries = options.delete("retries") || 0
- @backoff_delay = options.delete("backoff_delay") || 1.0
+ @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 }
end
# Issue a get request with the given path and query parameters. Get
# requests can be retried.
def get(path, params)
@@ -88,11 +89,11 @@
# Create a request info string for inspection
reqinfo = "#{method} #{uri.to_s}"
# Perform the request
begin
- resp = http.request(req)
+ resp = perform_request(http, req)
rescue => ex
raise Frenchy::ServerError.new(ex, reqinfo, nil)
end
# Return based on response
@@ -109,8 +110,12 @@
raise Frenchy::NotFound.new(nil, reqinfo, resp)
else
# All other responses are treated as a server error
raise Frenchy::ServiceUnavailable.new(nil, reqinfo, resp)
end
+ end
+
+ def perform_request(http, req)
+ http.request(req)
end
end
end