Class: PPCommand::Main
- Inherits:
-
Object
- Object
- PPCommand::Main
- Defined in:
- lib/ppcommand/main.rb
Instance Method Summary collapse
- #execute(opts, file) ⇒ Object
- #get_att_list(node, itab = 0) ⇒ Object
- #pp_csv(source) ⇒ Object
- #pp_csvtable(source) ⇒ Object
- #pp_html(source) ⇒ Object
- #pp_json(source) ⇒ Object
- #pp_xml(source) ⇒ Object
- #pp_xmlsimple(source) ⇒ Object
- #pp_yaml(source) ⇒ Object
- #pretty_xml(doc) ⇒ Object
-
#x_pretty_print(parent_node, itab) ⇒ Object
original from snippets.dzone.com/posts/show/4953.
Instance Method Details
#execute(opts, file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ppcommand/main.rb', line 8 def execute(opts, file) source = '' if file.nil? source = STDIN.read elsif file=~ %r{^https?://} open(file) do |io| source = io.read if opts[:type] == "auto" t = io.content_type if t =~ /json/ opts[:type] = "json" elsif t =~ /yaml/ opts[:type] = "yaml" elsif t =~ /csv/ opts[:type] = "csv" elsif t =~ /html/ opts[:type] = "html" elsif t =~ /xml/ opts[:type] = "xml" end end end else source = File.read(file) end if opts[:type] == "auto" if file =~ /\.xml$/i opts[:type] = "xml" elsif file =~ /\.json$/i opts[:type] = "json" elsif file =~ /\.(?:csv|txt)$/i opts[:type] = "csv" elsif file =~ /\.html$/i opts[:type] = "html" else opts[:type] = "yaml" end end case opts[:type] when "xml" pp_xml(source) when "xmlsimple" pp_xmlsimple(source) when "json" pp_json(source) when "csv" pp_csv(source) when "csvtable" pp_csvtable(source) when "html" pp_html(source) when "text" puts source else "yaml" pp_yaml(source) end end |
#get_att_list(node, itab = 0) ⇒ Object
161 162 163 164 165 |
# File 'lib/ppcommand/main.rb', line 161 def get_att_list(node, itab = 0) att_list = '' node.attributes.each { |attribute, val| att_list += "\n" + (" " * itab) + " #{attribute} = \"#{val}\"" } att_list end |
#pp_csv(source) ⇒ Object
92 93 94 95 |
# File 'lib/ppcommand/main.rb', line 92 def pp_csv(source) require 'csv' ap CSV.parse(source) end |
#pp_csvtable(source) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ppcommand/main.rb', line 97 def pp_csvtable(source) require 'csv' data = CSV.parse(source) keys = data.shift result = [] data.each do |values| entry = [] i = nil keys.each_with_index do |k, i| entry << [i, k, values[i]] end if keys.length < values.length values[i + 1 .. -1].each_with_index do |v, j| entry << [i + j + 1, nil, v] end end result << entry end ap result # data.map {|values| Hash[* [keys,values].transpose.flatten] } end |
#pp_html(source) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ppcommand/main.rb', line 119 def pp_html(source) begin require 'nokogiri' rescue Exception => e STDERR.puts "'nokogiri' is required to parse HTML." STDERR.puts "$ gem install nokorigi" return end doc = Nokogiri.HTML(source) # doc.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::AS_XML) ap doc end |
#pp_json(source) ⇒ Object
80 81 82 83 84 |
# File 'lib/ppcommand/main.rb', line 80 def pp_json(source) require 'rubygems' require 'json' ap JSON.parse(source) end |
#pp_xml(source) ⇒ Object
68 69 70 71 72 |
# File 'lib/ppcommand/main.rb', line 68 def pp_xml(source) require 'rubygems' require 'rexml/document' puts pretty_xml(REXML::Document.new(source)) end |
#pp_xmlsimple(source) ⇒ Object
74 75 76 77 78 |
# File 'lib/ppcommand/main.rb', line 74 def pp_xmlsimple(source) require 'rubygems' require 'xmlsimple' ap XmlSimple.xml_in(source) end |
#pp_yaml(source) ⇒ Object
86 87 88 89 90 |
# File 'lib/ppcommand/main.rb', line 86 def pp_yaml(source) YAML.each_document(StringIO.new(source)) do |obj| ap obj end end |
#pretty_xml(doc) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ppcommand/main.rb', line 167 def pretty_xml(doc) buffer = '' xml_declaration = doc.to_s.match('<\?.*\?>').to_s buffer += "#{xml_declaration}\n" if not xml_declaration.nil? xml_doctype = doc.to_s.match('<\!.*\">').to_s buffer += "#{xml_doctype}\n" if not xml_doctype.nil? buffer += "<#{doc.root.name}#{get_att_list(doc.root)}" if doc.root.to_a.length > 0 buffer +=">\n#{x_pretty_print(doc.root,2)}</#{doc.root.name}>" else buffer += " />\n" end end |
#x_pretty_print(parent_node, itab) ⇒ Object
original from snippets.dzone.com/posts/show/4953
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ppcommand/main.rb', line 133 def x_pretty_print(parent_node, itab) buffer = '' parent_node.elements.each do |node| buffer += ' ' * itab + "<#{node.name}#{get_att_list(node, itab)}" if node.to_a.length > 0 buffer += ">" if node.text.nil? buffer += "\n" buffer += x_pretty_print(node,itab+2) buffer += ' ' * itab + "</#{node.name}>\n" else node_text = node.text.strip if node_text != '' buffer += node_text buffer += "</#{node.name}>\n" else buffer += "\n" + x_pretty_print(node,itab+2) buffer += ' ' * itab + "</#{node.name}>\n" end end else buffer += " />\n" end end buffer end |