This repository was archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfaraday_stack.rb
More file actions
52 lines (45 loc) · 1.54 KB
/
faraday_stack.rb
File metadata and controls
52 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# encoding: utf-8
require 'faraday'
require 'forwardable'
require 'faraday_stack/addressable_patch'
module FaradayStack
extend Faraday::AutoloadHelper
autoload_all 'faraday_stack',
:ResponseMiddleware => 'response_middleware',
:ResponseJSON => 'response_json',
:ResponseXML => 'response_xml',
:ResponseHTML => 'response_html',
:Instrumentation => 'instrumentation',
:Caching => 'caching',
:FollowRedirects => 'follow_redirects',
:RackCompatible => 'rack_compatible'
# THE ÜBER STACK
def self.default_connection
@default_connection ||= self.build
end
class << self
extend Forwardable
attr_writer :default_connection
def_delegators :default_connection, :get, :post, :put, :head, :delete
end
def self.build(url = nil, options = {})
klass = nil
if url.is_a?(Hash) then options = url.dup
elsif url.is_a?(Class) then klass = url
else options = options.merge(:url => url)
end
klass ||= options.delete(:class) || Faraday::Connection
klass.new(options) do |builder|
builder.request :url_encoded
builder.request :json
yield builder if block_given?
builder.use ResponseXML, :content_type => /[+\/]xml$/
builder.use ResponseHTML, :content_type => 'text/html'
builder.use ResponseJSON, :content_type => /[+\/]json$/
builder.use ResponseJSON::MimeTypeFix, :content_type => /text\/(plain|javascript)/
builder.use FollowRedirects
builder.response :raise_error
builder.adapter Faraday.default_adapter
end
end
end