Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add script for validating JSON

* Add classes to validate JSON
* Add Gemfile for supporting libraries
* Add script to contribution guidelines
* Split out classes into separate files
* Require gems in main TrailRunner class
* Expand error message to recommend jsonlint.com
* Write specs

Other changes:
* Move script into `lib/tasks/` and make directory-agnostic
* Sort gems alphabetically
* Don't require mocha
* Add awesome_print to Gemfile for debugging
* Clean up Gemfile
* Specify ruby version in Gemfile
* Sort gems
* Remove awesome_print (used only for diagnostics)
* Change mocha require to remove deprecation message
  • Loading branch information...
commit 197b0746d4f0ea6e9d877238425fc4bbbfb891cb 1 parent efa2c9e
Adarsh Pandit authored
View
3  CONTRIBUTING.md
@@ -13,7 +13,8 @@ Contributing
> In which format should my contributions be submitted?
* Pull requests should be in JSON format
-* PRs should be validated before submission using [jsonlint](http://jsonlint.com/).
+* Run the json validator script (`ruby lib/json_validator.rb`)
+* For more detailed output, use [JSONlint.com](http://jsonlint.com).
* Considering using [this regex](https://gist.github.com/4068038) for converting Markdown to JSON
Merging Pull Requests (for admins)
View
9 Gemfile
@@ -0,0 +1,9 @@
+source :rubygems
+
+ruby '1.9.3'
+
+gem 'bourne'
+gem 'colored'
+gem 'json'
+gem 'mocha', require: false
+gem 'rspec'
View
31 Gemfile.lock
@@ -0,0 +1,31 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ awesome_print (1.1.0)
+ bourne (1.2.0)
+ mocha (= 0.12.3)
+ colored (1.2)
+ diff-lcs (1.1.3)
+ json (1.7.5)
+ metaclass (0.0.1)
+ mocha (0.12.3)
+ metaclass (~> 0.0.1)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.0)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ awesome_print
+ bourne
+ colored
+ json
+ mocha
+ rspec
View
38 lib/helpers/json_validator.rb
@@ -0,0 +1,38 @@
+class JSONValidator
+ def initialize(file_name)
+ @file_name = file_name
+ @contents = File.open(@file_name).read
+ end
+
+ def run
+ check_json_validation
+ end
+
+ private
+
+ def check_json_validation
+ if valid_json?
+ print_progress_marker
+ else
+ print_error_message
+ end
+ end
+
+ def valid_json?
+ begin
+ JSON.parse(@contents)
+ return true
+ rescue Exception => e
+ return false
+ end
+ end
+
+ def print_progress_marker
+ print '.'
+ end
+
+ def print_error_message
+ puts "\nERROR: #{@file_name} is not valid JSON. Learn more:"
+ puts "cat #{@file_name} | pbcopy; open http://jsonlint.com"
+ end
+end
View
32 lib/helpers/trail_runner.rb
@@ -0,0 +1,32 @@
+require 'json'
+require 'rubygems'
+
+class TrailRunner
+ def run
+ print_starting_message
+ run_validations_on_json_files
+ print_closing_puts
+ end
+
+ private
+
+ def print_starting_message
+ puts "Starting JSON validation\n"
+ end
+
+ def run_validations_on_json_files
+ json_files.each { |file| JSONValidator.new(file).run }
+ end
+
+ def json_files
+ Dir["#{script_directory}/../../trails/*.json"]
+ end
+
+ def script_directory
+ File.dirname(__FILE__)
+ end
+
+ def print_closing_puts
+ puts
+ end
+end
View
8 lib/validate_json.rb
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby
+
+script_directory = File.dirname(__FILE__)
+helper_files = script_directory + '/helpers/*.rb'
+
+Dir[helper_files].each { |file| require File.expand_path(file) }
+
+TrailRunner.new.run
View
21 spec/fixtures/invalid.json
@@ -0,0 +1,21 @@
+ "name": "UNIX",
+ "description": "A multitasking, multi-user computer operating system originally developed in 1969, variants of which now form the underpinning of many operating systems, including OS X, iOS, and Linux.",
+ "steps": [
+ {
+ "name": "Critical Learning",
+ "resources": [
+ {
+ "title": "Read The Linux Programming Interface. Chapters 1, 2, 6, 8, 10, 14, 15, 18, 25, 44, 62.",
+ "uri": "http://amzn.to/the-linux-programming-interface"
+ },
+ {
+ "title": "Read about I/O redirection.",
+ "uri": "http://en.wikipedia.org/wiki/Redirection_%28computing%29"
+ }
+ ],
+ "validations": [
+ "You can use these commands and operators: |, <, >, >>, &, ack, awk, cat, chmod, chown, cp, export, find, kill, locate, ls, mkdir, mv, ps, rm, sed, sort, tail, top, vim, whereis, xargs"
+ ]
+ }
+ ]
+}
View
22 spec/fixtures/valid.json
@@ -0,0 +1,22 @@
+{
+ "name": "UNIX",
+ "description": "A multitasking, multi-user computer operating system originally developed in 1969, variants of which now form the underpinning of many operating systems, including OS X, iOS, and Linux.",
+ "steps": [
+ {
+ "name": "Critical Learning",
+ "resources": [
+ {
+ "title": "Read The Linux Programming Interface. Chapters 1, 2, 6, 8, 10, 14, 15, 18, 25, 44, 62.",
+ "uri": "http://amzn.to/the-linux-programming-interface"
+ },
+ {
+ "title": "Read about I/O redirection.",
+ "uri": "http://en.wikipedia.org/wiki/Redirection_%28computing%29"
+ }
+ ],
+ "validations": [
+ "You can use these commands and operators: |, <, >, >>, &, ack, awk, cat, chmod, chown, cp, export, find, kill, locate, ls, mkdir, mv, ps, rm, sed, sort, tail, top, vim, whereis, xargs"
+ ]
+ }
+ ]
+}
View
24 spec/json_validator_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe JSONValidator, '#run' do
+ it 'returns a dot if the JSON is valid' do
+ $stdout = io = StringIO.new
+ dir = File.dirname(__FILE__)
+ file_name = File.open(dir + '/fixtures/valid.json')
+
+ JSONValidator.new(file_name).run
+
+ io.string.should eq '.'
+ end
+
+
+ it 'returns an error if the JSON is not valid' do
+ $stdout = io = StringIO.new
+ dir = File.dirname(__FILE__)
+ file_name = File.open(dir + '/fixtures/invalid.json')
+
+ JSONValidator.new(file_name).run
+
+ io.string.should =~ /ERROR/
+ end
+end
View
10 spec/spec_helper.rb
@@ -0,0 +1,10 @@
+script_directory = File.dirname(__FILE__)
+helper_files = script_directory + '/../lib/helpers/*.rb'
+
+Dir[helper_files].each do |f|
+ require File.expand_path(f)
+end
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
View
38 spec/trail_runner_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+require 'mocha/api'
+require 'rspec/mocks/standalone'
+require 'bourne'
+
+describe TrailRunner, '#run' do
+ it 'returns a starting message' do
+ $stdout = io = StringIO.new
+
+ TrailRunner.new.run
+
+ io.string.split("\n")[0].should =~ /Starting/
+ end
+
+ it 'calls JSONValidator.new(file).run on each file' do
+ validator_stub = stub('json validator stub', :run)
+ JSONValidator.stubs(:new).returns(validator_stub)
+
+ dir = File.dirname(__FILE__)
+ file_name = File.open(dir + '/fixtures/valid.json')
+ files = [file_name]
+ runner = TrailRunner.new
+ runner.stubs(:json_files).returns(files)
+
+ runner.run
+
+ JSONValidator.should have_received(:new)
+ end
+
+ it 'prints a closing puts' do
+ $stdout = io = StringIO.new
+
+ TrailRunner.new.run
+
+ output = io.string[-1, 1]
+ output.should == "\n"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.