pathy 0.0.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.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format=nested
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pathy.gemspec
4
+ gemspec
5
+
6
+ gem 'json'
7
+
8
+ group :development do
9
+ gem 'rspec'
10
+ gem 'ZenTest'
11
+ end
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pathy (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ZenTest (4.5.0)
10
+ diff-lcs (1.1.2)
11
+ json (1.5.1)
12
+ rspec (2.5.0)
13
+ rspec-core (~> 2.5.0)
14
+ rspec-expectations (~> 2.5.0)
15
+ rspec-mocks (~> 2.5.0)
16
+ rspec-core (2.5.1)
17
+ rspec-expectations (2.5.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ ZenTest
26
+ json
27
+ pathy!
28
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Christopher Burnett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,136 @@
1
+ ### Pathy ###
2
+
3
+ JSON validation helper.
4
+
5
+ ### Installation ###
6
+
7
+ gem install pathy
8
+
9
+ ### Usage ###
10
+
11
+ require 'spec_helper'
12
+
13
+ describe Pathy do
14
+ before :all do
15
+
16
+ # add the helper methods to any object.
17
+ # this can be called on any class.
18
+ Object.pathy!
19
+
20
+ @json = %[
21
+ {
22
+ "string" : "barr",
23
+ "number" : 1,
24
+ "array" : [1,2,3],
25
+ "hash" : {"one":{"two" : 2}}
26
+ }
27
+ ]
28
+
29
+ @json_array = %[
30
+ [{
31
+ "string" : "barr",
32
+ "number" : 1,
33
+ "array" : [1,2,3],
34
+ "hash" : {"one":{"two" : 2}}
35
+ }]
36
+ ]
37
+
38
+ end
39
+
40
+
41
+ describe "for hashes" do
42
+ before :all do
43
+ @obj = JSON.parse(@json)
44
+ @array = JSON.parse(@json_array)
45
+ end
46
+
47
+ it "should parse 'number' as 1" do
48
+ @obj.at_json_path("number").should == 1
49
+ end
50
+
51
+ it "should parse 'array' as [1,2,3]" do
52
+ @obj.at_json_path('array').should == [1,2,3]
53
+ end
54
+
55
+ it "should parse 'hash.one' as {'two' => 2}" do
56
+ @json.at_json_path('hash.one').should == {'two' => 2}
57
+ end
58
+
59
+ it "should parse 'hash.one' as {'two': 2}" do
60
+ @obj.at_json_path('hash.one.two').should == 2
61
+ end
62
+
63
+ describe "invalid paths" do
64
+ it "should raise InvalidPathError" do
65
+ lambda {
66
+ @obj.at_json_path('foo.bar')
67
+ }.should raise_error Pathy::InvalidPathError
68
+ end
69
+ end
70
+
71
+ describe "#has_json_path?" do
72
+ it "should be true for valid paths" do
73
+ @obj.has_json_path?('hash.one.two').should be_true
74
+ end
75
+ it "should be false for invalid paths" do
76
+ @obj.has_json_path?('hash.one.foo').should be_false
77
+ end
78
+
79
+ it "should work as rspec matcher" do
80
+ @obj.should have_json_path "hash.one"
81
+ end
82
+
83
+ end
84
+
85
+
86
+ end
87
+
88
+ describe "for arrays" do
89
+
90
+ before :all do
91
+ @array = JSON.parse(@json_array)
92
+ end
93
+
94
+ it "should find the index" do
95
+ @array.at_json_path('0.hash.one.two').should == 2
96
+ end
97
+
98
+ end
99
+
100
+ describe "for json strings" do
101
+
102
+ it "should parse 'number' as 1" do
103
+ @json.at_json_path("number").should == 1
104
+ end
105
+
106
+ it "should parse 'array' as [1,2,3]" do
107
+ @json.at_json_path('array').should == [1,2,3]
108
+ end
109
+
110
+ it "should parse 'hash.one' as {'two' => 2}" do
111
+ @json.at_json_path('hash.one').should == {'two' => 2}
112
+ end
113
+
114
+ it "should parse 'hash.one.two' as 2" do
115
+ @json.at_json_path('hash.one.two').should == 2
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+
123
+
124
+ ##Note on Patches/Pull Requests
125
+
126
+ * Fork the project.
127
+ * Make your feature addition or bug fix.
128
+ * Add tests for it. This is important so I don't break it in a
129
+ future version unintentionally.
130
+ * Commit, do not mess with rakefile, version, or history.
131
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
132
+ * Send me a pull request. Bonus points for topic branches.
133
+
134
+ ## Copyright
135
+
136
+ Copyright (c) 2011 Christopher Burnett. See LICENSE for details.
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+
3
+ module Pathy
4
+ InvalidPathError = Class.new(NoMethodError)
5
+
6
+ module InstanceMethods
7
+ def at_json_path path
8
+ method_chain = path.split('.')
9
+ method_chain.inject(self.reparsed_as_json) do |obj,m|
10
+ key = (obj.respond_to?(:push) ? m.to_i : m)
11
+ obj.send('[]', key) rescue raise InvalidPathError, "Could not resolve #{path} at #{key}"
12
+ end
13
+ end
14
+
15
+ def has_json_path? path
16
+ !!self.at_json_path(path)
17
+ rescue InvalidPathError
18
+ false
19
+ end
20
+
21
+ def reparsed_as_json
22
+ self.is_a?(String) ? JSON.parse(self) : JSON.parse(self.to_json)
23
+ end
24
+ end
25
+ module ClassMethods
26
+ def pathy!
27
+ self.send :include, InstanceMethods
28
+ end
29
+ end
30
+ end
31
+
32
+ Object.extend Pathy::ClassMethods
33
+
@@ -0,0 +1,3 @@
1
+ module Pathy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pathy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pathy"
7
+ s.version = Pathy::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Christopher Burnett"]
10
+ s.email = ["signalstatic@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/pathy"
12
+ s.summary = %q{JSON Validation Helper}
13
+ s.description = %q{Simple JSON Validation and rspec matchers}
14
+
15
+ s.rubyforge_project = "pathy"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pathy do
4
+ before :all do
5
+
6
+ Object.pathy!
7
+
8
+ @json = %[
9
+ {
10
+ "string" : "barr",
11
+ "number" : 1,
12
+ "array" : [1,2,3],
13
+ "hash" : {"one":{"two" : 2}}
14
+ }
15
+ ]
16
+
17
+ @json_array = %[
18
+ [{
19
+ "string" : "barr",
20
+ "number" : 1,
21
+ "array" : [1,2,3],
22
+ "hash" : {"one":{"two" : 2}}
23
+ }]
24
+ ]
25
+
26
+ end
27
+
28
+
29
+ describe "for hashes" do
30
+ before :all do
31
+ @obj = JSON.parse(@json)
32
+ @array = JSON.parse(@json_array)
33
+ end
34
+
35
+ it "should parse 'number' as 1" do
36
+ @obj.at_json_path("number").should == 1
37
+ end
38
+
39
+ it "should parse 'array' as [1,2,3]" do
40
+ @obj.at_json_path('array').should == [1,2,3]
41
+ end
42
+
43
+ it "should parse 'hash.one' as {'two' => 2}" do
44
+ @json.at_json_path('hash.one').should == {'two' => 2}
45
+ end
46
+
47
+ it "should parse 'hash.one' as {'two': 2}" do
48
+ @obj.at_json_path('hash.one.two').should == 2
49
+ end
50
+
51
+ describe "invalid paths" do
52
+ it "should raise InvalidPathError" do
53
+ lambda {
54
+ @obj.at_json_path('foo.bar')
55
+ }.should raise_error Pathy::InvalidPathError
56
+ end
57
+ end
58
+
59
+ describe "#has_json_path?" do
60
+ it "should be true for valid paths" do
61
+ @obj.has_json_path?('hash.one.two').should be_true
62
+ end
63
+ it "should be false for invalid paths" do
64
+ @obj.has_json_path?('hash.one.foo').should be_false
65
+ end
66
+
67
+ it "should work as rspec matcher" do
68
+ @obj.should have_json_path "hash.one"
69
+ end
70
+
71
+ end
72
+
73
+
74
+ end
75
+
76
+ describe "for arrays" do
77
+
78
+ before :all do
79
+ @array = JSON.parse(@json_array)
80
+ end
81
+
82
+ it "should find the index" do
83
+ @array.at_json_path('0.hash.one.two').should == 2
84
+ end
85
+
86
+ end
87
+
88
+ describe "for json strings" do
89
+
90
+ it "should parse 'number' as 1" do
91
+ @json.at_json_path("number").should == 1
92
+ end
93
+
94
+ it "should parse 'array' as [1,2,3]" do
95
+ @json.at_json_path('array').should == [1,2,3]
96
+ end
97
+
98
+ it "should parse 'hash.one' as {'two' => 2}" do
99
+ @json.at_json_path('hash.one').should == {'two' => 2}
100
+ end
101
+
102
+ it "should parse 'hash.one.two' as 2" do
103
+ @json.at_json_path('hash.one.two').should == 2
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'pathy'
4
+ require 'json'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Burnett
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-02 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple JSON Validation and rspec matchers
23
+ email:
24
+ - signalstatic@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - autotest/discover.rb
40
+ - lib/pathy.rb
41
+ - lib/pathy/version.rb
42
+ - pathy.gemspec
43
+ - spec/lib/pathy_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://rubygems.org/gems/pathy
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: pathy
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: JSON Validation Helper
79
+ test_files:
80
+ - spec/lib/pathy_spec.rb
81
+ - spec/spec_helper.rb