Class: Browze::Client
Overview
Desktop and mobile clients inherit from the base client.
Defined Under Namespace
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#headers ⇒ Hash
Get the updated headers.
Instance Method Summary collapse
-
#download(url, filename: nil) ⇒ Object
Download a file with an optional filename.
-
#get(url) ⇒ Browze::Response
Perform a GET request to the given url.
-
#initialize ⇒ Client
constructor
Initialize the cookie jar and headers.
-
#ip ⇒ String
Get the current public ip address.
-
#location ⇒ String
Return the current location based on the public ip address.
-
#post(url, body_hash) ⇒ Browze::Response
Perform a POST request to the given url with a request body.
-
#user_agent ⇒ String
Pick a random user agent and persist it in the instance.
Constructor Details
#initialize ⇒ Client
Initialize the cookie jar and headers.
18 19 20 21 |
# File 'lib/browze/client.rb', line 18 def initialize @cookies = CookieHash.new @headers = {} end |
Instance Attribute Details
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
14 15 16 |
# File 'lib/browze/client.rb', line 14 def @cookies end |
#headers ⇒ Hash
Get the updated headers.
63 64 65 |
# File 'lib/browze/client.rb', line 63 def headers @headers.merge({ 'User-Agent' => user_agent, 'Cookie' => @cookies. }) end |
Instance Method Details
#download(url, filename: nil) ⇒ Object
Download a file with an optional filename.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/browze/client.rb', line 48 def download(url, filename: nil) = TTY::ProgressBar.new('Downloading... [:bar] :percent') tempfile = Down.download( url, content_length_proc: ->(content_length) { .update(total: content_length) }, progress_proc: ->(progress) { .current = progress } ) filename ||= tempfile.original_filename FileUtils.mv(tempfile, filename) puts "Download complete: #{filename} (#{tempfile.size} bytes)" end |
#get(url) ⇒ Browze::Response
Perform a GET request to the given url.
27 28 29 30 31 |
# File 'lib/browze/client.rb', line 27 def get(url) resp = Browze::Response.new(self.class.get(url, headers: headers)) resp..each { |c| @cookies.(c) } resp end |
#ip ⇒ String
Get the current public ip address.
77 78 79 |
# File 'lib/browze/client.rb', line 77 def ip @ip ||= get('http://whatismyip.akamai.com').body end |
#location ⇒ String
Return the current location based on the public ip address.
84 85 86 87 |
# File 'lib/browze/client.rb', line 84 def location l = Geocoder.search(ip).first "#{l.city}, #{l.country}" end |
#post(url, body_hash) ⇒ Browze::Response
Perform a POST request to the given url with a request body.
38 39 40 41 42 |
# File 'lib/browze/client.rb', line 38 def post(url, body_hash) resp = Browze::Response.new(self.class.post(url, body: body_hash, headers: headers)) resp..each { |c| @cookies.(c) } resp end |
#user_agent ⇒ String
Pick a random user agent and persist it in the instance.
70 71 72 |
# File 'lib/browze/client.rb', line 70 def user_agent @user_agent ||= self.class::USER_AGENTS.sample end |