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

#initializeRouter

Returns a new instance of Router.



19
20
21
22
23
24
25
26
27
28
# File 'lib/glib/json_crawler/router.rb', line 19

def initialize
  @depth = -1
  @logger = ''
  @visitor = Glib::Json::Traversal::Visitor.new
  @read_only_actions = Set.new
  # default rails's development host
  @host ||= 'localhost:3000'
  @page_specs = []
  @page_urls = []
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#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

#_puts(text) ⇒ Object



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

def _puts(text)
  puts '  ' * @depth + text
end

#allowed?(url) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/glib/json_crawler/router.rb', line 137

def allowed?(url)
  regex = Regexp.new("#{host}.+(?<!\.pdf)$")
  regex.match(url)
end

#begin_page(spec, url) ⇒ Object



117
118
119
120
121
# File 'lib/glib/json_crawler/router.rb', line 117

def begin_page(spec, url)
  @page_specs << spec
  @page_urls << url
  @visitor.begin_page(spec)
end

#crawl_multiple(views, block) ⇒ Object



113
114
115
# File 'lib/glib/json_crawler/router.rb', line 113

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

#end_page(spec) ⇒ Object



123
124
125
126
127
# File 'lib/glib/json_crawler/router.rb', line 123

def end_page(spec)
  @page_specs.pop
  @page_urls.pop
  @visitor.end_page(spec)
end

#follow(http, target_routers) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/glib/json_crawler/router.rb', line 96

def follow(http, target_routers)
  if !target_routers.is_a?(Array)
    target_routers = [target_routers]
  end

  target_actions = Set.new
  target_routers.each do |router|
    target_actions += router.read_only_actions
  end

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

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



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

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

#page_specObject



129
130
131
# File 'lib/glib/json_crawler/router.rb', line 129

def page_spec
  @page_specs.last
end

#page_urlObject



133
134
135
# File 'lib/glib/json_crawler/router.rb', line 133

def page_url
  @page_urls.last
end

#process_action(http, spec) ⇒ Object



50
51
52
53
54
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
85
86
87
88
89
90
91
92
93
94
# File 'lib/glib/json_crawler/router.rb', line 50

def process_action(http, spec)
  action = spec&.fetch('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', 'windows/openWeb', 'windows/openWeb-v1'
      if allowed?(params['url'])
        @read_only_actions.add([action, params['url']])
        JsonCrawler::WindowsOpen.new(http, params, action)
      else
        self.log action, params['url']
      end
    when 'dialogs/show-v1', 'dialogs/show', 'popovers/show-v1', 'popovers/show'
      JsonCrawler::DialogsShow.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
      JsonCrawler::FormsSubmit.new(http, forms.last)
    when 'dialogs/alert-v1', 'dialogs/alert'
      JsonCrawler::DialogsAlert.new(http, params, action)
    else
      unless [
        'http/delete-v1',
        'dialogs/oauth-v1',
        'http/delete',
        'dialogs/oauth'
      ].include?(action)
        @read_only_actions.add([action, params['url']])
      end
      self.log action, params['url']
    end
    @depth -= 1
  end
end

#step(http, args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/glib/json_crawler/router.rb', line 30

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

  @read_only_actions.replace(@read_only_actions.sort_by { |e| e[1].to_s })
end