Skip to content
This repository was archived by the owner on Nov 13, 2020. It is now read-only.

Commit 36a4ce2

Browse files
committed
Initial commit.
This is all just the setup stuff. Any valid JSON is valid JSON-API here :wink:
0 parents  commit 36a4ce2

11 files changed

+180
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in json-api.gemspec
4+
gemspec

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Steve Klabnik
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# JSON::Api
2+
3+
A parser and validator for [JSON API](http://jsonapi.org) documents.
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'json-api'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install json-api
18+
19+
## Usage
20+
21+
```ruby
22+
require 'json/api'
23+
24+
json = <<-JSON
25+
{
26+
"posts": [{
27+
"id": "1",
28+
"title": "Rails is Omakase",
29+
"links": {
30+
"author": "9",
31+
"comments": [ "5", "12", "17", "20" ]
32+
}
33+
}]
34+
}
35+
JSON
36+
37+
puts JSON::Api.parse(json).class # => Hash
38+
```
39+
40+
## Contributing
41+
42+
1. Fork it
43+
2. Create your feature branch (`git checkout -b my-new-feature`)
44+
3. Commit your changes (`git commit -am 'Add some feature'`)
45+
4. Push to the branch (`git push origin my-new-feature`)
46+
5. Create new Pull Request

Rakefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "bundler/gem_tasks"
2+
3+
require "bundler/gem_tasks"
4+
5+
require 'rake/testtask'
6+
7+
Rake::TestTask.new do |t|
8+
t.libs << "lib"
9+
t.test_files = FileList['test/*_test.rb']
10+
t.ruby_opts = ['-r./test/test_helper.rb']
11+
t.verbose = true
12+
end
13+
14+
task :default => :test

json-api.gemspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'json/api/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "json-api"
8+
spec.version = JSON::Api::VERSION
9+
spec.authors = ["Steve Klabnik"]
10+
spec.email = ["[email protected]"]
11+
spec.description = %q{A JSON API parser.}
12+
spec.summary = %q{A JSON API parser.}
13+
spec.homepage = "http://jsonapi.org"
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files`.split($/)
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_development_dependency "json-schema"
22+
23+
spec.add_development_dependency "bundler", "~> 1.3"
24+
spec.add_development_dependency "rake"
25+
end

lib/json/api.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "json/api/version"
2+
require 'json'
3+
require 'json-schema'
4+
5+
module JSON
6+
module Api
7+
def self.parse(source, opts={})
8+
json = JSON.parse(source, opts)
9+
10+
validate(json, opts)
11+
12+
json
13+
# rescue
14+
# nil
15+
end
16+
17+
def self.validate(source, opts={})
18+
json = JSON.parse(source) if source.kind_of?(String)
19+
20+
JSON::Validator.validate!('lib/json/api/schema.json', json)
21+
end
22+
end
23+
end

lib/json/api/schema.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#"
3+
}

lib/json/api/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module JSON
2+
module Api
3+
VERSION = "0.1.0"
4+
end
5+
end

test/acceptance_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'json/api'
2+
3+
class AcceptanceTest < MiniTest::Unit::TestCase
4+
def test_homepage_id_style
5+
json = <<-JSON
6+
{
7+
"posts": [{
8+
"id": "1",
9+
"title": "Rails is Omakase",
10+
"links": {
11+
"author": "9",
12+
"comments": [ "5", "12", "17", "20" ]
13+
}
14+
}]
15+
}
16+
JSON
17+
18+
assert JSON::Api.parse(json), "failed to parse"
19+
end
20+
end

test/test_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'minitest/autorun'

0 commit comments

Comments
 (0)