Skip to content

Commit

Permalink
Look for the api key in either the LOCALEAPP_API_KEY env variable or …
Browse files Browse the repository at this point in the history
…try and read it from a .env file
  • Loading branch information
tigrish committed Nov 22, 2012
1 parent 85a2f46 commit 21a91ae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bin/localeapp
Expand Up @@ -24,6 +24,16 @@ else
end

pre do |global_options, command, options, args|
global_options[:k] = if global_options[:k]
global_options[:k]
elsif ENV['LOCALEAPP_API_KEY']
ENV['LOCALEAPP_API_KEY']
elsif File.exist?('.env') && IO.read('.env') =~ /^LOCALEAPP_API_KEY=(\w+)$/
$1
else
nil
end

if Localeapp.has_config_file? || !global_options[:k].nil?
true
else
Expand Down
27 changes: 27 additions & 0 deletions features/add.feature
Expand Up @@ -44,3 +44,30 @@ Feature: Adding a translation from the command line
Sending key: foo.baz
Success!
"""

Scenario: Running add with no initializer file, passing the key via an ENV variable
In order to add a key and translation content
When I have a valid project on localeapp.com with api key "MYAPIKEY"
When I have a LOCALEAPP_API_KEY env variable set to "MYAPIKEY"
When I run `localeapp add foo.baz en:"test en content"`
Then the output should contain:
"""
Localeapp Add
Sending key: foo.baz
Success!
"""
Then I clear the LOCALEAPP_API_KEY env variable

Scenario: Running add with no initializer file, passing the key via a .env file
In order to add a key and translation content
When I have a valid project on localeapp.com with api key "MYAPIKEY"
When I have a .env file containing the api key "MYAPIKEY"
When I run `localeapp add foo.baz en:"test en content"`
Then the output should contain:
"""
Localeapp Add
Sending key: foo.baz
Success!
"""
17 changes: 17 additions & 0 deletions features/step_definitions/cli_steps.rb
Expand Up @@ -41,6 +41,23 @@
add_fake_web_uri(:post, uri + '/rename', ['200', 'OK'], '')
end

When /^I have a LOCALEAPP_API_KEY env variable set to "(.*?)"$/ do |api_key|
ENV['LOCALEAPP_API_KEY'] = api_key
end

Then /^I clear the LOCALEAPP_API_KEY env variable$/ do
ENV['LOCALEAPP_API_KEY'] = nil
end

When /^I have a \.env file containing the api key "(.*?)"$/ do |api_key|
steps %Q{
And a file named ".env" with:
"""
LOCALEAPP_API_KEY=#{api_key}
"""
}
end

When /^an initializer file$/ do
steps %Q{
And a file named "config/initializers/localeapp.rb" with:
Expand Down

0 comments on commit 21a91ae

Please sign in to comment.