Skip to content

Commit

Permalink
Test integration into Rails controller
Browse files Browse the repository at this point in the history
  • Loading branch information
iain committed Jul 13, 2012
1 parent 1ef339a commit a786d80
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 22 deletions.
45 changes: 33 additions & 12 deletions README.md
Expand Up @@ -2,6 +2,8 @@

A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.

Since version 2.0, this gem is Rack middleware.

## Features

* Splits the http-header into languages specified by the user
Expand All @@ -15,30 +17,56 @@ See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

## Example

When using in Rails:

``` ruby
class SomeController < ApplicationController

def some_action

request.user_preferred_languages # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
http_accept_language.user_preferred_languages # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
available = %w{en en-US nl-BE}
request.preferred_language_from(available) # => 'nl-BE'
http_accept_language.preferred_language_from(available) # => 'nl-BE'

request.user_preferred_languages # => [ 'en-GB']
http_accept_language.user_preferred_languages # => [ 'en-GB']
available = %w{en-US}
request.compatible_language_from(available) # => 'en-US'
http_accept_language.compatible_language_from(available) # => 'en-US'

end

end
```

Older versions of Rails (pre 3.0) might need to include the middleware manually.

Usage in any Rack application, simple add the middleware:

``` ruby
require 'http_accept_language'
use HttpAcceptLanguage::Middleware
run YourAwesomeApp
```

Then you can access it:

``` ruby
class YourAwesomeApp

def self.call(env)
available = %w(en en-US nl-BE)
language = env.http_accept_language.preferred_language_from(available)
[ 200, {}, ["Oh, you speak #{language}!"]]
end

end
```

## Installation


### Without Bundler

Install the gem `http_accept_language`, require it in your Rails app.
Install the gem `http_accept_language`

### With Bundler

Expand All @@ -50,13 +78,6 @@ gem 'http_accept_language'

Run `bundle install` to install it.

## Changelog

* 2012-07-13: Sanitization for available locales and general clean up
* 2011-09-11: 1.0.2 release, still works appearantly
* 2010-01-05: Gem release
* 2009-03-12: Rails 2.3 compatible

---

Released under the MIT license
19 changes: 18 additions & 1 deletion features/rails_integration.feature
@@ -1,3 +1,4 @@
@rails
Feature: Rails Integration

To use http_accept_language inside a Rails application, just add it to your
Expand All @@ -8,5 +9,21 @@ Feature: Rails Integration
Scenario: Installing
When I generate a new Rails app
And I add http_accept_language to my Gemfile
And I run `bundle exec rake middleware`
And I run `rake middleware`
Then the output should contain "use HttpAcceptLanguage::Middleware"

Scenario: Using
Given I have installed http_accept_language
When I generate the following controller:
"""
class LanguagesController < ApplicationController
def index
languages = http_accept_language.user_preferred_languages
render :text => "Languages: #{languages.join(' : ')}"
end
end
"""
When I access that action with the HTTP_ACCEPT_LANGUAGE header "en-us,en-gb;q=0.8,en;q=0.6,es-419"
Then the response should contain "Languages: en-US : es-419 : en-GB : en"
40 changes: 32 additions & 8 deletions features/steps/rails.rb
@@ -1,13 +1,37 @@
Before "@rails" do
@rails = RailsDriver.new
end

When /^I generate a new Rails app$/ do
@aruba_io_wait_seconds = 10
app_name = "foobar"
# install rails with as few things as possible, for speed!
run_simple "bundle exec rails new #{app_name} --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit --old-style-hash"
cd app_name
@rails.generate_rails
end

When /^I add http_accept_language to my Gemfile$/ do
# Specifiy a path so cucumber will use the unreleased version of the gem
path = File.expand_path('../../../', __FILE__)
append_to_file "Gemfile", "gem 'http_accept_language', :path => '#{path}'"
@rails.append_gemfile
end

Given /^I have installed http_accept_language$/ do
@rails.install_gem
end

When /^I generate the following controller:$/ do |string|
@rails.generate_controller "languages", string
end

When /^I access that action with the HTTP_ACCEPT_LANGUAGE header "(.*?)"$/ do |header|
@rails.with_rails_running do
@rails.request_with_http_accept_language_header(header, "/languages")
end
end

Then /^the response should contain "(.*?)"$/ do |output|
@rails.output_should_contain(output)
end

When /^I run `rake middleware`$/ do
@rails.bundle_exec("rake middleware")
end

Then /^the output should contain "(.*?)"$/ do |expected|
@rails.assert_partial_output(expected, @rails.all_output)
end
1 change: 0 additions & 1 deletion features/support/aruba.rb

This file was deleted.

93 changes: 93 additions & 0 deletions features/support/rails_driver.rb
@@ -0,0 +1,93 @@
require 'aruba/api'

class RailsDriver
include Aruba::Api

def initialize
@aruba_io_wait_seconds = 10
# @announce_stdout = true
# @announce_stderr = true
# @announce_cmd = true
# @announce_dir = true
# @announce_env = true
end

def app_name
"foobar"
end

def install_gem
if app_exists?
cd app_name
else
generate_rails
append_gemfile
end
end

def app_exists?
in_current_dir do
File.exist?("#{app_name}/Gemfile")
end
end

def bundle_exec(cmd)
run_simple "bundle exec #{cmd}"
end

def generate_rails
# install rails with as few things as possible, for speed!
bundle_exec "rails new #{app_name} --force --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit --old-style-hash"
cd app_name
end

def append_gemfile
# Specifiy a path so cucumber will use the unreleased version of the gem
append_to_file "Gemfile", "gem 'http_accept_language', :path => '#{gem_path}'"
end

def gem_path
File.expand_path('../../../', __FILE__)
end

def generate_controller(name, content)
bundle_exec "rails generate resource #{name} --force"
write_file "app/controllers/#{name}_controller.rb", content
end

def request_with_http_accept_language_header(header, path)
run_simple "curl --retry 10 -H 'Accept-language: #{header}' #{File.join(host, path)} -o #{response}"
run_simple "cat out.html"
end

def host
"http://localhost:13000"
end

def with_rails_running
start_rails
yield
ensure
stop_rails
end

def start_rails
bundle_exec "rails server -p 13000 -d"
end

def stop_rails
in_current_dir do
`cat tmp/pids/server.pid | xargs kill -9`
end
end

def response
File.expand_path(File.join(current_dir, 'out.html'))
end

def output_should_contain(expected)
actual = File.open(response, 'r:utf-8').read
actual.should include expected
end

end
8 changes: 8 additions & 0 deletions lib/http_accept_language/railtie.rb
Expand Up @@ -3,7 +3,15 @@ class Railtie < ::Rails::Railtie

initializer "http_accept_language.add_middleware" do |app|
app.middleware.use Middleware
ApplicationController.send :include, EasyAccess
end

end

module EasyAccess
def http_accept_language
env.http_accept_language
end
end

end

0 comments on commit a786d80

Please sign in to comment.