Sha256: e6201bf88bd5ef65ef805c9985c8754e23b7fec2af244bb2bc7cd52009a25cf6
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
module BanksApi module Shinsei class JsParser def self.data_for(html_body) new(html_body).data end def initialize(html_body) @html_body = html_body end 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 attr_reader :html_body def setup_code @_setup_code ||= html_body.lines.first. match(snippet_regex). to_a.fetch(0, "") end def snippet_regex /(?<=<script language="JavaScript">).*(?=<\/script>)/ end def var_assign_regex /(\w+)(?:\[(\d+)\])?=(.*?)(?=;)/ end def parse_value(value) if value == "new Array()" [] else match_numeric(match_string(value)) end end def match_string(value) value.match(/^('|"|)(.*)\1$/)[2] end 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
banks_api-shinsei-0.1.2 | lib/banks_api/shinsei/js_parser.rb |