Class: Httply::Client

Inherits:
Object
  • Object
show all
Includes:
Proxies
Defined in:
lib/httply/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/httply/client.rb', line 7

def initialize(host: nil, configuration: ::Httply.configuration)
  self.host             =   host
  self.configuration    =   configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



3
4
5
# File 'lib/httply/client.rb', line 3

def configuration
  @configuration
end

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/httply/client.rb', line 3

def host
  @host
end

Instance Method Details

#delete(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil) ⇒ Object



43
44
45
# File 'lib/httply/client.rb', line 43

def delete(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
  request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options, as: as
end

#get(path, parameters: {}, headers: {}, options: {}, as: nil) ⇒ Object



23
24
25
# File 'lib/httply/client.rb', line 23

def get(path, parameters: {}, headers: {}, options: {}, as: nil)
  request path, method: :get, parameters: parameters, headers: headers, options: options, as: as
end

#head(path, parameters: {}, headers: {}, options: {}, as: nil) ⇒ Object



27
28
29
# File 'lib/httply/client.rb', line 27

def head(path, parameters: {}, headers: {}, options: {}, as: nil)
  request path, method: :head, parameters: parameters, headers: headers, options: options, as: as
end

#patch(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil) ⇒ Object



39
40
41
# File 'lib/httply/client.rb', line 39

def patch(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
  request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options, as: as
end

#post(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil) ⇒ Object



31
32
33
# File 'lib/httply/client.rb', line 31

def post(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
  request path, method: :post, parameters: parameters, data: data, headers: headers, options: options, as: as
end

#put(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil) ⇒ Object



35
36
37
# File 'lib/httply/client.rb', line 35

def put(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
  request path, method: :put, parameters: parameters, data: data, headers: headers, options: options, as: as
end

#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, as: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/httply/client.rb', line 47

def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
  connection                =   setup(path, method: method, headers: headers, options: options, as: as)
  
  response                  =   case method
    when :get
      connection.get do |request|
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :head
      connection.head do |request|
        request.params      =   parameters if parameters && !parameters.empty?
      end
    when :post, :put, :patch, :delete
      connection.send(method) do |request|
        request.body        =   data if data && !data.empty?
        request.params      =   parameters if parameters && !parameters.empty?
      end
  end
  
  return response
end

#setup(path, method: :get, headers: {}, options: {}, as: nil) ⇒ Object



69
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
# File 'lib/httply/client.rb', line 69

def setup(path, method: :get, headers: {}, options: {}, as: nil)
  client_options            =   options.fetch(:client, {})
  follow_redirects          =   options.fetch(:follow_redirects, true)
  redirect_limit            =   options.fetch(:redirects_limit, 10)
  proxy                     =   determine_proxy(options.fetch(:proxy, nil))
  
  url                       =   to_uri(path)
  
  headers                   =   {"User-Agent" => ::Agents.random_user_agent(options.fetch(:user_agent_device, :desktop))}.merge(headers)
  
  connection                =   ::Faraday.new(url, client_options) do |builder|
    builder.options[:timeout]         =   options.fetch(:timeout, nil)      if options.fetch(:timeout, nil)
    builder.options[:open_timeout]    =   options.fetch(:open_timeout, nil) if options.fetch(:open_timeout, nil)
    
    builder.headers         =   headers
    
    builder.request :url_encoded if [:post, :put, :patch, :delete].include?(method)
    
    builder.response :logger if self.configuration.verbose
    
    builder.response :xml,  content_type: /\bxml$/  if as.eql?(:xml)
    builder.response :json, content_type: /\bjson$/ if as.eql?(:json)
    builder.use ::Httply::Middlewares::ParseHtml      if as.eql?(:html)
    
    builder.use ::FaradayMiddleware::FollowRedirects, limit: redirect_limit if follow_redirects && redirect_limit && redirect_limit > 0
    
    if proxy
      builder.proxy         =   generate_faraday_proxy(proxy)
      puts "[Httply::Client] - Will use proxy: #{builder.proxy.inspect}" if self.configuration.verbose
    end

    builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
  end
  
  return connection
end

#to_uri(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/httply/client.rb', line 12

def to_uri(path)
  path                  =   path.gsub(/^\//i, "")
  
  if path !~ /^http(s)?:\/\// && !self.host.to_s.empty?
    host_part           =   self.host =~ /^http(s)?:\/\// ? self.host : "https://#{self.host}"
    path                =   "#{host_part}/#{path}"
  end
  
  return path
end