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: {}) ⇒ Object
- #get(path, parameters: {}, headers: {}, options: {}) ⇒ Object
- #head(path, parameters: {}, headers: {}, options: {}) ⇒ 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: {}) ⇒ Object
- #post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}) ⇒ 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/httply/client.rb', line 70 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 builder.proxy = generate_faraday_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: {}) ⇒ Object
39 40 41 |
# File 'lib/httply/client.rb', line 39 def delete(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :delete, parameters: parameters, data: data, headers: headers, options: end |
#get(path, parameters: {}, headers: {}, options: {}) ⇒ Object
19 20 21 |
# File 'lib/httply/client.rb', line 19 def get(path, parameters: {}, headers: {}, options: {}) request path, method: :get, parameters: parameters, headers: headers, options: end |
#head(path, parameters: {}, headers: {}, options: {}) ⇒ Object
23 24 25 |
# File 'lib/httply/client.rb', line 23 def head(path, parameters: {}, headers: {}, options: {}) request path, method: :head, parameters: parameters, headers: headers, options: end |
#log(message) ⇒ Object
107 108 109 |
# File 'lib/httply/client.rb', line 107 def log() puts "[Httply::Client] - #{message}" if self.configuration.verbose end |
#patch(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
35 36 37 |
# File 'lib/httply/client.rb', line 35 def patch(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :patch, parameters: parameters, data: data, headers: headers, options: end |
#post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
27 28 29 |
# File 'lib/httply/client.rb', line 27 def post(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :post, parameters: parameters, data: data, headers: headers, options: end |
#put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
31 32 33 |
# File 'lib/httply/client.rb', line 31 def put(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :put, parameters: parameters, data: data, headers: headers, options: end |
#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}) ⇒ 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 |
# File 'lib/httply/client.rb', line 43 def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}) 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 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 |