Skip to content

Commit

Permalink
Merge pull request #27 from tac0x2a/fix/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0x2a committed Mar 22, 2021
2 parents 1163bab + f1f8adf commit d5cd2ef
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
22 changes: 12 additions & 10 deletions lib/yasuri/yasuri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

module Yasuri

DefaultRetryCount = 5

def self.json2tree(json_string)
json = JSON.parse(json_string, {symbolize_names: true})
Yasuri.hash2node(json)
raise RuntimeError if json_string.nil? or json_string.empty?

node_hash = JSON.parse(json_string, {symbolize_names: true})
Yasuri.hash2node(node_hash)
end

def self.tree2json(node)
Expand All @@ -32,9 +36,9 @@ def self.yaml2tree(yaml_string)
raise RuntimeError if yaml.keys.size < 1

root_key, root = yaml.keys.first, yaml.values.first
hash = Yasuri.yaml2tree_sub(root_key, root)
node_hash = Yasuri.yaml2tree_sub(root_key, root)

Yasuri.hash2node(hash)
Yasuri.hash2node(node_hash)
end

private
Expand Down Expand Up @@ -66,17 +70,15 @@ def self.method_missing(method_name, pattern=nil, **opt, &block)
struct: Yasuri::StructNode,
links: Yasuri::LinksNode,
pages: Yasuri::PaginateNode,
map: Yasuri::MapNode
map: Yasuri::MapNode
}
Node2Text = Text2Node.invert

ReservedKeys = %i|node name path children|
def self.hash2node(node_h)
node = node_h[:node]
def self.hash2node(node_hash)
node = node_hash[:node]

fail "Not found 'node' value in map" if node.nil?
klass = Text2Node[node.to_sym]
klass::hash2node(node_h)
klass::hash2node(node_hash)
end

def self.node2hash(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/yasuri/yasuri_links_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Yasuri
class LinksNode
include Node
def inject(agent, page, opt = {}, element = page)
retry_count = opt[:retry_count] || 5
retry_count = opt[:retry_count] || Yasuri::DefaultRetryCount

links = element.search(@xpath) || [] # links expected
links.map do |link|
Expand All @@ -24,7 +24,7 @@ def inject(agent, page, opt = {}, element = page)
end

def node_type_str
"links"
"links".freeze
end
end # class
end # module
24 changes: 11 additions & 13 deletions lib/yasuri/yasuri_map_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,30 @@ def opts
end

def to_h
h = {}
h["node"] = "map"
h["name"] = self.name
h["children"] = self.children.map{|c| c.to_h} if not children.empty?
node_hash = {}
node_hash["node"] = "map".freeze
node_hash["name"] = self.name
node_hash["children"] = self.children.map{|c| c.to_h} if not children.empty?

self.opts.each do |key,value|
h[key] = value if not value.nil?
node_hash[key] = value if not value.nil?
end

h
node_hash
end

def self.hash2node(node_h)
reservedKeys = %i|node name children|
def self.hash2node(node_hash)
reserved_keys = %i|node name children|.freeze

node, name, children = reservedKeys.map do |key|
node_h[key]
end
node, name, children = reserved_keys.map{|key| node_hash[key]}

fail "Not found 'name' value in map" if name.nil?
fail "Not found 'children' value in map" if children.nil?
children ||= []

childnodes = children.map{|c| Yasuri.hash2node(c) }
reservedKeys.each{|key| node_h.delete(key)}
opt = node_h
reserved_keys.each{|key| node_hash.delete(key)}
opt = node_hash

self.new(name, childnodes, **opt)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/yasuri/yasuri_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def to_h

module ClassMethods
def hash2node(node_h)
reservedKeys = %i|node name path children|
reserved_keys = %i|node name path children|

node, name, path, children = ReservedKeys.map do |key|
node, name, path, children = reserved_keys.map do |key|
node_h[key]
end

Expand All @@ -46,7 +46,7 @@ def hash2node(node_h)
children ||= []

childnodes = children.map{|c| Yasuri.hash2node(c) }
reservedKeys.each{|key| node_h.delete(key)}
reserved_keys.each{|key| node_h.delete(key)}
opt = node_h

self.new(path, name, childnodes, **opt)
Expand Down
7 changes: 4 additions & 3 deletions lib/yasuri/yasuri_paginate_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(xpath, name, children = [], limit: nil, flatten: false)
end

def inject(agent, page, opt = {}, element = page)
retry_count = opt[:retry_count] || 5
retry_count = opt[:retry_count] || Yasuri::DefaultRetryCount

raise NotImplementedError.new("PagenateNode inside StructNode, Not Supported") if page != element

Expand All @@ -27,7 +27,7 @@ def inject(agent, page, opt = {}, element = page)
end
child_results << Hash[child_results_kv]

link = page.search(@xpath).first
link = page.search(@xpath).first # Todo raise: link is not found
break if link == nil

link_button = Mechanize::Page::Link.new(link, agent, page)
Expand All @@ -41,12 +41,13 @@ def inject(agent, page, opt = {}, element = page)

child_results
end

def opts
{limit:@limit, flatten:@flatten}
end

def node_type_str
"pages"
"pages".freeze
end
end
end
4 changes: 2 additions & 2 deletions lib/yasuri/yasuri_text_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def initialize(xpath, name, children = [], **opt)
@truncate = Regexp.new(@truncate.to_s) if not @truncate.nil?

@proc = proc.nil? ? nil : proc.to_sym

end

def inject(agent, page, opt = {}, element = page)
Expand All @@ -31,11 +30,12 @@ def inject(agent, page, opt = {}, element = page)
end

text = text.__send__(@proc) if @proc && text.respond_to?(@proc)

text
end

def node_type_str
"text"
"text".freeze
end

def opts
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
require 'coveralls'
Coveralls.wear!

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
Expand Down

0 comments on commit d5cd2ef

Please sign in to comment.