Class: Glib::JsonCrawler::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/glib/json_crawler/router.rb

Class Method Summary collapse

Class Method Details

.crawl_multiple(views, block) ⇒ Object



53
54
55
# File 'lib/glib/json_crawler/router.rb', line 53

def self.crawl_multiple(views, block)
  @@visitor.traverse_multiple views, block
end

.log(action, url, response = nil) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/glib/json_crawler/router.rb', line 4

def self.log(action, url, response = nil)
  @@logger.puts '  ' * @@depth + [
    action,
    response.present? ? response.code : nil,
    url
  ].compact.join(' :: ')
end

.set_up(logger) ⇒ Object



12
13
14
15
16
# File 'lib/glib/json_crawler/router.rb', line 12

def self.set_up(logger)
  @@depth = -1
  @@logger = logger
  @@visitor = Glib::Json::Traversal::Visitor.new
end

.step(http, args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glib/json_crawler/router.rb', line 18

def self.step(http, args)
  if args.is_a?(Hash) && args['rel'] != 'nofollow'
    if (onClick = (args.fetch('onClick', nil) || args.fetch('onResponse', nil)))
      action = onClick['action']
      params = onClick
    end

    if action.present?
      @@depth += 1
      child = case action
      when 'initiate_navigation'
        JsonCrawler::NavInitiate.new(http, params, action)
      when 'windows/open-v1', 'dialogs/open-v1'
        JsonCrawler::WindowsOpen.new(http, params, action)
      when 'http/delete-v1'
        JsonCrawler::ActionHttp.new(:delete, http, params, action)
      when 'http/post-v1'
        JsonCrawler::ActionHttp.new(:post, http, params, action)
      when 'forms/submit-v1'
        forms = @@visitor.forms
        raise 'Submit action needs to be inside a form' if forms.size < 1
        JsonCrawler::FormsSubmit.new(http, params, action, forms.last)
      else
        self.log action, params['url']
      end
      @@depth -= 1
      child
    end
  end
end

.tear_downObject



49
50
51
# File 'lib/glib/json_crawler/router.rb', line 49

def self.tear_down
  @@logger.close
end