class BanksApi::Shinsei::JsParser

Attributes

html_body[R]

Public Class Methods

data_for(html_body) click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 4
def self.data_for(html_body)
  new(html_body).data
end
new(html_body) click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 8
def initialize(html_body)
  @html_body = html_body
end

Public Instance Methods

data() click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 12
def data
  @_data ||= setup_code.
    to_enum(:scan, var_assign_regex).
    inject({}) do |variables|

      name, index, value = Regexp.last_match.captures

      value = parse_value(value)

      if index
        variables[name] ||= []
        variables[name][index.to_i] = value
      else
        variables[name] = value
      end

      variables
    end
end

Private Instance Methods

match_numeric(value) click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 62
def match_numeric(value)
  match = value.match /^\d{1,3}(,\d{3})*(\.\d*)?$/
  return value unless match
  if match[2]
    value.tr(",", "").to_f
  else
    value.tr(",", "").to_i
  end
end
match_string(value) click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 58
def match_string(value)
  value.match(/^('|"|)(.*)\1$/)[2]
end
parse_value(value) click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 50
def parse_value(value)
  if value == "new Array()"
    []
  else
    match_numeric(match_string(value))
  end
end
setup_code() click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 36
def setup_code
  @_setup_code ||= html_body.lines.first.
    match(snippet_regex).
    to_a.fetch(0, "")
end
snippet_regex() click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 42
def snippet_regex
  /(?<=<script language="JavaScript">).*(?=<\/script>)/
end
var_assign_regex() click to toggle source
# File lib/banks_api/shinsei/js_parser.rb, line 46
def var_assign_regex
  /(\w+)(?:\[(\d+)\])?=(.*?)(?=;)/
end