-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete rewrite, removed rspec-api-documentation
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
Showing
28 changed files
with
609 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.