Permalink
Browse files

adding README and rspec helper

  • Loading branch information...
1 parent 736c9db commit 38340eacde6ffba56c8823fbced0c044ddef5f9a @twoism committed Mar 2, 2011
Showing with 191 additions and 1 deletion.
  1. +20 −0 LICENSE
  2. +127 −0 README.md
  3. +12 −1 lib/pathy.rb
  4. +32 −0 spec/lib/pathy_spec.rb
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Christopher Burnett
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
127 README.md
@@ -0,0 +1,127 @@
+### Pathy ###
+
+ JSON validation helper.
+
+### Installation ###
+
+ gem install pathy
+
+### Usage ###
+
+ describe Pathy do
+ before :all do
+
+ Object.pathy!
+
+ @json = %[
+ {
+ "string" : "barr",
+ "number" : 1,
+ "array" : [1,2,3],
+ "hash" : {"one":{"two" : 2}}
+ }
+ ]
+
+ @json_array = %[
+ [{
+ "string" : "barr",
+ "number" : 1,
+ "array" : [1,2,3],
+ "hash" : {"one":{"two" : 2}}
+ }]
+ ]
+
+ end
+
+
+ describe "for hashes" do
+ before :all do
+ @obj = JSON.parse(@json)
+ @array = JSON.parse(@json_array)
+ end
+
+ it "should parse 'number' as 1" do
+ @obj.at_json_path("number").should == 1
+ end
+
+ it "should parse 'array' as [1,2,3]" do
+ @obj.at_json_path('array').should == [1,2,3]
+ end
+
+ it "should parse 'hash.one' as {'two' => 2}" do
+ @json.at_json_path('hash.one').should == {'two' => 2}
+ end
+
+ it "should parse 'hash.one' as {'two': 2}" do
+ @obj.at_json_path('hash.one.two').should == 2
+ end
+
+ describe "invalid paths" do
+ it "should raise InvalidPathError" do
+ lambda {
+ @obj.at_json_path('foo.bar')
+ }.should raise_error Pathy::InvalidPathError
+ end
+ end
+
+ describe "#has_json_path?" do
+ it "should be true for valid paths" do
+ @obj.has_json_path?('hash.one.two').should be_true
+ end
+ it "should be false for invalid paths" do
+ @obj.has_json_path?('hash.one.foo').should be_false
+ end
+
+ end
+
+
+ end
+
+ describe "for arrays" do
+
+ before :all do
+ @array = JSON.parse(@json_array)
+ end
+
+ it "should find the index" do
+ @array.at_json_path('0.hash.one.two').should == 2
+ end
+
+ end
+
+ describe "for json strings" do
+
+ it "should parse 'number' as 1" do
+ @json.at_json_path("number").should == 1
+ end
+
+ it "should parse 'array' as [1,2,3]" do
+ @json.at_json_path('array').should == [1,2,3]
+ end
+
+ it "should parse 'hash.one' as {'two' => 2}" do
+ @json.at_json_path('hash.one').should == {'two' => 2}
+ end
+
+ it "should parse 'hash.one.two' as 2" do
+ @json.at_json_path('hash.one.two').should == 2
+ end
+
+ end
+
+ end
+
+
+##Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (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)
+* Send me a pull request. Bonus points for topic branches.
+
+## Copyright
+
+Copyright (c) 2011 Christopher Burnett. See LICENSE for details.
View
@@ -1,10 +1,21 @@
require 'json'
module Pathy
+ InvalidPathError = Class.new(NoMethodError)
+
module InstanceMethods
def at_json_path path
method_chain = path.split('.')
- method_chain.inject(self.reparsed_as_json) {|obj,m| obj.send('[]', (obj.respond_to?(:push) ? m.to_i : m) ) }
+ method_chain.inject(self.reparsed_as_json) do |obj,m|
+ key = (obj.respond_to?(:push) ? m.to_i : m)
+ obj.send('[]', key) rescue raise InvalidPathError, "Could not resolve #{path} at #{key}"
+ end
+ end
+
+ def has_json_path? path
+ !!self.at_json_path(path)
+ rescue InvalidPathError
+ false
end
def reparsed_as_json
View
@@ -2,7 +2,9 @@
describe Pathy do
before :all do
+
Object.pathy!
+
@json = %[
{
"string" : "barr",
@@ -38,10 +40,33 @@
@obj.at_json_path('array').should == [1,2,3]
end
+ it "should parse 'hash.one' as {'two' => 2}" do
+ @json.at_json_path('hash.one').should == {'two' => 2}
+ end
+
it "should parse 'hash.one' as {'two': 2}" do
@obj.at_json_path('hash.one.two').should == 2
end
+ describe "invalid paths" do
+ it "should raise InvalidPathError" do
+ lambda {
+ @obj.at_json_path('foo.bar')
+ }.should raise_error Pathy::InvalidPathError
+ end
+ end
+
+ describe "#has_json_path?" do
+ it "should be true for valid paths" do
+ @obj.has_json_path?('hash.one.two').should be_true
+ end
+ it "should be false for invalid paths" do
+ @obj.has_json_path?('hash.one.foo').should be_false
+ end
+
+ end
+
+
end
describe "for arrays" do
@@ -53,9 +78,11 @@
it "should find the index" do
@array.at_json_path('0.hash.one.two').should == 2
end
+
end
describe "for json strings" do
+
it "should parse 'number' as 1" do
@json.at_json_path("number").should == 1
end
@@ -64,10 +91,15 @@
@json.at_json_path('array').should == [1,2,3]
end
+ it "should parse 'hash.one' as {'two' => 2}" do
+ @json.at_json_path('hash.one').should == {'two' => 2}
+ end
+
it "should parse 'hash.one.two' as 2" do
@json.at_json_path('hash.one.two').should == 2
end
end
+
end

0 comments on commit 38340ea

Please sign in to comment.