diff --git a/.gitignore b/.gitignore index 22fe389..43e1500 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ spec/dummy/db/*.sqlite3 spec/dummy/log/*.log spec/dummy/tmp/ spec/dummy/.sass-cache +*.swp +*.un~ +tags diff --git a/README.md b/README.md index 170bfeb..9932319 100644 --- a/README.md +++ b/README.md @@ -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) @@ -69,6 +75,22 @@ ApiTaster.route_path = Rails.root.to_s + "/app/api_tasters" # just an example 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`: diff --git a/app/assets/javascripts/api_taster/app.js b/app/assets/javascripts/api_taster/app.js index 1abd534..292309f 100644 --- a/app/assets/javascripts/api_taster/app.js +++ b/app/assets/javascripts/api_taster/app.js @@ -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; }, @@ -55,7 +55,13 @@ var ApiTaster = { } return baseUrl; - } + }, + + setHeaders: function(headers) { + this.headers = headers; + }, + + headers: [] }; @@ -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() diff --git a/app/helpers/api_taster/application_helper.rb b/app/helpers/api_taster/application_helper.rb index 194c161..b85118f 100644 --- a/app/helpers/api_taster/application_helper.rb +++ b/app/helpers/api_taster/application_helper.rb @@ -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 diff --git a/app/views/api_taster/routes/_headers.js b/app/views/api_taster/routes/_headers.js new file mode 100644 index 0000000..e657073 --- /dev/null +++ b/app/views/api_taster/routes/_headers.js @@ -0,0 +1,10 @@ +(function() { + if(typeof ApiTaster !== 'undefined') { + ApiTaster.setHeaders( + <%= JSON(headers.collect {|header, value| + {key: header, value: value} + }).html_safe + %> + ); + } +}).apply({}); diff --git a/app/views/api_taster/routes/show.html.erb b/app/views/api_taster/routes/show.html.erb index eeab34f..24550e7 100644 --- a/app/views/api_taster/routes/show.html.erb +++ b/app/views/api_taster/routes/show.html.erb @@ -5,6 +5,10 @@ :label_type => 'important' %> + + <% if @params.is_a?(Hash) && @params.has_key?(:undefined) %> <%= render 'undefined_route', :route => @params[:undefined] %> <% else %> diff --git a/lib/api_taster/version.rb b/lib/api_taster/version.rb index a7cb31f..ec73bd3 100644 --- a/lib/api_taster/version.rb +++ b/lib/api_taster/version.rb @@ -1,3 +1,3 @@ module ApiTaster - VERSION = "0.8.0" + VERSION = "0.8.1" end