Permalink
Browse files

gabe got tests working. working out events

  • Loading branch information...
1 parent 2773bcb commit add135a29f0ebad4577ac7fb8c822c775fb7ff7c @jwoertink jwoertink committed Nov 16, 2012
View
1 .rspec
@@ -0,0 +1 @@
+--color --format nested
View
@@ -1,2 +1,16 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+require 'rake'
+require 'rspec/core/rake_task'
+
+namespace :spec do
+ RSpec::Core::RakeTask.new(:normal) do |t|
+ t.pattern ='spec/**/*_spec.rb'
+ t.rcov = false
+ end
+end
+
+desc 'RSpec tests'
+task 'spec' => 'spec:normal'
+
+task 'default' => 'spec'
View
@@ -17,6 +17,8 @@ Gem::Specification.new do |gem|
gem.add_dependency 'faraday', '< 0.9.0'
gem.add_dependency 'faraday_middleware', '0.9.0'
+ gem.add_dependency 'nokogiri', '~> 1.5.5'
+ gem.add_dependency 'multi_xml', '~> 0.5'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'vcr'
View
@@ -1,5 +1,15 @@
-require "eventful/version"
+require 'eventful/version'
module Eventful
- # Your code goes here...
+ ENDPOINT = 'http://api.eventful.com/rest/'
+
+ class << self
+ attr_accessor :api_key
+
+ end
+
end
+
+require 'eventful/request'
+require 'eventful/response'
+require 'eventful/event'
View
@@ -0,0 +1,18 @@
+module Eventful
+
+ class Event
+
+ class << self
+
+ def search(options = {})
+ [].tap do |c|
+ c.extend Response
+ c << new
+ end
+ end
+
+ end
+
+ end
+
+end
View
@@ -0,0 +1,100 @@
+require 'faraday_middleware'
+
+module Eventful
+ ##
+ # Used by {Client} to execute HTTP requests against APIs.
+ module Request
+ FARADAY_OPTIONS = {
+ :headers => {
+ 'Accept' => 'text/xml, application/xml; charset=utf-8',
+ 'User-Agent' => -> { 'eventful-ruby/%s (Rubygems; Ruby %s %s)' % [Eventful::VERSION, RUBY_VERSION, RUBY_PLATFORM] }.call
+ },
+ :url => ::Eventful::ENDPOINT,
+ :request => {
+ :timeout => 5,
+ :open_timeout => 5
+ }
+ }
+
+ ##
+ # Builds and executes a `GET` request to the specified path with the
+ # provided options.
+ #
+ # @param [String] path
+ # @param [Hash] options
+ # @return [Faraday::Response]
+ def get(path = nil, options = {})
+ connection.get(path, options)
+ end
+
+ ##
+ # Builds and executes a `POST` request to the specified path with the
+ # provided options.
+ #
+ # @param [String] path
+ # @param [Hash] options
+ # @return [Faraday::Response]
+ def post(path = nil, options = {})
+ connection.post(path, options)
+ end
+
+ ##
+ # Builds and executes a `PUT` request to the specified path with the
+ # provided options.
+ #
+ # @param [String] path
+ # @param [Hash] options
+ # @return [Faraday::Response]
+ def put(path = nil, options = {})
+ connection.put(path, options)
+ end
+
+ ##
+ # Builds and executes a `DELETE` request to the specified path with the
+ # provided options.
+ #
+ # @param [String] path
+ # @param [Hash] options
+ # @return [Faraday::Response]
+ def delete(path = nil, options = {})
+ connection.delete(path, options)
+ end
+
+ ##
+ # Builds and returns a `Faraday::Connection` for executing HTTP requests.
+ #
+ # @return [Faraday::Connection]
+ def connection
+ @connection ||= Faraday::Connection.new(FARADAY_OPTIONS) do |c|
+ c.response :xml, :content_type => /\bxml$/
+ c.adapter(Faraday.default_adapter)
+ end
+ end
+
+ ##
+ # Returns a formatted string to be used as the user agent when making
+ # requests.
+ #
+ # @return [String]
+ # A user agent describing the environment
+ #
+ # @example
+ # client.user_agent
+ # # => "eventful-ruby/0.1.0 (Rubygems; Ruby 1.9.3 x86_64-darwin11.4.0)"
+ def user_agent
+ 'eventful-ruby/%s (Rubygems; Ruby %s %s)' % [Eventful::VERSION, RUBY_VERSION, RUBY_PLATFORM]
+ end
+
+ ##
+ # Returns the provided object and attaches the response, additionally
+ # specifying whether the request returned a successful response.
+ def respond_with(object, response = nil, options = {}, &block)
+ object.tap do |o|
+ o.extend(Response)
+ o.raw_response = response
+ o.success = options.has_key?(:success) ? options[:success] : true
+ yield(o) if block_given?
+ end
+ end
+ end
+end
View
@@ -0,0 +1,10 @@
+module Eventful
+ module Response
+ attr_accessor :raw_response
+ attr_writer :success
+
+ def success?
+ !!@success
+ end
+ end
+end
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe Eventful::Event do
+
+ describe '.search' do
+ context 'when coordinates are provided' do
+ subject { Eventful::Event.search(latitude: '123', longitude: '-123') }
+
+ it 'returns an array of events' do
+ subject.first.should be_kind_of(Eventful::Event)
+ end
+
+ # its(:response) { should be_success }
+ end
+ end
+
+end
@@ -0,0 +1,89 @@
+require 'spec_helper'
+
+describe Eventful::Request do
+ let(:http) do
+ Class.new do
+ include Eventful::Request
+ end
+ end
+
+ let(:url) { %r{^http://api.eventful.com/rest} }
+
+ let(:client) { http.new }
+
+ subject { client }
+
+ after(:each) do
+ WebMock.reset!
+ end
+
+ context '#get' do
+ subject { client.get }
+
+ before(:each) do
+ WebMock.stub_request(:any, /.*/)
+ end
+
+ it 'should execute a GET request' do
+ subject
+ WebMock.should have_requested(:get, url)
+ end
+ end
+
+ context '#post' do
+ subject { client.post(nil, 'something') }
+
+ before(:each) do
+ WebMock.stub_request(:any, url)
+ end
+
+ it 'should execute a POST request with the given data' do
+ subject
+ WebMock.should have_requested(:post, url).with(:body => 'something')
+ end
+ end
+
+ context '#put' do
+ subject { client.put(nil, 'something') }
+
+ before(:each) do
+ WebMock.stub_request(:any, url)
+ end
+
+ it 'should execute a PUT request with the given data' do
+ subject
+ WebMock.should have_requested(:put, url).with(:body => 'something')
+ end
+ end
+
+ context '#delete' do
+ subject { client.delete }
+
+ before(:each) do
+ WebMock.stub_request(:any, url)
+ end
+
+ it 'should execute a DELETE request' do
+ subject
+ WebMock.should have_requested(:delete, url)
+ end
+ end
+
+ context '#connection' do
+ it 'should accept XML responses' do
+ Faraday::Connection.should_receive(:new).with(hash_including(:headers => hash_including('Accept' => 'text/xml, application/xml; charset=utf-8')))
+ subject.connection
+ end
+
+ it 'should set the user agent' do
+ Faraday::Connection.should_receive(:new).with(hash_including(:headers => hash_including('User-Agent' => subject.user_agent)))
+ subject.connection
+ end
+ end
+
+ context '#user_agent' do
+ it 'should build a user agent containing the release, Ruby, and platform versions' do
+ subject.user_agent.should =~ %r{^eventful-ruby/[0-9\.]+ \(Rubygems; Ruby [0-9\.]+ .+\)$}
+ end
+ end
+end
View
@@ -0,0 +1,12 @@
+require 'spec_helper'
+
+describe Eventful do
+
+ describe '.api_key' do
+ it 'is accessible' do
+ subject.api_key = 'foo'
+ subject.api_key.should == 'foo'
+ end
+ end
+
+end
View
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development, :test)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+
+require 'rspec/autorun'
+require 'webmock/rspec'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'eventful-ruby'
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

0 comments on commit add135a

Please sign in to comment.