httply 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +14 -5
- data/lib/httply/client.rb +45 -40
- data/lib/httply/utilities/uri.rb +33 -0
- data/lib/httply/version.rb +1 -1
- data/lib/httply.rb +4 -0
- metadata +3 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 1a9a2b6ebd2873064c17ff349e01c280258cf3672aa3efa9fc4d9552d4d64852
         | 
| 4 | 
            +
              data.tar.gz: 304af45cd9cf5870fb14e1883d44fb8306cd01cb28b397f456a35d52736767f6
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 2276fbddf8538d72109e60498cdac15f6e3ce0a595b1a37a126b19965d2e1b244965c8a86df0f2c78196c68a267dc3d14dbebfac1bf01e6e91935e19003a8bdd
         | 
| 7 | 
            +
              data.tar.gz: bdfad9a05aa7b634193faf62f3768e80414591719bf0a7a06f9e8449378ca4d378e9684146672ae9550f58ba022fc9294148ede57570989f917065029fc478a2
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -2,7 +2,7 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            Httply is a lightweight wrapper around Faraday to support automatic randomization of proxies and user agents, amongst other things.
         | 
| 4 4 |  | 
| 5 | 
            -
            Randomized proxy switching support is provided by the [proxied](https://github.com/SebastianJ/proxied) gem and randomized user agent is provided by the [agents](https://github.com/SebastianJ/agents) gem.
         | 
| 5 | 
            +
            Randomized proxy switching support is provided by the [proxied](https://github.com/SebastianJ/proxied) gem and randomized user agent support is provided by the [agents](https://github.com/SebastianJ/agents) gem.
         | 
| 6 6 |  | 
| 7 7 | 
             
            Randomized/automatic proxy switching is currently only supported in conjunction with the [proxied](https://github.com/SebastianJ/proxied) gem. Manual proxy support is obviously supported irregardless of using [proxied](https://github.com/SebastianJ/proxied) or not.
         | 
| 8 8 |  | 
| @@ -26,16 +26,25 @@ Or install it yourself as: | |
| 26 26 |  | 
| 27 27 | 
             
            ### Instance
         | 
| 28 28 |  | 
| 29 | 
            -
            Httply can  | 
| 29 | 
            +
            Httply can be used as an instance:
         | 
| 30 30 |  | 
| 31 31 | 
             
            ```ruby
         | 
| 32 32 | 
             
            client    =   Httply::Client.new(host: "https://www.google.com")
         | 
| 33 33 | 
             
            response  =   client.get("/webhp", parameters: {hl: :en})
         | 
| 34 34 | 
             
            ```
         | 
| 35 35 |  | 
| 36 | 
            +
            Httply can also memoize the Faraday connection (e.g. if you're invoking an API and using the same proxy and user agent for every request):
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            ```ruby
         | 
| 39 | 
            +
            client    =   Httply::Client.new(host: "https://www.google.com", memoize: true)
         | 
| 40 | 
            +
            response  =   client.get("/webhp", parameters: {hl: :en})
         | 
| 41 | 
            +
            ```
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            The difference is that for the first example user-agent and proxy will automatically be rotated (if not providing a specifc user-agent or proxy) on every request whereas in the second example the user-agent and proxy will be kept for every request.
         | 
| 44 | 
            +
             | 
| 36 45 | 
             
            ### Class method
         | 
| 37 46 |  | 
| 38 | 
            -
             | 
| 47 | 
            +
            Httply can also be invoked on a class level:
         | 
| 39 48 |  | 
| 40 49 | 
             
            ```ruby
         | 
| 41 50 | 
             
            response  =   Httply.get("https://www.google.com/webhp", parameters: {hl: :en})
         | 
| @@ -45,10 +54,10 @@ response  =   Httply.get("https://www.google.com/webhp", parameters: {hl: :en}) | |
| 45 54 |  | 
| 46 55 | 
             
            HTML parsing requires that Nokogiri has been required elsewhere and is accessible for Httply.
         | 
| 47 56 |  | 
| 48 | 
            -
             | 
| 57 | 
            +
            If Nokogiri is available "text/html" responses will automatically be parsed by Nokogiri.
         | 
| 49 58 |  | 
| 50 59 | 
             
            ```ruby
         | 
| 51 | 
            -
            response  =   Httply.get("https://www.google.com/webhp", parameters: {hl: :en} | 
| 60 | 
            +
            response  =   Httply.get("https://www.google.com/webhp", parameters: {hl: :en})
         | 
| 52 61 | 
             
            ```
         | 
| 53 62 |  | 
| 54 63 | 
             
            response.body will now return a Nokogiri::HTML::Document
         | 
    
        data/lib/httply/client.rb
    CHANGED
    
    | @@ -1,63 +1,64 @@ | |
| 1 1 | 
             
            module Httply
         | 
| 2 2 | 
             
              class Client
         | 
| 3 3 | 
             
                attr_accessor :host, :configuration
         | 
| 4 | 
            +
                attr_accessor :memoize, :connection
         | 
| 4 5 |  | 
| 5 6 | 
             
                include ::Httply::Proxies
         | 
| 6 7 |  | 
| 7 | 
            -
                def initialize(host: nil, configuration: ::Httply.configuration)
         | 
| 8 | 
            -
                  self.host             =   host
         | 
| 8 | 
            +
                def initialize(host: nil, configuration: ::Httply.configuration, memoize: false)
         | 
| 9 | 
            +
                  self.host             =   ::Httply::Utilities::Uri.correct_host(host)
         | 
| 9 10 | 
             
                  self.configuration    =   configuration
         | 
| 11 | 
            +
                  self.memoize          =   memoize
         | 
| 12 | 
            +
                  self.connection       =   nil
         | 
| 10 13 | 
             
                end
         | 
| 11 14 |  | 
| 12 | 
            -
                def  | 
| 13 | 
            -
                   | 
| 14 | 
            -
                  
         | 
| 15 | 
            -
                  if path !~ /^http(s)?:\/\// && !self.host.to_s.empty?
         | 
| 16 | 
            -
                    host_part           =   self.host =~ /^http(s)?:\/\// ? self.host : "https://#{self.host}"
         | 
| 17 | 
            -
                    path                =   "#{host_part}/#{path}"
         | 
| 18 | 
            -
                  end
         | 
| 19 | 
            -
                  
         | 
| 20 | 
            -
                  return path
         | 
| 15 | 
            +
                def setup(host: nil, headers: {}, options: {})
         | 
| 16 | 
            +
                  self.connection     ||=   configure(host: host, headers: headers, options: options)
         | 
| 21 17 | 
             
                end
         | 
| 22 18 |  | 
| 23 | 
            -
                def get(path, parameters: {}, headers: {}, options: {} | 
| 24 | 
            -
                  request path, method: :get, parameters: parameters, headers: headers, options: options | 
| 19 | 
            +
                def get(path, parameters: {}, headers: {}, options: {})
         | 
| 20 | 
            +
                  request path, method: :get, parameters: parameters, headers: headers, options: options
         | 
| 25 21 | 
             
                end
         | 
| 26 22 |  | 
| 27 | 
            -
                def head(path, parameters: {}, headers: {}, options: {} | 
| 28 | 
            -
                  request path, method: :head, parameters: parameters, headers: headers, options: options | 
| 23 | 
            +
                def head(path, parameters: {}, headers: {}, options: {})
         | 
| 24 | 
            +
                  request path, method: :head, parameters: parameters, headers: headers, options: options
         | 
| 29 25 | 
             
                end
         | 
| 30 26 |  | 
| 31 | 
            -
                def post(path, parameters: {}, data: {}, headers: {}, options: {} | 
| 32 | 
            -
                  request path, method: :post, parameters: parameters, data: data, headers: headers, options: options | 
| 27 | 
            +
                def post(path, parameters: {}, data: {}, headers: {}, options: {})
         | 
| 28 | 
            +
                  request path, method: :post, parameters: parameters, data: data, headers: headers, options: options
         | 
| 33 29 | 
             
                end
         | 
| 34 30 |  | 
| 35 | 
            -
                def put(path, parameters: {}, data: {}, headers: {}, options: {} | 
| 36 | 
            -
                  request path, method: :put, parameters: parameters, data: data, headers: headers, options: options | 
| 31 | 
            +
                def put(path, parameters: {}, data: {}, headers: {}, options: {})
         | 
| 32 | 
            +
                  request path, method: :put, parameters: parameters, data: data, headers: headers, options: options
         | 
| 37 33 | 
             
                end
         | 
| 38 34 |  | 
| 39 | 
            -
                def patch(path, parameters: {}, data: {}, headers: {}, options: {} | 
| 40 | 
            -
                  request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options | 
| 35 | 
            +
                def patch(path, parameters: {}, data: {}, headers: {}, options: {})
         | 
| 36 | 
            +
                  request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options
         | 
| 41 37 | 
             
                end
         | 
| 42 38 |  | 
| 43 | 
            -
                def delete(path, parameters: {}, data: {}, headers: {}, options: {} | 
| 44 | 
            -
                  request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options | 
| 39 | 
            +
                def delete(path, parameters: {}, data: {}, headers: {}, options: {})
         | 
| 40 | 
            +
                  request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options
         | 
| 45 41 | 
             
                end
         | 
| 46 42 |  | 
| 47 | 
            -
                def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {} | 
| 48 | 
            -
                   | 
| 43 | 
            +
                def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {})
         | 
| 44 | 
            +
                  host                      =   !self.host.to_s.empty? ? self.host : ::Httply::Utilities::Uri.parse_host(path)
         | 
| 45 | 
            +
                  path                      =   ::Httply::Utilities::Uri.to_path(path)
         | 
| 46 | 
            +
                  connection                =   self.memoize ? setup(host: host, headers: headers, options: options) : configure(host: host, headers: headers, options: options)
         | 
| 49 47 |  | 
| 50 48 | 
             
                  response                  =   case method
         | 
| 51 49 | 
             
                    when :get
         | 
| 52 50 | 
             
                      connection.get do |request|
         | 
| 51 | 
            +
                        request.url path
         | 
| 53 52 | 
             
                        request.params      =   parameters if parameters && !parameters.empty?
         | 
| 54 53 | 
             
                      end
         | 
| 55 54 | 
             
                    when :head
         | 
| 56 55 | 
             
                      connection.head do |request|
         | 
| 56 | 
            +
                        request.url path
         | 
| 57 57 | 
             
                        request.params      =   parameters if parameters && !parameters.empty?
         | 
| 58 58 | 
             
                      end
         | 
| 59 59 | 
             
                    when :post, :put, :patch, :delete
         | 
| 60 60 | 
             
                      connection.send(method) do |request|
         | 
| 61 | 
            +
                        request.url path
         | 
| 61 62 | 
             
                        request.body        =   data if data && !data.empty?
         | 
| 62 63 | 
             
                        request.params      =   parameters if parameters && !parameters.empty?
         | 
| 63 64 | 
             
                      end
         | 
| @@ -66,35 +67,35 @@ module Httply | |
| 66 67 | 
             
                  return response
         | 
| 67 68 | 
             
                end
         | 
| 68 69 |  | 
| 69 | 
            -
                def  | 
| 70 | 
            +
                def configure(host:, headers: {}, options: {})
         | 
| 70 71 | 
             
                  client_options            =   options.fetch(:client, {})
         | 
| 71 | 
            -
                  follow_redirects          =   options.fetch(:follow_redirects, true)
         | 
| 72 | 
            -
                  redirect_limit            =   options.fetch(:redirects_limit, 10)
         | 
| 73 | 
            -
                  proxy                     =   determine_proxy(options.fetch(:proxy, nil))
         | 
| 74 72 |  | 
| 75 | 
            -
                   | 
| 73 | 
            +
                  request_options           =   options.fetch(:request, {})
         | 
| 74 | 
            +
                  redirects                 =   request_options.fetch(:redirects, 10)
         | 
| 76 75 |  | 
| 77 | 
            -
                   | 
| 76 | 
            +
                  proxy                     =   determine_proxy(options.fetch(:proxy, nil))
         | 
| 78 77 |  | 
| 79 | 
            -
                   | 
| 78 | 
            +
                  headers["User-Agent"]     =   headers.fetch("User-Agent", ::Agents.random_user_agent(options.fetch(:user_agent_device, :desktop)))
         | 
| 79 | 
            +
                  
         | 
| 80 | 
            +
                  connection                =   ::Faraday.new(host, client_options) do |builder|
         | 
| 80 81 | 
             
                    builder.options[:timeout]         =   options.fetch(:timeout, nil)      if options.fetch(:timeout, nil)
         | 
| 81 82 | 
             
                    builder.options[:open_timeout]    =   options.fetch(:open_timeout, nil) if options.fetch(:open_timeout, nil)
         | 
| 82 83 |  | 
| 83 84 | 
             
                    builder.headers         =   headers
         | 
| 84 85 |  | 
| 85 | 
            -
                    builder.request | 
| 86 | 
            -
                    
         | 
| 87 | 
            -
                    builder.response :logger if self.configuration.verbose
         | 
| 86 | 
            +
                    builder.request  :url_encoded if request_options.fetch(:url_encoded, false)
         | 
| 87 | 
            +
                    builder.request  :json        if request_options.fetch(:json, false)
         | 
| 88 88 |  | 
| 89 | 
            -
                    builder.response : | 
| 90 | 
            -
                    builder.response : | 
| 91 | 
            -
                    builder. | 
| 89 | 
            +
                    builder.response :logger      if self.configuration.verbose
         | 
| 90 | 
            +
                    builder.response :xml,  content_type: /\bxml$/
         | 
| 91 | 
            +
                    builder.response :json, content_type: /\bjson$/
         | 
| 92 | 
            +
                    builder.use ::Httply::Middlewares::ParseHtml, content_type: /\btext\/html$/
         | 
| 92 93 |  | 
| 93 | 
            -
                    builder.use ::FaradayMiddleware::FollowRedirects, limit:  | 
| 94 | 
            +
                    builder.use ::FaradayMiddleware::FollowRedirects, limit: redirects if redirects && redirects > 0
         | 
| 94 95 |  | 
| 95 96 | 
             
                    if proxy
         | 
| 96 97 | 
             
                      builder.proxy         =   generate_faraday_proxy(proxy)
         | 
| 97 | 
            -
                       | 
| 98 | 
            +
                      log("Will use proxy: #{builder.proxy.inspect}")
         | 
| 98 99 | 
             
                    end
         | 
| 99 100 |  | 
| 100 101 | 
             
                    builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
         | 
| @@ -103,5 +104,9 @@ module Httply | |
| 103 104 | 
             
                  return connection
         | 
| 104 105 | 
             
                end
         | 
| 105 106 |  | 
| 107 | 
            +
                def log(message)
         | 
| 108 | 
            +
                  puts "[Httply::Client] - #{message}" if self.configuration.verbose
         | 
| 109 | 
            +
                end
         | 
| 110 | 
            +
                
         | 
| 106 111 | 
             
              end
         | 
| 107 112 | 
             
            end
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            module Httply
         | 
| 2 | 
            +
              module Utilities
         | 
| 3 | 
            +
                class Uri
         | 
| 4 | 
            +
                  class << self
         | 
| 5 | 
            +
                    
         | 
| 6 | 
            +
                    def correct_host(host)
         | 
| 7 | 
            +
                      if !host.to_s.empty?
         | 
| 8 | 
            +
                        host                    =   host =~ /^http(s)?:\/\//i ? host : "https://#{host}"
         | 
| 9 | 
            +
                      end
         | 
| 10 | 
            +
                  
         | 
| 11 | 
            +
                      return host
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                
         | 
| 14 | 
            +
                    def parse_host(url)
         | 
| 15 | 
            +
                      host                      =   nil
         | 
| 16 | 
            +
                  
         | 
| 17 | 
            +
                      if host.to_s.empty? && url =~ /^http(s)?:\/\//
         | 
| 18 | 
            +
                        uri                     =   URI(url)
         | 
| 19 | 
            +
                        host                    =   "#{uri.scheme}://#{uri.host}"
         | 
| 20 | 
            +
                      end
         | 
| 21 | 
            +
                  
         | 
| 22 | 
            +
                      return host
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                
         | 
| 25 | 
            +
                    def to_path(path)
         | 
| 26 | 
            +
                      path                      =   path =~ /^http(s)?:\/\// ? URI(path).path : path
         | 
| 27 | 
            +
                      path                      =   path =~ /^\// ? path : "/#{path}"
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                    
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
            end
         | 
    
        data/lib/httply/version.rb
    CHANGED
    
    
    
        data/lib/httply.rb
    CHANGED
    
    | @@ -2,11 +2,15 @@ require "faraday" | |
| 2 2 | 
             
            require "faraday_middleware"
         | 
| 3 3 | 
             
            require "agents"
         | 
| 4 4 |  | 
| 5 | 
            +
            require "uri"
         | 
| 6 | 
            +
             | 
| 5 7 | 
             
            require "httply/version"
         | 
| 6 8 | 
             
            require "httply/configuration"
         | 
| 7 9 |  | 
| 8 10 | 
             
            require "httply/middlewares/html"
         | 
| 9 11 |  | 
| 12 | 
            +
            require "httply/utilities/uri"
         | 
| 13 | 
            +
             | 
| 10 14 | 
             
            require "httply/proxies"
         | 
| 11 15 | 
             
            require "httply/client"
         | 
| 12 16 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: httply
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.5
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Sebastian Johnsson
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2019- | 
| 11 | 
            +
            date: 2019-06-24 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: faraday
         | 
| @@ -162,6 +162,7 @@ files: | |
| 162 162 | 
             
            - lib/httply/configuration.rb
         | 
| 163 163 | 
             
            - lib/httply/middlewares/html.rb
         | 
| 164 164 | 
             
            - lib/httply/proxies.rb
         | 
| 165 | 
            +
            - lib/httply/utilities/uri.rb
         | 
| 165 166 | 
             
            - lib/httply/version.rb
         | 
| 166 167 | 
             
            homepage: https://github.com/SebastianJ/httply
         | 
| 167 168 | 
             
            licenses:
         |