Class: Glib::JsonCrawler::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Router

Returns a new instance of Router.



14
15
16
17
18
19
# File 'lib/glib/json_crawler/router.rb', line 14

def initialize(logger)
  @depth = -1
  @logger = logger
  @visitor = Glib::Json::Traversal::Visitor.new
  @read_only_actions = Set.new
end

Instance Attribute Details

#read_only_actionsObject (readonly)

Returns the value of attribute read_only_actions.



4
5
6
# File 'lib/glib/json_crawler/router.rb', line 4

def read_only_actions
  @read_only_actions
end

Instance Method Details

#crawl_multiple(views, block) ⇒ Object



91
92
93
# File 'lib/glib/json_crawler/router.rb', line 91

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

#follow(http, target_router) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/glib/json_crawler/router.rb', line 75

def follow(http, target_router)
  @depth += 1
  target_router.read_only_actions.each do |crawler_action|
    action, url = crawler_action
    http.get(url, action, {}, false)
  end
end

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



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

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

#process_action(http, spec) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/glib/json_crawler/router.rb', line 39

def process_action(http, spec)
  action = spec['action']
  params = spec

  if action.present?
    @depth += 1
    case action
    when 'initiate_navigation'
      @read_only_actions.add([action, params['url']])
      JsonCrawler::NavInitiate.new(http, params, action)
    when 'runMultiple-v1', 'runMultiple'
      JsonCrawler::RunMultiple.new(http, params, action)
    when 'windows/open-v1', 'dialogs/open-v1', 'windows/reload-v1', 'windows/open', 'dialogs/open', 'windows/reload'
      @read_only_actions.add([action, params['url']])
      JsonCrawler::WindowsOpen.new(http, params, action)
    when 'sheets/select-v1', 'sheets/select'
      JsonCrawler::Menu.new(http, params, action)
    when 'http/post-v1', 'http/post'
      JsonCrawler::ActionHttp.new(:post, http, params, action)
    when 'forms/submit-v1', 'forms/submit'
      forms = @visitor.forms
      # raise 'Submit action needs to be inside a form' if forms.size < 1
      JsonCrawler::FormsSubmit.new(http, forms.last)
    else
      unless [
        'http/delete-v1', 'dialogs/oauth-v1', 'windows/openWeb-v1',
        'http/delete', 'dialogs/oauth', 'windows/openWeb'
      ].include?(action)
        @read_only_actions.add([action, params['url']])
      end
      self.log action, params['url']
    end
    @depth -= 1
  end
end

#step(http, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/glib/json_crawler/router.rb', line 21

def step(http, args)
  # TODO: Refactor
  case args['view']
  when 'fields/submit-v1', 'fields/submit'
    @depth += 1
    forms = @visitor.forms
    JsonCrawler::FormsSubmit.new(http, forms.last)
    @depth -= 1
    return
  end

  if args.is_a?(Hash) && args['rel'] != 'nofollow'
    if (on_click = (args.fetch('onClick', nil)))
      process_action(http, on_click)
    end
  end
end

#tear_downObject



83
84
85
86
87
88
89
# File 'lib/glib/json_crawler/router.rb', line 83

def tear_down
  # NOTE: Experimental, to address issue where sometimes changes don't get saved.
  # FINDING: Doesn't work
  # @logger.flush

  @logger.close
end