Skip to content

Commit

Permalink
Complete rewrite, removed rspec-api-documentation
Browse files Browse the repository at this point in the history
RSpec API has been rewritten from scratch, implementing the whole
DSL from `resource` to `request` and `respond_to`, in a way that is
compatible with the previous DSL but does not depend on rspec-api-
documentation.

This makes RSpec API more flexible in terms of what goes inside the
`it` blocks and how fixtures are created. Also, for the first time,
it supports not just local API (built with ActiveRecord and accessed
with RackTest), but remote API (accessed with Faraday)
  • Loading branch information
claudiob committed Oct 6, 2013
1 parent 41089f4 commit 4c7b6f8
Show file tree
Hide file tree
Showing 28 changed files with 609 additions and 323 deletions.
45 changes: 5 additions & 40 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,50 +1,15 @@
PATH
remote: .
specs:
rspec-api (0.0.3)
rspec-api-documentation
rspec-api (0.1.0)
faraday

GEM
remote: http://rubygems.org/
specs:
activesupport (4.0.0)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.5)
atomic (1.1.13)
crack (0.4.1)
safe_yaml (~> 0.9.0)
diff-lcs (1.2.4)
i18n (0.6.5)
json (1.8.0)
minitest (4.7.5)
multi_json (1.8.0)
mustache (0.99.4)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-api-documentation (1.1.1.alpha)
activesupport (>= 3.0.0)
i18n (>= 0.1.0)
json (>= 1.4.6)
mustache (>= 0.99.4)
rspec (>= 2.14.0)
webmock (>= 1.7.0)
rspec-core (2.14.5)
rspec-expectations (2.14.2)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.3)
safe_yaml (0.9.7)
thread_safe (0.1.2)
atomic
tzinfo (0.3.37)
webmock (1.13.0)
addressable (>= 2.2.7)
crack (>= 0.3.2)
faraday (0.8.8)
multipart-post (~> 1.2.0)
multipart-post (1.2.0)

PLATFORMS
ruby
Expand Down
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
v0.1.0 - 2013/10/04 Removed dependency from rspec-api-documentation

v0.0.3 - 2013/10/03 Add 'encoding: UTF-8' for Ruby < 2 compatibility

v0.0.2 - 2013/09/13 Helpers extracted from claudiob/gigs
11 changes: 11 additions & 0 deletions WHY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Why rspec and not...
--------------------

Because metadata are cool

Why faraday and not...
----------------------

Because it's case-insensitive to the headers keys
Because it returns a format very similar to rack-test
Also see http://lanyrd.com/2012/rubyconf/szpth/
61 changes: 0 additions & 61 deletions lib/rspec-api/accept_helper.rb

This file was deleted.

38 changes: 38 additions & 0 deletions lib/rspec-api/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

module DSL
module ActiveRecord
module Route
extend ActiveSupport::Concern

module ClassMethods
def setup_fixtures
setup_one_fixture
end

def setup_one_fixture
model = rspec_api[:resource_name].singularize.constantize
# TODO: Don't hard-code where, use the type and can_be_nil of attributes
before(:all) { @fixture = model.first_or_create! where: 'here' }
after(:all) { @fixture.destroy }
end

def existing(field)
model = rspec_api[:resource_name].singularize.constantize
-> { model.pluck(field).first }
end

def unknown(field)
model = rspec_api[:resource_name].singularize.constantize
keys = 0.downto(-Float::INFINITY).lazy
-> { keys.reject {|value| model.exists? field => value}.first }
end

def apply(method_name, options = {})
-> { options[:to].call.send method_name }
end
end
end
end
end

RSpec.configuration.include DSL::ActiveRecord::Route, rspec_api_dsl: :route
86 changes: 86 additions & 0 deletions lib/rspec-api/active_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'faraday'

module DSL
module ActiveResource
module Route
extend ActiveSupport::Concern

def send_request(verb, route, body)
conn = Faraday.new 'https://api.github.com/' do |c|
c.use Faraday::Response::Logger, Logger.new('log/faraday.log')
c.use Faraday::Adapter::NetHttp
end

conn.headers[:user_agent] = 'RSpec API for Github'
conn.authorization *authorization.flatten

@last_response = conn.send verb, route, (body.to_json if body.present?)
end

def authorization
# TODO: Any other way to access metadata in a before(:all) ?
self.class.metadata[:rspec_api][:authorization]
end

module ClassMethods

def setup_fixtures
# nothing to do for now...
end

def existing(field)
case field
when :user then 'claudiob'
when :gist_id then '0d7b597d822102148810'
when :id then '921225'
end
end

def unknown(field)
case field
when :user then 'not-a-valid-user'
when :gist_id then 'not-a-valid-gist-id'
when :id then 'not-a-valid-id'
end
end
end
end
end
end


module DSL
module ActiveResource
module Resource
extend ActiveSupport::Concern

module ClassMethods
def authorize_with(options = {})
rspec_api[:authorization] = options
end
end
end
end
end


module DSL
module ActiveResource
module Request
extend ActiveSupport::Concern

def response
@last_response
end

def request_params
debugger
1
end
end
end
end

RSpec.configuration.include DSL::ActiveResource::Resource, rspec_api_dsl: :resource
RSpec.configuration.include DSL::ActiveResource::Request, rspec_api_dsl: :request
RSpec.configuration.include DSL::ActiveResource::Route, rspec_api_dsl: :route
66 changes: 0 additions & 66 deletions lib/rspec-api/api_helper.rb

This file was deleted.

53 changes: 0 additions & 53 deletions lib/rspec-api/attributes_helper.rb

This file was deleted.

Loading

0 comments on commit 4c7b6f8

Please sign in to comment.