httply 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +96 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/httply.gemspec +38 -0
- data/lib/httply/client.rb +105 -0
- data/lib/httply/configuration.rb +11 -0
- data/lib/httply/middlewares/html.rb +17 -0
- data/lib/httply/proxies.rb +119 -0
- data/lib/httply/version.rb +3 -0
- data/lib/httply.rb +38 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1d37057e1f9283c47105297acf72d2df5830d4ca52d7122bff2c0f553f59b52d
|
4
|
+
data.tar.gz: d9824a74d238b9e588ad55cfb4bf2c62df0a8de74d9b27595cd108bbf0c7e095
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e16a95b0bf80c052b067d3013c9f366c50b7b59b157ae4fe8ea776a6227baa8ad3a78f97fb8def082e7f99fe1effac453f85b2e8d5f2dd555ab62df60d64878
|
7
|
+
data.tar.gz: 38f3bf90fe397db2a2e053017346b1e550c3b84dc023ccefa6f06a6fba9620cf383945f83d0880f347685d97b6b9f53ae65a8b74b581a4fd84faff02348a9be0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.3
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sebastian.johnsson@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
httply (0.1.0)
|
5
|
+
agents (>= 0.1.4)
|
6
|
+
faraday (>= 0.15.4)
|
7
|
+
faraday_middleware (>= 0.13.1)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
agents (0.1.4)
|
13
|
+
coderay (1.1.2)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
faraday (0.15.4)
|
16
|
+
multipart-post (>= 1.2, < 3)
|
17
|
+
faraday_middleware (0.13.1)
|
18
|
+
faraday (>= 0.7.4, < 1.0)
|
19
|
+
method_source (0.9.2)
|
20
|
+
mini_portile2 (2.4.0)
|
21
|
+
multi_xml (0.6.0)
|
22
|
+
multipart-post (2.1.1)
|
23
|
+
nokogiri (1.10.3)
|
24
|
+
mini_portile2 (~> 2.4.0)
|
25
|
+
pry (0.12.2)
|
26
|
+
coderay (~> 1.1.0)
|
27
|
+
method_source (~> 0.9.0)
|
28
|
+
rake (10.5.0)
|
29
|
+
rspec (3.8.0)
|
30
|
+
rspec-core (~> 3.8.0)
|
31
|
+
rspec-expectations (~> 3.8.0)
|
32
|
+
rspec-mocks (~> 3.8.0)
|
33
|
+
rspec-core (3.8.0)
|
34
|
+
rspec-support (~> 3.8.0)
|
35
|
+
rspec-expectations (3.8.3)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.8.0)
|
38
|
+
rspec-mocks (3.8.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-support (3.8.0)
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
bundler (~> 1.17)
|
48
|
+
httply!
|
49
|
+
multi_xml (~> 0.6.0)
|
50
|
+
nokogiri (~> 1.10)
|
51
|
+
pry (~> 0.12.2)
|
52
|
+
rake (~> 10.0)
|
53
|
+
rspec (~> 3.0)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Sebastian Johnsson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Httply
|
2
|
+
|
3
|
+
Httply is a lightweight wrapper around Faraday to support automatic randomization of proxies and user agents, amongst other things.
|
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.
|
6
|
+
|
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
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'httply'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install httply
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Instance
|
28
|
+
|
29
|
+
Httply can either be used as an instance, e.g:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
client = Httply::Client.new(host: "https://www.google.com")
|
33
|
+
response = client.get("/webhp", parameters: {hl: :en})
|
34
|
+
```
|
35
|
+
|
36
|
+
### Class method
|
37
|
+
|
38
|
+
Or directly as a class method:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
response = Httply.get("https://www.google.com/webhp", parameters: {hl: :en})
|
42
|
+
```
|
43
|
+
|
44
|
+
### HTML parsing
|
45
|
+
|
46
|
+
HTML parsing requires that Nokogiri has been required elsewhere and is accessible for Httply.
|
47
|
+
|
48
|
+
You can also force parsing responses as HTML using Nokogiri:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
response = Httply.get("https://www.google.com/webhp", parameters: {hl: :en}, as: :html)
|
52
|
+
```
|
53
|
+
|
54
|
+
response.body will now return a Nokogiri::HTML::Document
|
55
|
+
|
56
|
+
### Proxies
|
57
|
+
|
58
|
+
Proxy usage:
|
59
|
+
|
60
|
+
Proxies can either be used in a normal fashion:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
response = Httply.get("https://www.google.com/webhp", parameters: {hl: :en}, as: :html, options: {proxy: {host: "127.0.0.1", port: 8080, username: "usRnaMe", password: "paswD"}})
|
64
|
+
```
|
65
|
+
|
66
|
+
Or be randomized on a per request basis with the help of [proxied](https://github.com/SebastianJ/proxied):
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
response = Httply.get("https://www.google.com/webhp", parameters: {hl: :en}, as: :html, options: {proxy: {randomize: true, protocol: :http, type: :private, category: :private_proxies_x}})
|
70
|
+
```
|
71
|
+
|
72
|
+
Proxy options for randomization are as follows:
|
73
|
+
|
74
|
+
- randomize: if proxies should be randomized using [proxied](https://github.com/SebastianJ/proxied)
|
75
|
+
- protocol: what protocol to use (http or socks), defaults to :http
|
76
|
+
- type: what type of proxies should be used (public or private), defaults to nil
|
77
|
+
- category: a specific category to use to further filter proxies from the database, defaults to nil
|
78
|
+
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SebastianJ/httply. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
93
|
+
|
94
|
+
## Code of Conduct
|
95
|
+
|
96
|
+
Everyone interacting in the Httply project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/SebastianJ/httply/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "httply"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.config.history.file = File.join(__FILE__, "../.pry_history")
|
12
|
+
Pry.start
|
13
|
+
|
14
|
+
#require "irb"
|
15
|
+
#IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/httply.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "httply/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "httply"
|
8
|
+
spec.version = Httply::VERSION
|
9
|
+
spec.authors = ["Sebastian Johnsson"]
|
10
|
+
spec.email = ["sebastian.johnsson@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Httply - lightweight Faraday wrapper}
|
13
|
+
spec.description = %q{Httply is a lightweight wrapper around Faraday to support automatic randomization of proxies and user agents, amongst other things.}
|
14
|
+
spec.homepage = "https://github.com/SebastianJ/httply"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "faraday", ">= 0.15.4"
|
27
|
+
spec.add_dependency "faraday_middleware", ">= 0.13.1"
|
28
|
+
spec.add_dependency "agents", ">= 0.1.4"
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "pry", "~> 0.12.2"
|
35
|
+
|
36
|
+
spec.add_development_dependency "nokogiri", "~> 1.10"
|
37
|
+
spec.add_development_dependency "multi_xml", "~> 0.6.0"
|
38
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module Httply
|
2
|
+
class Client
|
3
|
+
attr_accessor :host, :configuration
|
4
|
+
|
5
|
+
include ::Httply::Proxies
|
6
|
+
|
7
|
+
def initialize(host: nil, configuration: ::Httply.configuration)
|
8
|
+
self.host = host
|
9
|
+
self.configuration = configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_uri(path)
|
13
|
+
path = path.gsub(/^\//i, "")
|
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
|
21
|
+
end
|
22
|
+
|
23
|
+
def get(path, parameters: {}, headers: {}, options: {}, as: nil)
|
24
|
+
request path, method: :get, parameters: parameters, headers: headers, options: options, as: as
|
25
|
+
end
|
26
|
+
|
27
|
+
def head(path, parameters: {}, headers: {}, options: {}, as: nil)
|
28
|
+
request path, method: :head, parameters: parameters, headers: headers, options: options, as: as
|
29
|
+
end
|
30
|
+
|
31
|
+
def post(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
|
32
|
+
request path, method: :post, parameters: parameters, data: data, headers: headers, options: options, as: as
|
33
|
+
end
|
34
|
+
|
35
|
+
def put(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
|
36
|
+
request path, method: :put, parameters: parameters, data: data, headers: headers, options: options, as: as
|
37
|
+
end
|
38
|
+
|
39
|
+
def patch(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
|
40
|
+
request path, method: :patch, parameters: parameters, data: data, headers: headers, options: options, as: as
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(path, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
|
44
|
+
request path, method: :delete, parameters: parameters, data: data, headers: headers, options: options, as: as
|
45
|
+
end
|
46
|
+
|
47
|
+
def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, as: nil)
|
48
|
+
connection = setup(path, headers: headers, options: options, as: as)
|
49
|
+
|
50
|
+
response = case method
|
51
|
+
when :get
|
52
|
+
connection.get do |request|
|
53
|
+
request.parameters = parameters if parameters && !parameters.empty?
|
54
|
+
end
|
55
|
+
when :head
|
56
|
+
connection.head do |request|
|
57
|
+
request.parameters = parameters if parameters && !parameters.empty?
|
58
|
+
end
|
59
|
+
when :post, :put, :patch, :delete
|
60
|
+
connection.send(method) do |request|
|
61
|
+
request.body = data if data && !data.empty?
|
62
|
+
request.parameters = parameters if parameters && !parameters.empty?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
return response
|
67
|
+
end
|
68
|
+
|
69
|
+
def setup(path, headers: {}, options: {}, as: nil)
|
70
|
+
client_options = options.fetch(:client, {})
|
71
|
+
follow_redirects = options.fetch(:follow_redirects, false)
|
72
|
+
redirect_limit = options.fetch(:redirects_limit, 10)
|
73
|
+
proxy = determine_proxy(options.fetch(:proxy, nil))
|
74
|
+
|
75
|
+
url = to_uri(path)
|
76
|
+
|
77
|
+
headers = {"User-Agent" => ::Agents.random_user_agent(options.fetch(:user_agent_device, :desktop))}.merge(headers)
|
78
|
+
|
79
|
+
connection = ::Faraday.new(url, client_options) do |builder|
|
80
|
+
builder.options[:timeout] = options.fetch(:timeout, nil) if options.fetch(:timeout, nil)
|
81
|
+
builder.options[:open_timeout] = options.fetch(:open_timeout, nil) if options.fetch(:open_timeout, nil)
|
82
|
+
|
83
|
+
builder.headers = headers
|
84
|
+
|
85
|
+
builder.response :logger if self.configuration.verbose
|
86
|
+
|
87
|
+
builder.response :xml, content_type: /\bxml$/ if as.eql?(:xml)
|
88
|
+
builder.response :json, content_type: /\bjson$/ if as.eql?(:json)
|
89
|
+
builder.use ::Httply::Middlewares::ParseHtml if as.eql?(:html)
|
90
|
+
|
91
|
+
builder.use ::FaradayMiddleware::FollowRedirects, limit: redirect_limit if follow_redirects && redirect_limit && redirect_limit > 0
|
92
|
+
|
93
|
+
if proxy
|
94
|
+
builder.proxy = generate_faraday_proxy(proxy)
|
95
|
+
puts "[Httply::Client] - Will use proxy: #{builder.proxy.inspect}" if self.configuration.verbose
|
96
|
+
end
|
97
|
+
|
98
|
+
builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
|
99
|
+
end
|
100
|
+
|
101
|
+
return connection
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'faraday_middleware/response_middleware'
|
2
|
+
|
3
|
+
module Httply
|
4
|
+
module Middlewares
|
5
|
+
# Public: parses response bodies with Nokogiri.
|
6
|
+
class ParseHtml < ::FaradayMiddleware::ResponseMiddleware
|
7
|
+
dependency 'nokogiri'
|
8
|
+
|
9
|
+
define_parser do |body, parser_options|
|
10
|
+
::Nokogiri::HTML(body, nil, "utf-8")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# deprecated alias
|
17
|
+
Faraday::Response::ParseHtml = Httply::Middlewares::ParseHtml
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module Httply
|
2
|
+
module Proxies
|
3
|
+
|
4
|
+
def determine_proxy(options)
|
5
|
+
proxy = nil
|
6
|
+
|
7
|
+
if options
|
8
|
+
proxy ||= {}
|
9
|
+
|
10
|
+
if options.is_a?(String)
|
11
|
+
proxy = proxy_from_string(options, proxy)
|
12
|
+
|
13
|
+
elsif options.is_a?(Hash) && !options.empty?
|
14
|
+
proxy = proxy_from_hash(options, proxy)
|
15
|
+
|
16
|
+
elsif options.is_a?(Array) && options.any?
|
17
|
+
proxy = proxy_from_array(options, proxy)
|
18
|
+
|
19
|
+
elsif proxy_model_defined? && options.is_a?(::Proxy)
|
20
|
+
proxy = proxy_from_object(options, proxy)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
return proxy
|
25
|
+
end
|
26
|
+
|
27
|
+
def proxy_from_string(options, proxy)
|
28
|
+
options = options.gsub(/^http(s)?:\/\//i, "")
|
29
|
+
parts = options.split(":")
|
30
|
+
|
31
|
+
if parts.size.eql?(2)
|
32
|
+
proxy[:host] = parts.first
|
33
|
+
proxy[:port] = parts.second.to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
return proxy
|
37
|
+
end
|
38
|
+
|
39
|
+
def proxy_from_hash(options, proxy)
|
40
|
+
host = options.fetch(:host, nil)
|
41
|
+
port = options.fetch(:port, nil)
|
42
|
+
|
43
|
+
username = options.fetch(:username, nil)
|
44
|
+
password = options.fetch(:password, nil)
|
45
|
+
|
46
|
+
randomize = options.fetch(:randomize, true)
|
47
|
+
type = options.fetch(:type, :all)
|
48
|
+
protocol = options.fetch(:protocol, :all)
|
49
|
+
|
50
|
+
if randomize && proxy_model_defined?
|
51
|
+
proxy_object = ::Proxy.get_random_proxy(protocol: protocol, proxy_type: type)
|
52
|
+
proxy = proxy_from_object(proxy_object)
|
53
|
+
else
|
54
|
+
if host && port
|
55
|
+
proxy[:host] = host
|
56
|
+
proxy[:port] = port
|
57
|
+
|
58
|
+
proxy = set_credentials(username, password, proxy)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
return proxy
|
63
|
+
end
|
64
|
+
|
65
|
+
def proxy_from_array(options)
|
66
|
+
item = options.sample
|
67
|
+
|
68
|
+
if item.is_a?(String)
|
69
|
+
proxy = proxy_from_string(item, proxy)
|
70
|
+
elsif item.is_a?(Hash) && !item.empty?
|
71
|
+
proxy = proxy_from_hash(item, proxy)
|
72
|
+
elsif proxy_model_defined? && item.is_a?(::Proxy)
|
73
|
+
proxy = proxy_from_object(item, proxy)
|
74
|
+
end
|
75
|
+
|
76
|
+
return proxy
|
77
|
+
end
|
78
|
+
|
79
|
+
def proxy_from_object(proxy_object, proxy)
|
80
|
+
if proxy_object
|
81
|
+
proxy[:host] = proxy_object.host
|
82
|
+
proxy[:port] = proxy_object.port
|
83
|
+
username = !proxy_object.username.to_s.empty? ? proxy_object.username : nil
|
84
|
+
password = !proxy_object.password.to_s.empty? ? proxy_object.password : nil
|
85
|
+
|
86
|
+
proxy = set_credentials(username, password, proxy)
|
87
|
+
end
|
88
|
+
|
89
|
+
return proxy
|
90
|
+
end
|
91
|
+
|
92
|
+
def set_credentials(username, password, proxy)
|
93
|
+
proxy[:username] = username unless username.to_s.empty?
|
94
|
+
proxy[:password] = password unless password.to_s.empty?
|
95
|
+
|
96
|
+
return proxy
|
97
|
+
end
|
98
|
+
|
99
|
+
def proxy_model_defined?
|
100
|
+
defined = Module.const_get("Proxy").is_a?(Class) rescue false
|
101
|
+
defined = (defined && ::Proxy.respond_to?(:get_random_proxy))
|
102
|
+
|
103
|
+
return defined
|
104
|
+
end
|
105
|
+
|
106
|
+
def generate_faraday_proxy(proxy)
|
107
|
+
proxy_options = {}
|
108
|
+
|
109
|
+
if proxy && !proxy[:host].to_s.empty? && !proxy[:port].to_s.empty?
|
110
|
+
proxy_options[:uri] = "http://#{proxy[:host]}:#{proxy[:port]}"
|
111
|
+
proxy_options[:user] = proxy[:username] unless proxy[:username].to_s.empty?
|
112
|
+
proxy_options[:password] = proxy[:password] unless proxy[:password].to_s.empty?
|
113
|
+
end
|
114
|
+
|
115
|
+
return proxy_options
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
data/lib/httply.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "faraday_middleware"
|
3
|
+
require "agents"
|
4
|
+
|
5
|
+
require "httply/version"
|
6
|
+
require "httply/configuration"
|
7
|
+
|
8
|
+
require "httply/middlewares/html"
|
9
|
+
|
10
|
+
require "httply/proxies"
|
11
|
+
require "httply/client"
|
12
|
+
|
13
|
+
module Httply
|
14
|
+
class Error < StandardError; end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_writer :configuration
|
18
|
+
|
19
|
+
def configuration
|
20
|
+
@configuration ||= ::Httply::Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def reset
|
24
|
+
@configuration = ::Httply::Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def configure
|
28
|
+
yield(configuration)
|
29
|
+
end
|
30
|
+
|
31
|
+
[:get, :head, :post, :put, :patch, :delete].each do |http_verb|
|
32
|
+
define_method(http_verb) do |path, args|
|
33
|
+
::Httply::Client.new.send(http_verb, path, **args)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httply
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastian Johnsson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.15.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.15.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.13.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.13.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: agents
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.4
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.4
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.12.2
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.12.2
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: nokogiri
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.10'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.10'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: multi_xml
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.6.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.6.0
|
139
|
+
description: Httply is a lightweight wrapper around Faraday to support automatic randomization
|
140
|
+
of proxies and user agents, amongst other things.
|
141
|
+
email:
|
142
|
+
- sebastian.johnsson@gmail.com
|
143
|
+
executables: []
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".gitignore"
|
148
|
+
- ".rspec"
|
149
|
+
- ".ruby-version"
|
150
|
+
- ".travis.yml"
|
151
|
+
- CODE_OF_CONDUCT.md
|
152
|
+
- Gemfile
|
153
|
+
- Gemfile.lock
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- bin/console
|
158
|
+
- bin/setup
|
159
|
+
- httply.gemspec
|
160
|
+
- lib/httply.rb
|
161
|
+
- lib/httply/client.rb
|
162
|
+
- lib/httply/configuration.rb
|
163
|
+
- lib/httply/middlewares/html.rb
|
164
|
+
- lib/httply/proxies.rb
|
165
|
+
- lib/httply/version.rb
|
166
|
+
homepage: https://github.com/SebastianJ/httply
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
metadata: {}
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubygems_version: 3.0.3
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: Httply - lightweight Faraday wrapper
|
189
|
+
test_files: []
|