From 84afc887087cb832cc576e76a7df94fe8ab20909 Mon Sep 17 00:00:00 2001 From: "Brandon M. West" Date: Fri, 27 May 2011 21:29:13 -0600 Subject: [PATCH] add rspec tests, mostly passing --- Gemfile | 11 +- Rakefile | 2 +- lib/ruby-rainmaker.rb | 4 + lib/ruby_rainmaker/api.rb | 2 +- .../{config.rb => configuration.rb} | 1 - lib/ruby_rainmaker/connection.rb | 3 +- spec/fixtures/person.json | 168 ++++++++++++++++++ spec/helper.rb | 31 ++++ spec/ruby_rainmaker/api_spec.rb | 65 +++++++ spec/ruby_rainmaker/client/person_spec.rb | 12 ++ spec/ruby_rainmaker/client_spec.rb | 10 ++ spec/ruby_rainmaker_spec.rb | 105 +++++++++++ test/helper.rb | 18 -- test/test_ruby-rainmaker.rb | 7 - 14 files changed, 405 insertions(+), 34 deletions(-) rename lib/ruby_rainmaker/{config.rb => configuration.rb} (98%) create mode 100644 spec/fixtures/person.json create mode 100644 spec/helper.rb create mode 100644 spec/ruby_rainmaker/api_spec.rb create mode 100644 spec/ruby_rainmaker/client/person_spec.rb create mode 100644 spec/ruby_rainmaker/client_spec.rb create mode 100644 spec/ruby_rainmaker_spec.rb delete mode 100644 test/helper.rb delete mode 100644 test/test_ruby-rainmaker.rb diff --git a/Gemfile b/Gemfile index f7fa9d8..91ffd1f 100644 --- a/Gemfile +++ b/Gemfile @@ -7,10 +7,13 @@ source "http://rubygems.org" # Include everything needed to run rake, tests, features, etc. group :development do gem "rake", "0.8.7" - gem "shoulda", ">= 0" - gem "bundler", "~> 1.0.0" - gem "jeweler", "~> 1.6.0" - gem "rcov", ">= 0" + gem "shoulda", ">= 0" + gem "bundler", "~> 1.0.0" + gem "jeweler", "~> 1.6.0" + gem "rcov", ">= 0" + gem "rspec" gem "faraday" gem "faraday_middleware" + gem "simplecov" + gem "webmock" end diff --git a/Rakefile b/Rakefile index 3275662..2f2fafd 100644 --- a/Rakefile +++ b/Rakefile @@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem| gem.homepage = "http://github.com/brandonmwest/rainmaker" gem.license = "MIT" gem.summary = %Q{Wrapper for the rainmaker API} - gem.description = %Q{TODO: longer description of your gem} + gem.description = %Q{Wrapper for the rainmaker.cc API} gem.email = "brawest@gmail.com" gem.authors = ["Brandon M. West"] # dependencies defined in Gemfile diff --git a/lib/ruby-rainmaker.rb b/lib/ruby-rainmaker.rb index 7ef6b94..f8d2ca1 100644 --- a/lib/ruby-rainmaker.rb +++ b/lib/ruby-rainmaker.rb @@ -1,5 +1,9 @@ require "faraday" require "faraday_middleware" +require 'ruby_rainmaker/error' +require 'ruby_rainmaker/configuration' +require 'ruby_rainmaker/api' +require 'ruby_rainmaker/client' module RubyRainmaker extend Configuration diff --git a/lib/ruby_rainmaker/api.rb b/lib/ruby_rainmaker/api.rb index e0d27de..866a8b7 100644 --- a/lib/ruby_rainmaker/api.rb +++ b/lib/ruby_rainmaker/api.rb @@ -1,7 +1,7 @@ require 'ruby_rainmaker/connection' require 'ruby_rainmaker/request' -module Twitter +module RubyRainmaker # @private class API # @private diff --git a/lib/ruby_rainmaker/config.rb b/lib/ruby_rainmaker/configuration.rb similarity index 98% rename from lib/ruby_rainmaker/config.rb rename to lib/ruby_rainmaker/configuration.rb index a7ec9c6..cc36aa9 100644 --- a/lib/ruby_rainmaker/config.rb +++ b/lib/ruby_rainmaker/configuration.rb @@ -1,5 +1,4 @@ require 'faraday' -require 'ruby_rainmaker/version' module RubyRainmaker # Defines constants and methods related to configuration diff --git a/lib/ruby_rainmaker/connection.rb b/lib/ruby_rainmaker/connection.rb index b0103a3..5ef33fa 100644 --- a/lib/ruby_rainmaker/connection.rb +++ b/lib/ruby_rainmaker/connection.rb @@ -17,8 +17,7 @@ def connection(raw=false) } Faraday.new(options) do |builder| - builder.use Faraday::Request::MultipartWithFile - builder.use Faraday::Request::Multipart + builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded builder.use Faraday::Request::Gateway, gateway if gateway builder.use Faraday::Response::RaiseHttp4xx diff --git a/spec/fixtures/person.json b/spec/fixtures/person.json new file mode 100644 index 0000000..fa58f43 --- /dev/null +++ b/spec/fixtures/person.json @@ -0,0 +1,168 @@ +{ + "status": 200, + "contactInfo": { + "familyName": "Lorang", + "givenName": "Bart", + "fullName": "Bart Lorang", + "emailAddresses": + [ + "lorangb@gmail.com" + ] +}, + "interests": { + "Football": true, + "Sports & Recreation": true, + "Business": true, + "Online News": true, + "Baseball": true, + "Tennis": true, + "News & Current Events": true, + "Basketball": true, + "Blogging": true, + "Social Networks": true, + "Online Journals": true, + "Golf": true, + "Technology": true +}, + "organizations": +[ + { + "name": "Forseti Holdings, LLC", + "title": "Chairman & CEO", + "startDate": "2010-01" + }, + { + "name": "Rainmaker Technologies", + "title": "CEO", + "startDate": "2010-01" + }, + { + "name": "Forseti Holdings LLC", + "title": "Chairman & CEO", + "isPrimary": true + }, + { + "name": "CloudCenter LLC", + "title": "Chairman & CEO", + "isPrimary": false + }, + { + "name": "DTS", + "isPrimary": false + } +], + "demographics": { + "influencerScore": "81-90", + "householdIncome": "250k+", + "age": "31", + "homeOwnerStatus": "Own", + "locationGeneral": "Denver, Colorado, United States", + "children": "No", + "gender": "Male", + "maritalStatus": "Single" +}, + "socialProfiles": +[ + { + "type": "facebook", + "url": "http://www.facebook.com/bart.lorang", + "id": "651620441", + "birthday": "08/16/1979", + "username": "bart.lorang" + }, + { + "url": "http://twitter.com/lorangb", + "id": "5998422", + "type": "twitter", + "username": "lorangb" + }, + { + "url": "http://www.linkedin.com/in/bartlorang", + "id": "bartlorang", + "type": "linkedin", + "username": "bartlorang" + }, + { + "url": "http://about.me/lorangb", + "type": "about.me" + }, + { + "url": "http://www.flickr.com/people/39267654@N00/", + "id": "39267654@N00", + "type": "flickr" + }, + { + "url": "http://profiles.friendster.com/6986589", + "type": "friendster" + }, + { + "url": "https://profiles.google.com/lorangb", + "id": "lorangb", + "type": "google profile", + "username": "lorangb" + }, + { + "url": "http://www.myspace.com/137200880", + "type": "myspace" + }, + { + "url": "http://picasaweb.google.com/lorangb", + "type": "picasa" + }, + { + "url": "http://tungle.me/bartlorang", + "id": "bartlorang", + "type": "tungle.me", + "username": "bartlorang" + }, + { + "url": "http://youtube.com/lorangb", + "type": "youtube" + }, + { + "type": "friendster", + "url": "http://profiles.friendster.com/6986589" + } +] + "photos": +[ + { + "url": "http://graph.facebook.com/651620441/picture?type=large", + "type": "facebook" + }, + { + "url": "https://lh5.googleusercontent.com/-EkI-dQC-4iM/AAAAAAAAAAI/AAAAAAAAAAA/o2NExlQVurA/photo.jpg?sz=200", + "type": "google profile" + }, + { + "url": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41508_651620441_4210927_n.jpg", + "type": "facebook" + }, + { + "url": "http://photos.friendster.com/photos/98/56/6986589/4262265956117t.jpg", + "type": "friendster" + }, + { + "url": "http://c2.ac-images.myspacecdn.com/images01/128/l_d7f2149dcb61b290b25232bf4c7968b9.jpg", + "type": "myspace" + }, + { + "url": "http://images.plaxo.com/fetch_image?path=249108119662_0_-413637613", + "type": "plaxo" + + }, + { + "url": "http://a1.twimg.com/profile_images/712689472/Me.png", + "type": "twitter" + + }, + { + "type": "gravatar", + "url": "https://secure.gravatar.com/avatar/956b7dca7c77a12c43ebe9ae09dfaba8" + }, + { + "type": "linkedin", + "url": "http://media.linkedin.com/mpr/mpr/shrink_80_80/p/2/000/086/2c8/2444fae.jpg" + } +], +} \ No newline at end of file diff --git a/spec/helper.rb b/spec/helper.rb new file mode 100644 index 0000000..9aaf3e0 --- /dev/null +++ b/spec/helper.rb @@ -0,0 +1,31 @@ +require 'webmock/rspec' +require 'simplecov' +require 'ruby-rainmaker' +require 'rspec' + +SimpleCov.start do + add_group 'RubyRainmaker', 'lib/ruby_rainmaker' + add_group 'Faraday Middleware', 'lib/faraday' + add_group 'Specs', 'spec' +end + + +RSpec.configure do |config| + config.include WebMock::API +end + +def a_get(path) + a_request(:get, RubyRainmaker.endpoint + path) +end + +def stub_get(path) + stub_request(:get, RubyRainmaker.endpoint + path) +end + +def fixture_path + File.expand_path("../fixtures", __FILE__) +end + +def fixture(file) + File.new(fixture_path + '/' + file) +end diff --git a/spec/ruby_rainmaker/api_spec.rb b/spec/ruby_rainmaker/api_spec.rb new file mode 100644 index 0000000..2fdf37b --- /dev/null +++ b/spec/ruby_rainmaker/api_spec.rb @@ -0,0 +1,65 @@ +require 'helper' + +describe RubyRainmaker::API do + before do + @keys = RubyRainmaker::Configuration::VALID_OPTIONS_KEYS + end + + context "with module configuration" do + before do + RubyRainmaker.configure do |config| + @keys.each do |key| + config.send("#{key}=", key) + end + end + end + + after do + RubyRainmaker.reset + end + + it "should inherit module configuration" do + api = RubyRainmaker::API.new + @keys.each do |key| + api.send(key).should == key + end + end + + context "with class configuration" do + + before do + @configuration = { + :consumer_key => 'CK', + :adapter => :typhoeus, + :endpoint => 'http://tumblr.com/', + :gateway => 'apigee-1111.apigee.com', + :format => :xml, + :proxy => 'http://erik:sekret@proxy.example.com:8080', + :user_agent => 'Custom User Agent', + } + end + + context "during initialization" + + it "should override module configuration" do + api = RubyRainmaker::API.new(@configuration) + @keys.each do |key| + api.send(key).should == @configuration[key] + end + end + + context "after initilization" do + + it "should override module configuration after initialization" do + api = RubyRainmaker::API.new + @configuration.each do |key, value| + api.send("#{key}=", value) + end + @keys.each do |key| + api.send(key).should == @configuration[key] + end + end + end + end + end +end diff --git a/spec/ruby_rainmaker/client/person_spec.rb b/spec/ruby_rainmaker/client/person_spec.rb new file mode 100644 index 0000000..f1a9574 --- /dev/null +++ b/spec/ruby_rainmaker/client/person_spec.rb @@ -0,0 +1,12 @@ +require 'helper' + +describe RubyRainmaker::Client do + RubyRainmaker::Configuration::VALID_FORMATS.each do |format| + context ".new(:format => '#{format}')" do + before do + @client = RubyRainmaker::Client.new(:format => format, :consumer_key => 'CK') + end + + end + end + end \ No newline at end of file diff --git a/spec/ruby_rainmaker/client_spec.rb b/spec/ruby_rainmaker/client_spec.rb new file mode 100644 index 0000000..9663ef7 --- /dev/null +++ b/spec/ruby_rainmaker/client_spec.rb @@ -0,0 +1,10 @@ +require 'helper' + +describe RubyRainmaker::Client do + it "should connect using the endpoint configuration" do + client = RubyRainmaker::Client.new + endpoint = URI.parse(client.api_endpoint) + connection = client.send(:connection).build_url(nil).to_s + connection.should == endpoint.to_s + end +end diff --git a/spec/ruby_rainmaker_spec.rb b/spec/ruby_rainmaker_spec.rb new file mode 100644 index 0000000..9ba63a1 --- /dev/null +++ b/spec/ruby_rainmaker_spec.rb @@ -0,0 +1,105 @@ +require 'helper' + +describe RubyRainmaker do + after do + RubyRainmaker.reset + end + + context "when delegating to a client" do + + before do + stub_get("person.json"). + with(:query => {:email => "lorangb@gmail.com"}). + to_return(:body => fixture("person.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + + it "should get the correct resource" do + RubyRainmaker.person('lorangb@gmail.com') + a_get("person.json"). + with(:query => {:email => "lorangb@gmail.com"}). + should have_been_made + end + + it "should return the same results as a client" do + RubyRainmaker.person('lorangb@gmail.com').should == RubyRainmaker::Client.new.person('lorangb@gmail.com') + end + + end + + describe '.respond_to?' do + it 'takes an optional include private argument' do + RubyRainmaker.respond_to?(:client, true).should be_true + end + end + + describe ".client" do + it "should be a RubyRainmaker::Client" do + RubyRainmaker.client.should be_a RubyRainmaker::Client + end + end + + describe ".adapter" do + it "should return the default adapter" do + RubyRainmaker.adapter.should == RubyRainmaker::Configuration::DEFAULT_ADAPTER + end + end + + describe ".adapter=" do + it "should set the adapter" do + RubyRainmaker.adapter = :typhoeus + RubyRainmaker.adapter.should == :typhoeus + end + end + + describe ".endpoint" do + it "should return the default endpoint" do + RubyRainmaker.endpoint.should == RubyRainmaker::Configuration::DEFAULT_ENDPOINT + end + end + + describe ".endpoint=" do + it "should set the endpoint" do + RubyRainmaker.endpoint = 'http://tumblr.com/' + RubyRainmaker.endpoint.should == 'http://tumblr.com/' + end + end + + describe ".format" do + it "should return the default format" do + RubyRainmaker.format.should == RubyRainmaker::Configuration::DEFAULT_FORMAT + end + end + + describe ".format=" do + it "should set the format" do + RubyRainmaker.format = 'xml' + RubyRainmaker.format.should == 'xml' + end + end + + describe ".user_agent" do + it "should return the default user agent" do + RubyRainmaker.user_agent.should == RubyRainmaker::Configuration::DEFAULT_USER_AGENT + end + end + + describe ".user_agent=" do + it "should set the user_agent" do + RubyRainmaker.user_agent = 'Custom User Agent' + RubyRainmaker.user_agent.should == 'Custom User Agent' + end + end + + describe ".configure" do + + RubyRainmaker::Configuration::VALID_OPTIONS_KEYS.each do |key| + + it "should set the #{key}" do + RubyRainmaker.configure do |config| + config.send("#{key}=", key) + RubyRainmaker.send(key).should == key + end + end + end + end +end diff --git a/test/helper.rb b/test/helper.rb deleted file mode 100644 index d0b7d4e..0000000 --- a/test/helper.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'rubygems' -require 'bundler' -begin - Bundler.setup(:default, :development) -rescue Bundler::BundlerError => e - $stderr.puts e.message - $stderr.puts "Run `bundle install` to install missing gems" - exit e.status_code -end -require 'test/unit' -require 'shoulda' - -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -$LOAD_PATH.unshift(File.dirname(__FILE__)) -require 'rainmaker' - -class Test::Unit::TestCase -end diff --git a/test/test_ruby-rainmaker.rb b/test/test_ruby-rainmaker.rb deleted file mode 100644 index e93c5bb..0000000 --- a/test/test_ruby-rainmaker.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'helper' - -class TestRubyRainmaker < Test::Unit::TestCase - should "probably rename this file and start testing for real" do - flunk "hey buddy, you should probably rename this file and start testing for real" - end -end