Class: AppleSystemStatus::Crawler
- Inherits:
-
Object
- Object
- AppleSystemStatus::Crawler
- Defined in:
- lib/apple_system_status/crawler.rb
Constant Summary collapse
- USER_AGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"- DEFAULT_CHROME_OPTIONS_ARGS =
%W( headless disable-gpu window-size=1280,800 no-sandbox user-agent=#{USER_AGENT} ).freeze
- MAX_RETRY_COUNT =
5
Class Method Summary collapse
- .blank_string?(str) ⇒ Boolean
-
.perform(country: nil, title: nil, chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) ⇒ Hash
crawl apple system status page.
Instance Method Summary collapse
- #apple_url(country) ⇒ Object
-
#initialize(chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) ⇒ Crawler
constructor
A new instance of Crawler.
-
#perform(country: nil, title: nil) ⇒ Hash
crawl apple system status page.
- #quit! ⇒ Object
Constructor Details
#initialize(chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) ⇒ Crawler
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/apple_system_status/crawler.rb', line 20 def initialize(chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) .register_driver :chrome_headless do |app| client = Selenium::WebDriver::Remote::Http::Default.new client.read_timeout = 120 = { args: } [:binary] = if opts = Selenium::WebDriver::Chrome::Options.new(profile: nil, **) capabilities = Selenium::WebDriver::Remote::Capabilities.chrome ::Selenium::Driver.new( app, browser: :chrome, capabilities: [capabilities, opts], http_client: client, ) end @session = ::Session.new(:chrome_headless) end |
Class Method Details
.blank_string?(str) ⇒ Boolean
116 117 118 119 |
# File 'lib/apple_system_status/crawler.rb', line 116 def self.blank_string?(str) return true unless str str.strip.empty? end |
.perform(country: nil, title: nil, chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) ⇒ Hash
crawl apple system status page. When finished crawling, clear capybara session
109 110 111 112 113 114 |
# File 'lib/apple_system_status/crawler.rb', line 109 def self.perform(country: nil, title: nil, chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) crawler = AppleSystemStatus::Crawler.new(chrome_options_args: , chrome_options_binary: ) crawler.perform(country: country, title: title) ensure crawler.quit! end |
Instance Method Details
#apple_url(country) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/apple_system_status/crawler.rb', line 87 def apple_url(country) if self.class.blank_string?(country) || country == "us" "https://www.apple.com/support/systemstatus/" else "https://www.apple.com/#{country}/support/systemstatus/" end end |
#perform(country: nil, title: nil) ⇒ Hash
crawl apple system status page
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apple_system_status/crawler.rb', line 56 def perform(country: nil, title: nil) @session.visit(apple_url(country)) response = { title: @session.find(".section-date .date-copy").text.strip, services: [], } MAX_RETRY_COUNT.times do services = fetch_services if services.empty? # wait until the page is fully loaded sleep 1 else response[:services] = services break end end raise "Not found services" if response[:services].empty? unless self.class.blank_string?(title) response[:services].select! { |service| service[:title] == title } end response[:services].sort_by! { |service| service[:title] } response end |
#quit! ⇒ Object
41 42 43 |
# File 'lib/apple_system_status/crawler.rb', line 41 def quit! @session.driver.quit if @session end |