Class: Yawast::Shared::Http
- Inherits:
-
Object
- Object
- Yawast::Shared::Http
- Defined in:
- lib/shared/http.rb
Class Method Summary collapse
- .check_not_found(uri, file) ⇒ Object
- .get(uri, headers = nil) ⇒ Object
-
.get_headers(extra_headers = nil) ⇒ Object
noinspection RubyStringKeysInHashInspection.
- .get_http(uri) ⇒ Object
- .get_json(uri) ⇒ Object
- .get_status_code(uri) ⇒ Object
- .get_with_code(uri, headers = nil) ⇒ Object
- .head(uri) ⇒ Object
- .put(uri, body, headers = nil) ⇒ Object
- .setup(proxy, cookie) ⇒ Object
Class Method Details
.check_not_found(uri, file) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/shared/http.rb', line 107 def self.check_not_found(uri, file) fake_uri = uri.copy fake_uri.path = if file "/#{SecureRandom.hex}.html" else "/#{SecureRandom.hex}/" end if Yawast::Shared::Http.get_status_code(fake_uri) != '404' # crazy 404 handling return false end true end |
.get(uri, headers = nil) ⇒ Object
55 56 57 |
# File 'lib/shared/http.rb', line 55 def self.get(uri, headers = nil) get_with_code(uri, headers)[:body] end |
.get_headers(extra_headers = nil) ⇒ Object
noinspection RubyStringKeysInHashInspection
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/shared/http.rb', line 125 def self.get_headers(extra_headers = nil) headers = if @cookie.nil? {'User-Agent' => HTTP_UA} else {'User-Agent' => HTTP_UA, 'Cookie' => @cookie} end headers.merge! extra_headers unless extra_headers.nil? headers end |
.get_http(uri) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/shared/http.rb', line 97 def self.get_http(uri) req = if @proxy Net::HTTP.new(uri.host, uri.port, @proxy_host, @proxy_port) else Net::HTTP.new(uri.host, uri.port) end req end |
.get_json(uri) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/shared/http.rb', line 59 def self.get_json(uri) body = '' begin req = get_http(uri) req.use_ssl = uri.scheme == 'https' res = req.request_get(uri, {'User-Agent' => "YAWAST/#{Yawast::VERSION}"}) body = res.read_body rescue # rubocop:disable Style/RescueStandardError, Lint/HandleExceptions # do nothing for now end JSON.parse body end |
.get_status_code(uri) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/shared/http.rb', line 89 def self.get_status_code(uri) req = get_http(uri) req.use_ssl = uri.scheme == 'https' res = req.head(uri, get_headers) res.code end |
.get_with_code(uri, headers = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/shared/http.rb', line 38 def self.get_with_code(uri, headers = nil) body = '' code = nil begin req = get_http(uri) req.use_ssl = uri.scheme == 'https' res = req.request_get(uri, get_headers(headers)) body = res.read_body code = res.code rescue => e # rubocop:disable Style/RescueStandardError Yawast::Utilities.puts_error "Error sending request to #{uri} - '#{e.}'" end {body: body, code: code} end |
.head(uri) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/shared/http.rb', line 23 def self.head(uri) begin req = get_http(uri) req.use_ssl = uri.scheme == 'https' req.head(uri, get_headers) rescue # rubocop:disable Style/RescueStandardError # if we get here, the HEAD failed - but GET may work # so we silently fail back to using GET instead req = get_http(uri) req.use_ssl = uri.scheme == 'https' res = req.request_get(uri, get_headers) res end end |
.put(uri, body, headers = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/shared/http.rb', line 74 def self.put(uri, body, headers = nil) ret = nil begin req = get_http(uri) req.use_ssl = uri.scheme == 'https' res = req.request_put(uri, body, get_headers(headers)) ret = res.read_body rescue # rubocop:disable Style/RescueStandardError, Lint/HandleExceptions # do nothing for now end ret end |
.setup(proxy, cookie) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/shared/http.rb', line 9 def self.setup(proxy, ) if !proxy.nil? && proxy.include?(':') @proxy_host, @proxy_port = proxy.split(':') @proxy = true puts "Using Proxy: #{proxy}" else @proxy = false end @cookie = puts "Using Cookie: #{@cookie}" unless @cookie.nil? end |