Class: Httply::Client
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#host ⇒ Object
Returns the value of attribute host.
-
#memoize ⇒ Object
Returns the value of attribute memoize.
Instance Method Summary collapse
- #configure(host:, headers: {}, options: {}) ⇒ Object
- #delete(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #force_format(response, format) ⇒ Object
- #get(path, parameters: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #head(path, parameters: {}, headers: {}, options: {}, format: nil) ⇒ Object
-
#initialize(host: nil, configuration: ::Httply.configuration, memoize: false) ⇒ Client
constructor
A new instance of Client.
- #log(message) ⇒ Object
- #patch(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #post(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #put(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
- #setup(host: nil, headers: {}, options: {}) ⇒ Object
Methods included from Proxies
#determine_proxy, #generate_faraday_proxy, #proxy_from_array, #proxy_from_hash, #proxy_from_object, #proxy_from_string, #proxy_model_defined?, #set_credentials
Constructor Details
#initialize(host: nil, configuration: ::Httply.configuration, memoize: false) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 |
# File 'lib/httply/client.rb', line 8 def initialize(host: nil, configuration: ::Httply.configuration, memoize: false) self.host = ::Httply::Utilities::Uri.correct_host(host) self.configuration = configuration self.memoize = memoize self.connection = nil end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
3 4 5 |
# File 'lib/httply/client.rb', line 3 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/httply/client.rb', line 4 def connection @connection end |
#host ⇒ Object
Returns the value of attribute host.
3 4 5 |
# File 'lib/httply/client.rb', line 3 def host @host end |
#memoize ⇒ Object
Returns the value of attribute memoize.
4 5 6 |
# File 'lib/httply/client.rb', line 4 def memoize @memoize end |
Instance Method Details
#configure(host:, headers: {}, options: {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/httply/client.rb', line 86 def configure(host:, headers: {}, options: {}) = .fetch(:client, {}) = .fetch(:request, {}) redirects = .fetch(:redirects, 10) proxy = determine_proxy(.fetch(:proxy, nil)) headers["User-Agent"] = headers.fetch("User-Agent", ::Agents.random_user_agent(.fetch(:user_agent_device, :desktop))) connection = ::Faraday.new(host, ) do |builder| builder.[:timeout] = .fetch(:timeout, nil) if .fetch(:timeout, nil) builder.[:open_timeout] = .fetch(:open_timeout, nil) if .fetch(:open_timeout, nil) builder.headers = headers builder.request :url_encoded if .fetch(:url_encoded, false) builder.request :json if .fetch(:json, false) builder.response :logger if self.configuration.verbose builder.response :xml, content_type: /\bxml$/ builder.response :json, content_type: /\bjson$/ builder.use ::Httply::Middlewares::ParseHtml, content_type: /\btext\/html$/ builder.use ::FaradayMiddleware::FollowRedirects, limit: redirects if redirects && redirects > 0 if proxy && !proxy.empty? builder.proxy = proxy log("Will use proxy: #{builder.proxy.inspect}") end builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter) end return connection end |
#delete(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
39 40 41 |
# File 'lib/httply/client.rb', line 39 def delete(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) request path, method: :delete, parameters: parameters, data: data, headers: headers, options: , format: format end |
#force_format(response, format) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/httply/client.rb', line 73 def force_format(response, format) case format.to_sym when :json response.body = ::JSON.parse(response.body) when :xml response.body = ::MultiXml.parse(response.body) when :html response.body = ::Nokogiri::HTML(response.body, nil, "utf-8") end return response end |
#get(path, parameters: {}, headers: {}, options: {}, format: nil) ⇒ Object
19 20 21 |
# File 'lib/httply/client.rb', line 19 def get(path, parameters: {}, headers: {}, options: {}, format: nil) request path, method: :get, parameters: parameters, headers: headers, options: , format: format end |
#head(path, parameters: {}, headers: {}, options: {}, format: nil) ⇒ Object
23 24 25 |
# File 'lib/httply/client.rb', line 23 def head(path, parameters: {}, headers: {}, options: {}, format: nil) request path, method: :head, parameters: parameters, headers: headers, options: , format: format end |
#log(message) ⇒ Object
123 124 125 |
# File 'lib/httply/client.rb', line 123 def log() puts "[Httply::Client] - #{}" if self.configuration.verbose end |
#patch(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
35 36 37 |
# File 'lib/httply/client.rb', line 35 def patch(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) request path, method: :patch, parameters: parameters, data: data, headers: headers, options: , format: format end |
#post(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
27 28 29 |
# File 'lib/httply/client.rb', line 27 def post(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) request path, method: :post, parameters: parameters, data: data, headers: headers, options: , format: format end |
#put(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
31 32 33 |
# File 'lib/httply/client.rb', line 31 def put(path, parameters: {}, data: {}, headers: {}, options: {}, format: nil) request path, method: :put, parameters: parameters, data: data, headers: headers, options: , format: format end |
#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, format: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/httply/client.rb', line 43 def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, format: nil) host = !self.host.to_s.empty? ? self.host : ::Httply::Utilities::Uri.parse_host(path) path = ::Httply::Utilities::Uri.to_path(path) connection = self.memoize ? setup(host: host, headers: headers, options: ) : configure(host: host, headers: headers, options: ) response = case method when :get connection.get do |request| request.url path request.params = parameters if parameters && !parameters.empty? end when :head connection.head do |request| request.url path request.params = parameters if parameters && !parameters.empty? end when :post, :put, :patch, :delete connection.send(method) do |request| request.url path request.body = data if data && !data.empty? request.params = parameters if parameters && !parameters.empty? end end response = ::Httply::Response.new(response) response = force_format(response, format) unless format.to_s.empty? return response end |
#setup(host: nil, headers: {}, options: {}) ⇒ Object
15 16 17 |
# File 'lib/httply/client.rb', line 15 def setup(host: nil, headers: {}, options: {}) self.connection ||= configure(host: host, headers: headers, options: ) end |