Class: AppleSystemStatus::Crawler

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil) ⇒ Crawler

Returns a new instance of Crawler.

Parameters:

  • chrome_options_args (Array<String>) (defaults to: DEFAULT_CHROME_OPTIONS_ARGS)
  • chrome_options_binary (String) (defaults to: nil)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apple_system_status/crawler.rb', line 20

def initialize(chrome_options_args: DEFAULT_CHROME_OPTIONS_ARGS, chrome_options_binary: nil)
  Capybara.register_driver :chrome_headless do |app|
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.read_timeout = 120

    chrome_options = { args: chrome_options_args }
    chrome_options[:binary] = chrome_options_binary if chrome_options_binary

    opts = Selenium::WebDriver::Chrome::Options.new(profile: nil, **chrome_options)

    Capybara::Selenium::Driver.new(
      app,
      browser: :chrome,
      options: opts,
      http_client: client,
    )
  end
  @session = Capybara::Session.new(:chrome_headless)
end

Class Method Details

.blank_string?(str) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
# File 'lib/apple_system_status/crawler.rb', line 115

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

Examples:

response format

{
  title: ,
  services: [
    { title: , description: , status:  }
  ]
}

Parameters:

  • country (String) (defaults to: nil)

    country code. (e.g. jp, ca, fr. default. us)

  • title (String) (defaults to: nil)

    If specified, narrow the service title

  • chrome_options_args (Array<String>) (defaults to: DEFAULT_CHROME_OPTIONS_ARGS)
  • chrome_options_binary (String) (defaults to: nil)

Returns:

  • (Hash)


108
109
110
111
112
113
# File 'lib/apple_system_status/crawler.rb', line 108

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_args, chrome_options_binary: chrome_options_binary)
  crawler.perform(country: country, title: title)
ensure
  crawler.quit!
end

Instance Method Details

#apple_url(country) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/apple_system_status/crawler.rb', line 86

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

Examples:

response format

{
  title: ,
  services: [
    { title: , description: , status:  }
  ]
}

Parameters:

  • country (String) (defaults to: nil)

    country code. (e.g. jp, ca, fr. default. us)

  • title (String) (defaults to: nil)

    If specified, narrow the service title

Returns:

  • (Hash)


55
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
# File 'lib/apple_system_status/crawler.rb', line 55

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



40
41
42
# File 'lib/apple_system_status/crawler.rb', line 40

def quit!
  @session.driver.quit if @session
end