Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Rails 4.1 #57

Merged
merged 7 commits into from
Jun 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ spec/dummy/db/*.sqlite3
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
*.swp
*.un~
tags
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.2
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ApiTaster [![endorse](http://api.coderwall.com/fredwu/endorsecount.png)](http://coderwall.com/fredwu) [![Build Status](https://secure.travis-ci.org/fredwu/api_taster.png?branch=master)](http://travis-ci.org/fredwu/api_taster) [![Dependency Status](https://gemnasium.com/fredwu/api_taster.png)](https://gemnasium.com/fredwu/api_taster)

### NOTE
> If you want to use this gem with Rails 3x/4.0 please specify version 0.7.0 in
your Gemfile.

> Version 0.8 of this gem is compatible only with Rails 4.1.

A quick and easy way to visually test your Rails application's API.

![](http://i.imgur.com/8Dnto.png)
Expand Down Expand Up @@ -29,7 +35,7 @@ Rails.application.routes.draw do
end
```

In `routes.rb`, define parameters for each API endpoint after the normal routes definition block. For example:
In `lib/api_tasters/routes.rb`, define parameters for each API endpoint after the normal routes definition block. For example:

```ruby
if Rails.env.development?
Expand Down Expand Up @@ -60,10 +66,31 @@ if Rails.env.development?
end
```

You can change the default `lib/api_tasters/routes.rb` path by creating `config/initializers/api_taster.rb` with the content below:
```ruby
ApiTaster.route_path = Rails.root.to_s + "/app/api_tasters" # just an example
```

### Share Params with Test Factories

If you use a test factory such as [FactoryGirl](https://github.com/thoughtbot/factory_girl), you can require your test factories and share the params. For example in FactoryGirl you can use the `attributes_for(:name_of_factory)` method.

### Custom Headers

If there are certain headers (such as auth token) that need to be present to
consume an API endpoint, you may set then in `APITaster.global_headers` before
`APITaster.routes`:

```ruby
ApiTaster.global_headers = {
'Authorization' => 'Token token=teGpfbVitpnUwm7qStf9'
}

ApiTaster.routes do
# your route definitions
end
```

### Global Params

If there are certain params (such as API version and auth token) that need to be present in every API endpoint, you may set them in `ApiTaster.global_params` before `ApiTaster.routes`:
Expand Down
6 changes: 3 additions & 3 deletions api_taster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split($\)
s.test_files = Dir["spec/**/*"]

s.add_dependency 'rails', '~> 4.0.0'
s.add_dependency 'rails', '~> 4.1.0'
s.add_dependency 'jquery-rails'
s.add_dependency 'sass-rails'
s.add_dependency 'bootstrap-sass', '~> 2.1'
s.add_dependency 'sass-rails', '~> 4.0.3'
s.add_dependency 'bootstrap-sass', '~> 3.1.0.0'
s.add_dependency 'redcarpet'
s.add_dependency 'remotipart', '~> 1.0'

Expand Down
18 changes: 15 additions & 3 deletions app/assets/javascripts/api_taster/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var ApiTaster = {

detectContentType: function(response) {
var contentType = response.getResponseHeader("Content-Type");
var detectedContentType = null
var detectedContentType = null;

if (contentType.match(/application\/json/)) {
detectedContentType = 'json';
}
};

return detectedContentType;
},
Expand Down Expand Up @@ -55,7 +55,13 @@ var ApiTaster = {
}

return baseUrl;
}
},

setHeaders: function(headers) {
this.headers = headers;
},

headers: []

};

Expand Down Expand Up @@ -117,6 +123,12 @@ jQuery(function($) {
ApiTaster.disableUrlParams();

window.ajax = $.ajax({
beforeSend: function(xhr) {
var headers = ApiTaster.headers;
for(var l = headers.length, i = 0; i < l; i ++) {
xhr.setRequestHeader(headers[i].key, headers[i].value);
}
},
url: ApiTaster.getSubmitUrl($form),
type: $form.attr('method'),
data: $form.serialize()
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api_taster/routes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ApiTaster
class RoutesController < ApiTaster::ApplicationController
before_filter :map_routes
layout false, except: :index

def index
@routes = Route.grouped_routes
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/api_taster/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ def markdown(text)
markdown_renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown_renderer.render(text).html_safe
end

def headers_js_callback
render partial: "api_taster/routes/headers.js", locals: {headers: ApiTaster.global_headers}
end
end
end
10 changes: 10 additions & 0 deletions app/views/api_taster/routes/_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {
if(typeof ApiTaster !== 'undefined') {
ApiTaster.setHeaders(
<%= JSON(headers.collect {|header, value|
{key: header, value: value}
}).html_safe
%>
);
}
}).apply({});
6 changes: 5 additions & 1 deletion app/views/api_taster/routes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
:label_type => 'important'
%>

<script type="application/javascript">
<%= headers_js_callback %>
</script>

<% if @params.is_a?(Hash) && @params.has_key?(:undefined) %>
<%= render 'undefined_route', :route => @params[:undefined] %>
<% else %>
Expand All @@ -16,7 +20,7 @@
<% end %>

<% @params.each do |param| %>
<%= form_tag @route[:path], :method => @route[:verb], :class => 'well form-horizontal', :remote => true do %>
<%= form_tag @route[:full_path], :method => @route[:verb], :class => 'well form-horizontal', :remote => true do %>

<% if param[:url_params].empty? && param[:post_params].empty? %>
<div class="alert alert-info">
Expand Down
3 changes: 3 additions & 0 deletions lib/api_taster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module ApiTaster
mattr_accessor :route_path
self.route_path = "#{Rails.root}/lib/api_tasters"

mattr_accessor :global_headers
self.global_headers = {}

def self.routes(&block)
ApiTaster::RouteCollector.routes << block
end
Expand Down
5 changes: 3 additions & 2 deletions lib/api_taster/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ApiTaster
class FormBuilder < AbstractController::Base
include AbstractController::Rendering
include ActionView::Layouts
include ActionView::Context
include ActionView::Helpers::CaptureHelper

Expand Down Expand Up @@ -48,7 +49,7 @@ def add_to_buffer(params, parent_labels = [])

def add_element_to_buffer(parent_labels, label, value)
@_buffer += render(
:partial => 'api_taster/routes/param_form_element',
:template => 'api_taster/routes/_param_form_element',
:locals => {
:label => "#{print_labels(parent_labels)}#{label}",
:label_text => label,
Expand All @@ -59,7 +60,7 @@ def add_element_to_buffer(parent_labels, label, value)

def add_legend_to_buffer(parent_labels, label)
@_buffer += render(
:partial => 'api_taster/routes/param_form_legend',
:template => 'api_taster/routes/_param_form_legend',
:locals => { :label => print_labels(parent_labels.clone << label) }
)
end
Expand Down
16 changes: 11 additions & 5 deletions lib/api_taster/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ def discover_rack_app(app)

def normalise_route(route, path_prefix = nil)
route.verb.source.split('|').map do |verb|
path = path_prefix.to_s + route.path.spec.to_s.sub('(.:format)', '')
{
:id => @_route_counter+=1,
:name => route.name,
:verb => verb.gsub(/[$^]/, ''),
:path => path_prefix.to_s + route.path.spec.to_s.sub('(.:format)', ''),
:reqs => route.requirements
:id => @_route_counter+=1,
:name => route.name,
:verb => verb.gsub(/[$^]/, ''),
:path => path,
:full_path => rails_url_root + path,
:reqs => route.requirements
}
end
end
Expand All @@ -143,6 +145,10 @@ def hash_diff(h1, h2)
h2[k] == v
end.merge!(h2.dup.delete_if { |k, v| h1.has_key?(k) })
end

def rails_url_root
ActionController::Base.relative_url_root.to_s.chomp('/')
end
end
end
end
2 changes: 1 addition & 1 deletion lib/api_taster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ApiTaster
VERSION = "0.7.0"
VERSION = "0.8.1"
end
5 changes: 3 additions & 2 deletions spec/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ module ApiTaster
:name => 'home',
:verb => 'GET',
:path => '/home',
:full_path => '/home',
:reqs => {
:controller => 'application',
:action => 'home'
:action => 'home',
:controller => 'application'
}
}
end
Expand Down