lib/leadlight/service.rb in leadlight-0.0.5 vs lib/leadlight/service.rb in leadlight-0.0.6
- old
+ new
@@ -1,8 +1,9 @@
require 'fattr'
require 'forwardable'
require 'leadlight/request'
+require 'leadlight/connection_builder'
module Leadlight
module Service
extend Forwardable
@@ -11,11 +12,12 @@
fattr(:tints) { self.class.tints }
fattr(:codec) { service_options.fetch(:codec) { Codec.new } }
fattr(:type_map) { TypeMap.new }
def_delegators :codec, :encode, :decode
- def_delegators 'self.class', :types, :type_for_name, :request_class
+ def_delegators 'self.class', :types, :type_for_name, :request_class, :http_adapter
+ def_delegators :Leadlight, :common_connection_stack
def initialize(service_options={})
@service_options = service_options
execute_hook(:on_init, self)
end
@@ -29,16 +31,16 @@
def url
self.class.url
end
def connection
- @connection ||= Faraday.new(url: self.url) do |builder|
- builder.use Leadlight::ServiceMiddleware, service: self
- instance_exec(builder, &connection_stack)
- builder.use Faraday::Response::Logger, logger
- instance_exec(builder, &Leadlight.common_connection_stack)
- end
+ @connection ||= ConnectionBuilder.new do |cxn|
+ cxn.url url
+ cxn.service self
+ cxn.common_stack common_connection_stack
+ cxn.adapter http_adapter
+ end.call
end
[:options, :head, :get, :post, :put, :delete, :patch].each do |name|
define_method(name) do |url, *args, &block|
perform_request(url, name, *args, &block)
@@ -51,11 +53,11 @@
get(*args).raise_on_error.submit_and_wait(&block)
end
private
- def perform_request(url, http_method, params={}, body=nil, &representation_handler)
- req = request_class.new(self, connection, url, http_method, params, body)
+ def perform_request(url, http_method, body=nil, options={}, &representation_handler)
+ req = request_class.new(self, connection, url, http_method, body, options)
if representation_handler
req.submit_and_wait(&representation_handler)
end
req
end