module Encounter::HTMLParser

parser for html pages @private

Public Class Methods

included(base) click to toggle source
# File lib/encounter/parser.rb, line 5
def self.included(base)
  base.extend Encounter::ParserClassMethods
end

Public Instance Methods

load_page(url, params = {}) click to toggle source

Returns Nokogiri object for URL

@param url [String] URL @param params [Hash] URL parameters

@return [Nokogiri::CSS::Node]

# File lib/encounter/parser.rb, line 114
def load_page(url, params = {})
  Nokogiri::HTML(@conn.page_get(url, params))
end
parse_all(obj) click to toggle source

Running all methods from {.define_parser_list}

@param obj [Nokogiri::CSS::Node] Nokigiri object

@return [Hash] all parsed attributes

# File lib/encounter/parser.rb, line 14
def parse_all(obj)
  return {} unless respond_to? :parser_list
  raise 'parser_list must be Array' unless parser_list.is_a? Array

  result = {}
  parser_list.each do |k|
    raise "Unknown method #{k}" unless respond_to? k, true
    result.merge! send(k, obj)
  end
  result
end
parse_attributes(obj) click to toggle source

Parse standard text attributes. List of attributes is given in PARSER_OBJECTS constant. Every attribute should have next fields:

  • id - CSS selector of HTML element

  • attr - attribute name

Possible additional fields are:

  • type - 'f' for Float or 'i' for Integer

  • proc - Proc object to be executed over result

@param obj [Nokogiri::CSS::Node] Nokigiri object @return [Hash] all parsed attributes

# File lib/encounter/parser.rb, line 40
def parse_attributes(obj)
  Hash[
    self.class::PARSER_OBJECTS.map do |o|
      res = obj.css(o[:id]).map(&:text).join
      res = ParserConvertors.send("to_#{o[:type]}", res) if o[:type]
      res = o[:proc].call(res) if o[:proc]
      [o[:attr], res]
    end
  ]
end
parse_cvs_pair(url, params, proc) click to toggle source

Parse document with semicolon-separated id-name records.

@param url [String] URL of list @param params [Hash] URL parameters @param proc [Proc] Procedure for value postprocessing

@return [Hash]

# File lib/encounter/parser.rb, line 101
def parse_cvs_pair(url, params, proc)
  @conn.page_get(url, params).each_line.map do |r|
    r = r.split(';')
    proc.call(r) if r.size == 2 && r.first =~ /\d+/
  end.compact
end
parse_id_name(pair) click to toggle source

Convert array of two elements to hash with id and name keys.

@param pair [Array] @return [Hash]

# File lib/encounter/parser.rb, line 90
def parse_id_name(pair)
  { id: pair.first.to_i, name: pair.last.strip }
end
parse_max_page(obj, suffix) click to toggle source

Find page number for paginated lists

@param obj [Nokogiri::CSS::Node] Nokigiri object @param suffix [String] Suffix, contained in URL to select only needed URLs

@return [Integer]

# File lib/encounter/parser.rb, line 81
def parse_max_page(obj, suffix)
  obj.css('a').select { |a| a['href'] =~ /#{suffix}.*page=\d+$/ }
     .map { |a| a['href'].match(/page=(\d+)$/).captures.first.to_i }.max
end
parse_url_id(url) click to toggle source

Return object ID from URL. Supported objects are {Encounter::Player}, {Encounter::Game} and {Encounter::Team}

# File lib/encounter/parser.rb, line 53
def parse_url_id(url)
  url.match(/[gtu]id=(\d+)/).captures.first.to_i
end
parse_url_object(obj) click to toggle source

Return Encounter object from URL. Supported objects are {Encounter::Player}and {Encounter::Team}

@param obj [String] @return [Encounter::Player] if giver URL links to player @return [Encounter::Team] if giver URL links to team

# File lib/encounter/parser.rb, line 63
def parse_url_object(obj)
  c = obj['href'].match(/([gtu])id=(\d+)/).captures
  case c.first
  when 't'
    Encounter::Team.new(@conn, tid: c.last.to_i, name: obj.text)
  when 'u'
    Encounter::Player.new(@conn, uid: c.last.to_i, name: obj.text)
  else
    raise 'Unsupported link type'
  end
end