Class: Miteru::HTTPClient
- Inherits:
-
Object
- Object
- Miteru::HTTPClient
- Defined in:
- lib/miteru/http_client.rb
Constant Summary collapse
- DEFAULT_UA =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
- URLSCAN_UA =
"miteru/#{Miteru::VERSION}".freeze
Instance Attribute Summary collapse
-
#ssl_context ⇒ Object
readonly
Returns the value of attribute ssl_context.
Class Method Summary collapse
- .download(url, base_dir = "/tmp") ⇒ Object
- .get(url, options = {}) ⇒ Object
- .head(url, options = {}) ⇒ Object
- .post(url, options = {}) ⇒ Object
Instance Method Summary collapse
- #download(url, destination) ⇒ Object
- #get(url, options = {}) ⇒ Object
- #head(url, options = {}) ⇒ Object
-
#initialize ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #post(url, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ HTTPClient
Returns a new instance of HTTPClient.
14 15 16 17 18 |
# File 'lib/miteru/http_client.rb', line 14 def initialize ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE @ssl_context = ctx end |
Instance Attribute Details
#ssl_context ⇒ Object (readonly)
Returns the value of attribute ssl_context.
12 13 14 |
# File 'lib/miteru/http_client.rb', line 12 def ssl_context @ssl_context end |
Class Method Details
.download(url, base_dir = "/tmp") ⇒ Object
49 50 51 |
# File 'lib/miteru/http_client.rb', line 49 def download(url, base_dir = "/tmp") new.download(url, base_dir) end |
.get(url, options = {}) ⇒ Object
53 54 55 |
# File 'lib/miteru/http_client.rb', line 53 def get(url, = {}) new.get url, end |
.head(url, options = {}) ⇒ Object
61 62 63 |
# File 'lib/miteru/http_client.rb', line 61 def head(url, = {}) new.head url, end |
.post(url, options = {}) ⇒ Object
57 58 59 |
# File 'lib/miteru/http_client.rb', line 57 def post(url, = {}) new.post url, end |
Instance Method Details
#download(url, destination) ⇒ Object
20 21 22 23 24 |
# File 'lib/miteru/http_client.rb', line 20 def download(url, destination) down = Down::Http.new(**) { |client| client.headers(**default_headers) } down.download(url, destination: destination) destination end |
#get(url, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/miteru/http_client.rb', line 35 def get(url, = {}) = .merge HTTP.follow .timeout(write: 2, connect: 5, read: 10) .headers(urlscan_url?(url) ? urlscan_headers : default_headers) .get(url, ) end |
#head(url, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/miteru/http_client.rb', line 26 def head(url, = {}) = .merge HTTP.follow .timeout(3) .headers(urlscan_url?(url) ? urlscan_headers : default_headers) .head(url, ) end |
#post(url, options = {}) ⇒ Object
44 45 46 |
# File 'lib/miteru/http_client.rb', line 44 def post(url, = {}) HTTP.post url, end |