From 93c2ed4fbcdac323518da6faf4db957d1b714182 Mon Sep 17 00:00:00 2001 From: Brian Jones Date: Mon, 5 Mar 2012 22:20:03 -0500 Subject: [PATCH] Issue #68 * lib/jaribio/plan.rb: now inherits from RemoteObject * lib/jaribio/record.rb (initialize): fixed some bugs * lib/jaribio/remote_object.rb: new file, this is a base class for our active_resource objects that will automatically add the needed api_key query parameter as needed * lib/jaribio_formatter.rb: fixing some requires * spec/lib/jaribio/record_spec.rb: new spec * spec/lib/jaribio/rspec_formatter_spec.rb: fixed the spec related to results which was wrong before * spec/spec_helper.rb: require in jaribio_formatter instead of jaribio-formatter --- jaribio_formatter/lib/jaribio/plan.rb | 12 ++------ jaribio_formatter/lib/jaribio/record.rb | 8 +++--- .../lib/jaribio/remote_object.rb | 23 +++++++++++++++ jaribio_formatter/lib/jaribio_formatter.rb | 2 +- .../spec/lib/jaribio/record_spec.rb | 28 +++++++++++++++++++ .../spec/lib/jaribio/rspec_formatter_spec.rb | 10 +++---- jaribio_formatter/spec/spec_helper.rb | 2 +- 7 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 jaribio_formatter/lib/jaribio/remote_object.rb create mode 100644 jaribio_formatter/spec/lib/jaribio/record_spec.rb diff --git a/jaribio_formatter/lib/jaribio/plan.rb b/jaribio_formatter/lib/jaribio/plan.rb index 6061ccb..8f45070 100644 --- a/jaribio_formatter/lib/jaribio/plan.rb +++ b/jaribio_formatter/lib/jaribio/plan.rb @@ -1,15 +1,7 @@ -require 'active_resource' +require 'jaribio/remote_object' module Jaribio - class Plan < ActiveResource::Base - # site is set as needed by formatters - - # use json, not xml - self.format = :json - - # set timeout to 5 seconds (does not affect DNS lookups generally) - self.timeout = 5 - + class Plan < RemoteObject # Usage: Jaribio::Plan.find(1, :params => {'api_key' => 'asdf'}) end end diff --git a/jaribio_formatter/lib/jaribio/record.rb b/jaribio_formatter/lib/jaribio/record.rb index 6ce2750..530ffa3 100644 --- a/jaribio_formatter/lib/jaribio/record.rb +++ b/jaribio_formatter/lib/jaribio/record.rb @@ -5,10 +5,10 @@ class Record attr_accessor :key, :description, :state - def initialize(args) - key = args[:key] - description = args[:description] - state = args[:state] + def initialize(args = {}) + self.key = args[:key] + self.description = args[:description] + self.state = args[:state] end def failed? diff --git a/jaribio_formatter/lib/jaribio/remote_object.rb b/jaribio_formatter/lib/jaribio/remote_object.rb new file mode 100644 index 0000000..71549a3 --- /dev/null +++ b/jaribio_formatter/lib/jaribio/remote_object.rb @@ -0,0 +1,23 @@ +require 'active_resource' +require 'active_support/core_ext/class/attribute_accessors' + +module Jaribio + class RemoteObject < ActiveResource::Base + cattr_accessor :api_key + + # use json, not xml + self.format = :json + + # set timeout to 5 seconds (does not affect DNS lookups generally) + self.timeout = 5 + + class << self + def query_string(options) + options = {} if options.nil? + options[:api_key] = api_key unless api_key.nil? + super(options) + end + end + + end +end diff --git a/jaribio_formatter/lib/jaribio_formatter.rb b/jaribio_formatter/lib/jaribio_formatter.rb index 76353b6..593e672 100644 --- a/jaribio_formatter/lib/jaribio_formatter.rb +++ b/jaribio_formatter/lib/jaribio_formatter.rb @@ -1,3 +1,3 @@ -%w(rspec_formatter.rb record.rb formatter/version.rb).each do |file| +%w(remote_object plan rspec_formatter record formatter/version).each do |file| require File.expand_path(File.join(File.dirname(__FILE__), 'jaribio', file)) end diff --git a/jaribio_formatter/spec/lib/jaribio/record_spec.rb b/jaribio_formatter/spec/lib/jaribio/record_spec.rb new file mode 100644 index 0000000..3c0417e --- /dev/null +++ b/jaribio_formatter/spec/lib/jaribio/record_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe "Jaribio::Record" do + describe "#failed?" do + it "is true when not set" do + Jaribio::Record.new().failed?.should be_true + end + + it "is true when state is FAIL" do + Jaribio::Record.new(:state => Jaribio::Record::FAIL).failed?.should be_true + end + + it "is false when state is PASS" do + Jaribio::Record.new(:state => Jaribio::Record::PASS).failed?.should be_false + end + end + + describe "#eql?" do + let(:attributes) { Hash.new(:key => 'key', :description => 'description', :state => Jaribio::Record::PASS) } + + it "true if attributes are eql?" do + a = Jaribio::Record.new(attributes) + b = Jaribio::Record.new(attributes) + a.should eql(b) + end + end + +end diff --git a/jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb b/jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb index 4a69c27..d6b1911 100644 --- a/jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb +++ b/jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb @@ -86,11 +86,11 @@ def verify_key_and_description(example, expected_key, expected_description) it "values are a hash with description and failed state" do formatter.results.should eql({ - 'e2' => Jaribio::Record.new(:key => 'e2', :description => 'object example 2', :failed => true), - 'g1' => Jaribio::Record.new(:key => 'g1', :description => 'object subgroup', :failed => true), - 'g1e2' => Jaribio::Record.new(:key => 'g1e2', :description => 'object subgroup example 2', :failed => true), - 'object' => Jaribio::Record.new(:key => 'object', :description => 'object', :failed => true), - 'object subgroup2' => Jaribio::Record.new(:key => 'object subgroup2', :description => 'object subgroup2', :failed => true), + 'e2' => Jaribio::Record.new(:key => 'e2', :description => 'object example 2', :state => Jaribio::Record::FAIL), + 'g1' => Jaribio::Record.new(:key => 'g1', :description => 'object subgroup', :state => Jaribio::Record::FAIL), + 'g1e2' => Jaribio::Record.new(:key => 'g1e2', :description => 'object subgroup example 2', :state => Jaribio::Record::FAIL), + 'object' => Jaribio::Record.new(:key => 'object', :description => 'object', :state => Jaribio::Record::FAIL), + 'object subgroup2' => Jaribio::Record.new(:key => 'object subgroup2', :description => 'object subgroup2', :state => Jaribio::Record::FAIL), }) end end diff --git a/jaribio_formatter/spec/spec_helper.rb b/jaribio_formatter/spec/spec_helper.rb index 6d6c196..9fa3d3e 100644 --- a/jaribio_formatter/spec/spec_helper.rb +++ b/jaribio_formatter/spec/spec_helper.rb @@ -7,7 +7,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rspec' -require 'jaribio-formatter' +require 'jaribio_formatter' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories.