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"

Instance Method Summary collapse

Constructor Details

#initializeCrawler


9
10
11
12
13
14
15
# File 'lib/apple_system_status/crawler.rb', line 9

def initialize
  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app)
  end
  @session = Capybara::Session.new(:poltergeist)
  @session.driver.headers = { "User-Agent" => USER_AGENT }
end

Instance Method Details

#apple_url(country) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/apple_system_status/crawler.rb', line 63

def apple_url(country)
  if country.blank? || 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:  }
  ]
}

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/apple_system_status/crawler.rb', line 28

def perform(country: nil, title: nil)
  @session.visit(apple_url(country))

  title_parts = [
    @session.find("#key_date_text").text,
    @session.find("#key_date").text,
    @session.find("#key_date_post").text,
  ]

  response = {
    title:    title_parts.join(" ").strip,
    services: [],
  }

  @session.all("#dashboard td").each_with_object(response[:services]) do |td, services|
    begin
      services << {
        title:       td.find("p[role='text']").text,
        description: td.find("p[role='text']")["aria-label"],
        status:      td.find("span")["class"],
      }
    rescue Capybara::ElementNotFound
      # NOTE: Capybara::Node::Matchers#has_css? is very slow!
    end
  end

  unless title.blank?
    response[:services].select! { |service| service[:title] == title }
  end

  response[:services].sort_by! { |service| service[:title] }

  response
end