magnum-pi 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTc1YTljNjBjYzcwMmY5YmQxZmNhNGQ1MjhmNzU1ZjRhNzQ3MGQyMg==
4
+ MTgzYWJjMmVmMWM2Yjc4ODVmMzkxOTlmYWNlZWI4OTk1YTMxODQ3NA==
5
5
  data.tar.gz: !binary |-
6
- YjNlNjUyY2VlNzUxOThjY2U0MmMyMTBhMGNhMGJjZGJkNDBhMDFiMw==
6
+ YWViMGNjMjM3MWQxNGRiYzNjZGM0ODEwMzlmYWMyMjI3NmVkZjk2Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGI1NzVkYWYxZjcxMmZkNzgyMjc1YzhmNThhZGQ2M2FlOGYzOGJjMzM2NTkw
10
- NTExMTNkYjM0MGY3Nzc5NmI2ZWQ2MmM1ZDE3YmYzZjgwMDkzYzcwMWQ3N2Ey
11
- YzY1ODVlNWQwZTFlNDBjODdlZTMyNTk4YzI0YTEwYjM2YjA5MDI=
9
+ ZGZiM2YzMzQyYjUyMDM4ZTZkNDNmMGUxY2MwNDRjZTNhMDVmYWU3YTk5MzIz
10
+ N2RhZjJkYTNkODRkMDA2YTA5ZTM5MjVlNTk1YTAxMjdlN2IyNjNiMTRhYmMx
11
+ YTk1NWQzMzA5MDU1OGFlNTJiM2JjOGFkZGQyZGQ5MDc1ZTNhMzU=
12
12
  data.tar.gz: !binary |-
13
- MGMzMjA5N2ViZjA5MGIyN2Y1ZmFjNmM5MmFmMzVkZTg5NDM4NmQzZmY1MTJk
14
- MWJlZjIzZjIzOTBhNjdjODMxNWRjMjcyM2NhZWU4NmViODQxYjQ3ZjRiZWY0
15
- OTU1ZjdjOTEzMjVkYjJkOWY2ZWM5ODFkZjI0M2QyNDczNzNmMDA=
13
+ NWJiNDhkNzI2Njg5MjAyNGNhNTY2NmEwZTQ3MThiMWFiM2U0ZTE1Y2I4MDA2
14
+ NThlMWJkYjA5MTMzNDc2YjM5OTgwNmNjZDliN2E3ZWU2MzAxOWNhNzgzMWQ5
15
+ OWJhMzI5OTY2ZjgxY2NhM2Y5YWZiMDUwZDE0ZjU1MmJjMmI3OGM=
@@ -1,5 +1,9 @@
1
1
  = MagnumPI CHANGELOG
2
2
 
3
+ == Version 0.2.1 (June 18, 2014)
4
+
5
+ * Created `Aj` for iterating through large JSON files with Saj
6
+
3
7
  == Version 0.2.0 (June 16, 2014)
4
8
 
5
9
  * Replaced XmlSimple with Ox and created `Ax` for iterating through large XML files with Sax
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -80,7 +80,7 @@ module MagnumPI
80
80
  def parse_content(response)
81
81
  case api[:format].to_s
82
82
  when "json"
83
- Oj.load response
83
+ Aj.new response
84
84
  when "xml"
85
85
  Ax.new response
86
86
  else
@@ -1,2 +1,3 @@
1
1
  require "magnum-pi/gem_ext/mechanize"
2
+ require "magnum-pi/gem_ext/oj"
2
3
  require "magnum-pi/gem_ext/ox"
@@ -0,0 +1 @@
1
+ require "magnum-pi/gem_ext/oj/aj"
@@ -0,0 +1,135 @@
1
+ require "stringio"
2
+
3
+ class Aj < Oj::Saj
4
+
5
+ def initialize(json)
6
+ @stringio = StringIO.new json
7
+ end
8
+
9
+ def each(pattern, &block)
10
+ @pattern = pattern.split("/").collect{|x| x == "" ? "*" : x}
11
+ @block = block
12
+
13
+ @stringio.rewind
14
+ Oj.saj_parse self, @stringio
15
+
16
+ ensure
17
+ @types = nil
18
+ @current_path = nil
19
+ @entries = nil
20
+ @regexp = nil
21
+ end
22
+
23
+ def hash_start(key)
24
+ start_enum key, {}, :hash
25
+ end
26
+
27
+ def array_start(key)
28
+ start_enum key, [], :array
29
+ end
30
+
31
+ def hash_end(key)
32
+ end_enum
33
+ end
34
+
35
+ def array_end(key)
36
+ end_enum
37
+ end
38
+
39
+ def add_value(value, key)
40
+ if (entry = current_entry).nil?
41
+ current_path << :nokey
42
+ entries << {key => value}
43
+ end_enum
44
+ else
45
+ entry[key] = value
46
+ end
47
+ end
48
+
49
+ def to_enum
50
+ enum = nil
51
+ each "*" do |entry, key|
52
+ enum ||= begin
53
+ if key == :nokey
54
+ entry
55
+ elsif key.is_a?(String)
56
+ {}
57
+ else
58
+ []
59
+ end
60
+ end
61
+ enum[key] = entry if key != :nokey
62
+ end
63
+ enum
64
+ end
65
+
66
+ private
67
+
68
+ def types
69
+ @types ||= []
70
+ end
71
+
72
+ def current_path
73
+ @current_path ||= []
74
+ end
75
+
76
+ def entries
77
+ @entries ||= []
78
+ end
79
+
80
+ def current_entry
81
+ entries[-1]
82
+ end
83
+
84
+ def current_key
85
+ current_path[-1]
86
+ end
87
+
88
+ def current_type
89
+ types[-1]
90
+ end
91
+
92
+ def entry?
93
+ @regexp ||= begin
94
+ pattern = @pattern.join(%q{\/}).gsub("*", %q{[^\/]+})
95
+ Regexp.new "^#{pattern}$"
96
+ end
97
+ !!current_path.join("/").match(@regexp)
98
+ end
99
+
100
+ def start_enum(key, entry, type)
101
+ add_to_current_path key
102
+ if current_entry || entry?
103
+ add_entry entry
104
+ end
105
+ types << type
106
+ end
107
+
108
+ def end_enum
109
+ entry = entries.pop
110
+ if entry?
111
+ @block.call entry, current_key
112
+ end
113
+ finalize_entry
114
+ end
115
+
116
+ def add_to_current_path(key)
117
+ key ||= (current_entry || []).size if current_type == :array
118
+ current_path << key if key
119
+ end
120
+
121
+ def add_entry(entry)
122
+ if (parent = current_entry).nil?
123
+ parent = (current_type == :hash ? {} : [])
124
+ entries << parent
125
+ end
126
+ parent[current_key] = entry
127
+ entries << entry
128
+ end
129
+
130
+ def finalize_entry
131
+ current_path.pop
132
+ types.pop
133
+ end
134
+
135
+ end
@@ -10,13 +10,13 @@ class Ax < Ox::Sax
10
10
  @pattern = pattern.split("/").collect{|x| x == "" ? "*" : x}
11
11
  @block = block
12
12
 
13
+ @stringio.rewind
13
14
  Ox.sax_parse self, @stringio
14
15
 
15
16
  ensure
16
17
  @current_path = nil
17
18
  @elements = nil
18
- @entry_regexp = nil
19
- @last_call = nil
19
+ @regexp = nil
20
20
  end
21
21
 
22
22
  def start_element(name)
@@ -28,7 +28,6 @@ class Ax < Ox::Sax
28
28
  else
29
29
  return
30
30
  end
31
- @last_call = :start_element
32
31
  end
33
32
 
34
33
  def attr(name, str)
@@ -47,8 +46,6 @@ class Ax < Ox::Sax
47
46
  element = finalize_element
48
47
  if entry?
49
48
  @block.call element, current_path[-1]
50
- elsif element
51
- @last_call = :end_element
52
49
  end
53
50
  current_path.pop
54
51
  end
@@ -61,6 +58,8 @@ class Ax < Ox::Sax
61
58
  hash
62
59
  end
63
60
 
61
+ alias :to_enum :to_hash
62
+
64
63
  private
65
64
 
66
65
  def current_path
@@ -72,11 +71,11 @@ private
72
71
  end
73
72
 
74
73
  def entry?
75
- @entry_regexp ||= begin
74
+ @regexp ||= begin
76
75
  pattern = @pattern.join(%q{\/}).gsub("*", %q{[^\/]+})
77
76
  Regexp.new "^#{pattern}$"
78
77
  end
79
- !!current_path.join("/").match(@entry_regexp)
78
+ !!current_path.join("/").match(@regexp)
80
79
  end
81
80
 
82
81
  def current_element
@@ -1,7 +1,7 @@
1
1
  module MagnumPI
2
2
  MAJOR = 0
3
3
  MINOR = 2
4
- TINY = 0
4
+ TINY = 1
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join(".")
7
7
  end
@@ -25,4 +25,5 @@ Gem::Specification.new do |gem|
25
25
  gem.add_development_dependency "simplecov"
26
26
  gem.add_development_dependency "minitest"
27
27
  gem.add_development_dependency "mocha"
28
+ gem.add_development_dependency "xml-simple"
28
29
  end
@@ -10,5 +10,6 @@ end
10
10
 
11
11
  require "bundler"
12
12
  Bundler.require :default, :development, :test
13
+ require "xmlsimple"
13
14
 
14
15
  require_relative "test_helper/minitest"
@@ -1,10 +1,10 @@
1
- class MiniTest::Test
2
- def setup
3
- $stringio ||= StringIO.new
4
- $stdout = $stringio
5
- end
6
- def teardown
7
- $stdout = STDOUT
8
- $stringio.truncate $stringio.rewind
9
- end
10
- end
1
+ # class MiniTest::Test
2
+ # def setup
3
+ # $stringio ||= StringIO.new
4
+ # $stdout = $stringio
5
+ # end
6
+ # def teardown
7
+ # $stdout = STDOUT
8
+ # $stringio.truncate $stringio.rewind
9
+ # end
10
+ # end
@@ -31,7 +31,7 @@ module Unit
31
31
  response = mock
32
32
  response.expects(:content).returns('{"name": "Paul Engel"}')
33
33
  @consumer.expects(:request).with(:get, "http://foo.bar", {:foo => "bar"}).returns(response)
34
- assert_equal({"name" => "Paul Engel"}, @consumer.get(:foo => "bar"))
34
+ assert_equal({"name" => "Paul Engel"}, @consumer.get(:foo => "bar").to_enum)
35
35
  end
36
36
  end
37
37
  describe "#post" do
@@ -39,7 +39,7 @@ module Unit
39
39
  response = mock
40
40
  response.expects(:content).returns('{"name": "Paul Engel"}')
41
41
  @consumer.expects(:request).with(:post, "http://foo.bar", {:foo => "bar"}).returns(response)
42
- assert_equal({"name" => "Paul Engel"}, @consumer.post(:foo => "bar"))
42
+ assert_equal({"name" => "Paul Engel"}, @consumer.post(:foo => "bar").to_enum)
43
43
  end
44
44
  end
45
45
  describe "#download" do
@@ -149,7 +149,7 @@ module Unit
149
149
  <<-JSON
150
150
  {"foo": "bar"}
151
151
  JSON
152
- )
152
+ ).to_enum
153
153
  )
154
154
  @consumer.expects(:api).returns :format => "xml"
155
155
  assert_equal(
@@ -0,0 +1,82 @@
1
+ require_relative "../../test_helper"
2
+
3
+ module Unit
4
+ class TestAj < MiniTest::Test
5
+
6
+ describe Aj do
7
+ before do
8
+ @json = <<-JSON
9
+ {
10
+ "data": {
11
+ "articles": [
12
+ {
13
+ "title": "MagnumPI is awesome!!!",
14
+ "category": "ruby"
15
+ }, {
16
+ "title": "Netherlands beats Spain with 1-5 :)",
17
+ "category": "sport"
18
+ }
19
+ ]
20
+ }
21
+ }
22
+ JSON
23
+ end
24
+ it "parses JSON like Oj" do
25
+ assert_equal Oj.load(@json), Aj.new(@json).to_enum
26
+ assert_equal({
27
+ "data" => {
28
+ "articles" => [
29
+ {
30
+ "title" => "MagnumPI is awesome!!!",
31
+ "category" => "ruby"
32
+ }, {
33
+ "title" => "Netherlands beats Spain with 1-5 :)",
34
+ "category" => "sport"
35
+ }
36
+ ]
37
+ }
38
+ }, Aj.new(@json).to_enum)
39
+ assert_equal({
40
+ "name" => "Paul Engel"
41
+ }, Aj.new('{"name": "Paul Engel"}').to_enum)
42
+ assert_equal([
43
+ {"name" => "Paul Engel"},
44
+ {"name" => "Ken Adams"}
45
+ ], Aj.new('[{"name": "Paul Engel"}, {"name": "Ken Adams"}]').to_enum)
46
+ end
47
+ it "can return an array within a JSON document" do
48
+ result = []
49
+ Aj.new(@json).each("data/articles") do |entry|
50
+ result << entry
51
+ end
52
+ assert_equal [
53
+ [
54
+ {
55
+ "title" => "MagnumPI is awesome!!!",
56
+ "category" => "ruby"
57
+ }, {
58
+ "title" => "Netherlands beats Spain with 1-5 :)",
59
+ "category" => "sport"
60
+ }
61
+ ]
62
+ ], result
63
+ end
64
+ it "can iterate through a JSON document" do
65
+ articles = []
66
+ Aj.new(@json).each("data/articles/*") do |article|
67
+ articles << article
68
+ end
69
+ assert_equal [
70
+ {
71
+ "title" => "MagnumPI is awesome!!!",
72
+ "category" => "ruby"
73
+ }, {
74
+ "title" => "Netherlands beats Spain with 1-5 :)",
75
+ "category" => "sport"
76
+ }
77
+ ], articles
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -22,6 +22,8 @@ module Unit
22
22
  XML
23
23
  end
24
24
  it "parses XML like XmlSimple" do
25
+ assert_equal XmlSimple.xml_in(@xml), Ax.new(@xml).to_enum
26
+ assert_equal XmlSimple.xml_in(@xml), Ax.new(@xml).to_hash
25
27
  assert_equal({
26
28
  "data" => [{
27
29
  "article" => [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magnum-pi
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
  - Paul Engel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-16 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ! '>='
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: xml-simple
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: Create an easy interface to talk with APIs
140
154
  email:
141
155
  - pm_engel@icloud.com
@@ -166,6 +180,8 @@ files:
166
180
  - lib/magnum-pi/gem_ext/mechanize/http.rb
167
181
  - lib/magnum-pi/gem_ext/mechanize/http/agent.rb
168
182
  - lib/magnum-pi/gem_ext/mechanize/util.rb
183
+ - lib/magnum-pi/gem_ext/oj.rb
184
+ - lib/magnum-pi/gem_ext/oj/aj.rb
169
185
  - lib/magnum-pi/gem_ext/ox.rb
170
186
  - lib/magnum-pi/gem_ext/ox/ax.rb
171
187
  - lib/magnum-pi/version.rb
@@ -178,8 +194,9 @@ files:
178
194
  - test/unit/api/test_instance.rb
179
195
  - test/unit/api/test_resources.rb
180
196
  - test/unit/api/test_scheme.rb
181
- - test/unit/core_ext/test_ax.rb
182
197
  - test/unit/core_ext/test_deep_clone.rb
198
+ - test/unit/gem_ext/test_aj.rb
199
+ - test/unit/gem_ext/test_ax.rb
183
200
  - test/unit/test_api.rb
184
201
  - test/unit/test_dsl.rb
185
202
  - test/unit/test_magnum-pi.rb
@@ -214,8 +231,9 @@ test_files:
214
231
  - test/unit/api/test_instance.rb
215
232
  - test/unit/api/test_resources.rb
216
233
  - test/unit/api/test_scheme.rb
217
- - test/unit/core_ext/test_ax.rb
218
234
  - test/unit/core_ext/test_deep_clone.rb
235
+ - test/unit/gem_ext/test_aj.rb
236
+ - test/unit/gem_ext/test_ax.rb
219
237
  - test/unit/test_api.rb
220
238
  - test/unit/test_dsl.rb
221
239
  - test/unit/test_magnum-pi.rb