Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby
Tree: d68a22a926

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
autotest
lib
spec
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE
README.md
Rakefile
pathy.gemspec

README.md

Pathy

JSON validation helper.

Installation

gem install pathy

Usage

require 'spec_helper'

describe Pathy do
  before :all do

    # add the helper methods to any object.
    # this can be called on any class.
    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

      it "should work as rspec matcher" do
        @obj.should have_json_path "hash.one"
      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.

Something went wrong with that request. Please try again.