ppcommand 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b056394e4ded505fc8f3504c999b3b4f58151c03
4
- data.tar.gz: 3d4c6f399311cd0875c843b8bc15bb284eff3921
3
+ metadata.gz: f7bc8dfd32503c7ff22d467c184aeac1366a8b72
4
+ data.tar.gz: cdb3f08a20e3fa47cdb493eea9f5458d33f1a9bb
5
5
  SHA512:
6
- metadata.gz: 69b02307466c3f2f695c9b0cbe4d5775604d31c5bd6f00ccd325a7f69b6bcdd76e1005671d08337cc5b61899f758c42768c210b33917c7bc69c3048734db1f10
7
- data.tar.gz: 143396ef13ff69181955a997acbcd147fcdd06a470163e30e91d2be4d5f6117fac9701cce1632727a73a97c738b001fb897d283721119cae12d6b60a21886537
6
+ metadata.gz: 9e9d4f088daafe444b25525146e1a36c8df8416dbcf30b32a4c184f4f61783f6a46eb1abbc1f19ecc757afd7cbce11249437c7754a3d8fc847c2a637d11f1560
7
+ data.tar.gz: 9e49d51412e675c5cb225b9f4ea0b476df5d7a7eb4612558cdeecd30824e05d5d042a12d1da7d161f0ea8b1d94ba30900a568729d28a7ce40a03a151bc12439a
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+
3
+ require 'optparse'
4
+ require 'ppcommand/main'
5
+ require 'open-uri'
6
+
7
+ module PPCommand
8
+
9
+ class CLI
10
+ def self.execute(argv)
11
+ opts = {:type => "auto"}
12
+ opp = OptionParser.new
13
+
14
+ opp.banner = "pp [options] [file|URI]"
15
+ opp.on_tail("-h", "--help", "show this help.") do
16
+ puts opp
17
+ exit
18
+ end
19
+ opp.on_tail("-v", "--version", "show version.") do
20
+ puts "ppcommand #{ PPCommand::VERSION }"
21
+ exit
22
+ end
23
+
24
+ opp.on("-c", "--csv", "parse CSV and pp."){|x| opts[:type] = "csv"}
25
+ opp.on("-C", "--csvtable", "parse CSV, add labels and pp."){|x| opts[:type] = "csvtable"}
26
+ opp.on("-H", "--html", "parse HTML and pp."){|x| opts[:type] = "html"}
27
+ opp.on("-j", "--json", "parse JSON and pp."){|x| opts[:type] = "json"}
28
+ opp.on("-x", "--xml", "parse XML using REXML and pp."){|x| opts[:type] = "xml"}
29
+ opp.on("-X", "--xmlsimple", "parse XML using XMLSimple and pp."){|x| opts[:type] = "xmlsimple"}
30
+ opp.on("-y", "--yaml", "parse YAML and pp."){|x| opts[:type] = "yaml"}
31
+
32
+ opp.on("-t", "--text", "do not parse. print plain text."){|x| opts[:type] = "text"}
33
+
34
+ opp.parse!(argv)
35
+
36
+ file = argv.shift
37
+
38
+ PPCommand::Main.new.execute(opts, file)
39
+ end
40
+ end
41
+ end
@@ -1,11 +1,69 @@
1
1
  # coding: utf-8
2
2
 
3
- require 'pp'
3
+ require 'awesome_print'
4
4
  require 'yaml'
5
- require 'optparse'
6
5
 
7
6
  module PPCommand
8
7
  class Main
8
+ def execute(opts, file)
9
+ source = ''
10
+ if file.nil?
11
+ source = STDIN.read
12
+ elsif file=~ %r{^https?://}
13
+ open(file) do |io|
14
+ source = io.read
15
+ if opts[:type] == "auto"
16
+ t = io.content_type
17
+ if t =~ /json/
18
+ opts[:type] = "json"
19
+ elsif t =~ /yaml/
20
+ opts[:type] = "yaml"
21
+ elsif t =~ /csv/
22
+ opts[:type] = "csv"
23
+ elsif t =~ /html/
24
+ opts[:type] = "html"
25
+ elsif t =~ /xml/
26
+ opts[:type] = "xml"
27
+ end
28
+ end
29
+ end
30
+ else
31
+ source = File.read(file)
32
+ end
33
+
34
+ if opts[:type] == "auto"
35
+ if file =~ /\.xml$/i
36
+ opts[:type] = "xml"
37
+ elsif file =~ /\.json$/i
38
+ opts[:type] = "json"
39
+ elsif file =~ /\.(?:csv|txt)$/i
40
+ opts[:type] = "csv"
41
+ elsif file =~ /\.html$/i
42
+ opts[:type] = "html"
43
+ else
44
+ opts[:type] = "yaml"
45
+ end
46
+ end
47
+
48
+ case opts[:type]
49
+ when "xml"
50
+ pp_xml(source)
51
+ when "xmlsimple"
52
+ pp_xmlsimple(source)
53
+ when "json"
54
+ pp_json(source)
55
+ when "csv"
56
+ pp_csv(source)
57
+ when "csvtable"
58
+ pp_csvtable(source)
59
+ when "html"
60
+ pp_html(source)
61
+ when "text"
62
+ puts source
63
+ else "yaml"
64
+ pp_yaml(source)
65
+ end
66
+ end
9
67
 
10
68
  def pp_xml(source)
11
69
  require 'rubygems'
@@ -16,24 +74,24 @@ module PPCommand
16
74
  def pp_xmlsimple(source)
17
75
  require 'rubygems'
18
76
  require 'xmlsimple'
19
- pp XmlSimple.xml_in(source)
77
+ ap XmlSimple.xml_in(source)
20
78
  end
21
79
 
22
80
  def pp_json(source)
23
81
  require 'rubygems'
24
82
  require 'json'
25
- pp JSON.parse(source)
83
+ ap JSON.parse(source)
26
84
  end
27
85
 
28
86
  def pp_yaml(source)
29
87
  YAML.each_document(StringIO.new(source)) do |obj|
30
- pp obj
88
+ ap obj
31
89
  end
32
90
  end
33
91
 
34
92
  def pp_csv(source)
35
93
  require 'csv'
36
- pp CSV.parse(source)
94
+ ap CSV.parse(source)
37
95
  end
38
96
 
39
97
  def pp_csvtable(source)
@@ -54,7 +112,7 @@ module PPCommand
54
112
  end
55
113
  result << entry
56
114
  end
57
- pp result
115
+ ap result
58
116
  # data.map {|values| Hash[* [keys,values].transpose.flatten] }
59
117
  end
60
118
 
@@ -68,95 +126,7 @@ module PPCommand
68
126
  end
69
127
  doc = Nokogiri.HTML(source)
70
128
  # doc.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::AS_XML)
71
- pp doc
72
- end
73
-
74
- def execute(argv)
75
- opts = {:type => "auto"}
76
- opp = OptionParser.new
77
-
78
- opp.banner = "pp [options] [file|URI]"
79
- opp.on_tail("-h", "--help", "show this help.") do
80
- puts opp
81
- exit
82
- end
83
- opp.on_tail("-v", "--version", "show version.") do
84
- puts "ppcommand #{ PPCommand::VERSION }"
85
- exit
86
- end
87
-
88
- opp.on("-c", "--csv", "parse CSV and pp."){|x| opts[:type] = "csv"}
89
- opp.on("-C", "--csvtable", "parse CSV, add labels and pp."){|x| opts[:type] = "csvtable"}
90
- opp.on("-H", "--html", "parse HTML and pp."){|x| opts[:type] = "html"}
91
- opp.on("-j", "--json", "parse JSON and pp."){|x| opts[:type] = "json"}
92
- opp.on("-x", "--xml", "parse XML using REXML and pp."){|x| opts[:type] = "xml"}
93
- opp.on("-X", "--xmlsimple", "parse XML using XMLSimple and pp."){|x| opts[:type] = "xmlsimple"}
94
- opp.on("-y", "--yaml", "parse YAML and pp."){|x| opts[:type] = "yaml"}
95
-
96
- opp.on("-t", "--text", "do not parse. print plain text."){|x| opts[:type] = "text"}
97
-
98
- opp.parse!(argv)
99
-
100
- file = argv.shift
101
-
102
- source = ""
103
- if file.nil?
104
- source = STDIN.read
105
- elsif file=~ %r{^https?://}
106
- require 'open-uri'
107
- open(file) do |io|
108
- source = io.read
109
- if opts[:type] == "auto"
110
- t = io.content_type
111
- if t =~ /json/
112
- opts[:type] = "json"
113
- elsif t =~ /yaml/
114
- opts[:type] = "yaml"
115
- elsif t =~ /csv/
116
- opts[:type] = "csv"
117
- elsif t =~ /html/
118
- opts[:type] = "html"
119
- elsif t =~ /xml/
120
- opts[:type] = "xml"
121
- end
122
- end
123
- end
124
- else
125
- source = File.read(file)
126
- end
127
-
128
- if opts[:type] == "auto"
129
- if file =~ /\.xml$/i
130
- opts[:type] = "xml"
131
- elsif file =~ /\.json$/i
132
- opts[:type] = "json"
133
- elsif file =~ /\.(?:csv|txt)$/i
134
- opts[:type] = "csv"
135
- elsif file =~ /\.html$/i
136
- opts[:type] = "html"
137
- else
138
- opts[:type] = "yaml"
139
- end
140
- end
141
-
142
- case opts[:type]
143
- when "xml"
144
- pp_xml(source)
145
- when "xmlsimple"
146
- pp_xmlsimple(source)
147
- when "json"
148
- pp_json(source)
149
- when "csv"
150
- pp_csv(source)
151
- when "csvtable"
152
- pp_csvtable(source)
153
- when "html"
154
- pp_html(source)
155
- when "text"
156
- puts source
157
- else "yaml"
158
- pp_yaml(source)
159
- end
129
+ ap doc
160
130
  end
161
131
 
162
132
  # original from http://snippets.dzone.com/posts/show/4953
@@ -1,3 +1,3 @@
1
1
  module PPCommand
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/lib/ppcommand.rb CHANGED
@@ -1,8 +1,8 @@
1
- require "ppcommand/version"
2
- require "ppcommand/main"
1
+ require 'ppcommand/version'
2
+ require 'ppcommand/cli'
3
3
 
4
4
  module PPCommand
5
5
  def self.execute
6
- PPCommand::Main.new.execute(ARGV)
6
+ PPCommand::CLI.execute(ARGV)
7
7
  end
8
8
  end
data/ppcommand.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'xml-simple'
22
+ spec.add_dependency 'awesome_print'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.6'
24
25
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppcommand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - KOSEKI Kengo
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -83,6 +97,7 @@ files:
83
97
  - VERSION
84
98
  - bin/pp
85
99
  - lib/ppcommand.rb
100
+ - lib/ppcommand/cli.rb
86
101
  - lib/ppcommand/main.rb
87
102
  - lib/ppcommand/version.rb
88
103
  - ppcommand.gemspec