Skip to content

Commit

Permalink
Use draft blurbs in development
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Nov 17, 2010
1 parent 0cd076e commit ecc7f65
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 53 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -14,4 +14,5 @@ gem "yard", :require => false
gem "aruba"
gem "ruby-debug"
gem "json"
gem "thin"

7 changes: 7 additions & 0 deletions Gemfile.lock
Expand Up @@ -24,7 +24,9 @@ GEM
gherkin (~> 2.1.4)
json_pure (~> 1.4.3)
term-ansicolor (~> 1.0.4)
daemons (1.1.0)
diff-lcs (1.1.2)
eventmachine (0.12.10)
gherkin (2.1.5)
trollop (~> 1.16.2)
httparty (0.6.1)
Expand Down Expand Up @@ -54,6 +56,10 @@ GEM
rack (>= 1.0)
sqlite3-ruby (1.3.1)
term-ansicolor (1.0.5)
thin (1.2.7)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
trollop (1.16.2)
webmock (1.3.5)
addressable (>= 2.1.1)
Expand All @@ -76,5 +82,6 @@ DEPENDENCIES
sham_rack
sinatra
sqlite3-ruby
thin
webmock
yard
44 changes: 36 additions & 8 deletions features/rails.feature
Expand Up @@ -7,8 +7,8 @@ Feature: Using copycopter in a rails app

Scenario: copycopter in the controller
Given the "abc123" project has the following blurbs:
| key | published content |
| en.users.index.controller-test | This is a test |
| key | draft content |
| en.users.index.controller-test | This is a test |
When I write to "app/controllers/users_controller.rb" with:
"""
class UsersController < ActionController::Base
Expand All @@ -34,8 +34,8 @@ Feature: Using copycopter in a rails app

Scenario: copycopter in the view
Given the "abc123" project has the following blurbs:
| key | published content |
| en.users.index.view-test | This is a test |
| key | draft content |
| en.users.index.view-test | This is a test |
When I write to "app/controllers/users_controller.rb" with:
"""
class UsersController < ActionController::Base
Expand All @@ -61,8 +61,8 @@ Feature: Using copycopter in a rails app

Scenario: copycopter detects updates to copy
Given the "abc123" project has the following blurbs:
| key | published content |
| en.users.index.controller-test | Old content |
| key | draft content |
| en.users.index.controller-test | Old content |
When I write to "app/controllers/users_controller.rb" with:
"""
class UsersController < ActionController::Base
Expand All @@ -86,8 +86,8 @@ Feature: Using copycopter in a rails app
And I visit /users/
Then the output should contain "Old content"
When the the following blurbs are updated in the "abc123" project:
| key | published content |
| en.users.index.controller-test | New content |
| key | draft content |
| en.users.index.controller-test | New content |
And I wait for changes to be synchronized
And I visit /users/
Then the output should contain "New content"
Expand Down Expand Up @@ -119,3 +119,31 @@ Feature: Using copycopter in a rails app
| key | draft content |
| en.users.index.404 | not found |

Scenario: copycopter in development
Given the "abc123" project has the following blurbs:
| key | published content | draft content |
| en.users.index.controller-test | This is a test | Extra extra |
When I write to "app/controllers/users_controller.rb" with:
"""
class UsersController < ActionController::Base
def index
@text = t("users.index.controller-test", :default => "default")
end
end
"""
When I write to "config/routes.rb" with:
"""
ActionController::Routing::Routes.draw do |map|
map.resources :users
end
"""
When I write to "app/views/users/index.html.erb" with:
"""
<%= @text %>
"""
When I configure the copycopter client to used published data
And I start the application
And I wait for changes to be synchronized
And I visit /users/
Then the output should contain "This is a test"

15 changes: 5 additions & 10 deletions features/step_definitions/copycopter_server_steps.rb
Expand Up @@ -5,17 +5,12 @@
end

Given /^the "([^"]*)" project has the following blurbs:$/ do |api_key, table|
project = FakeCopycopterApp.projects[api_key]
project = FakeCopycopterApp.project(api_key)
table.hashes.each do |blurb_hash|
key = blurb_hash['key']

if blurb_hash['draft content']
project.draft[key] = blurb_hash['draft content']
end

if blurb_hash['published content']
project.published[key] = blurb_hash['published content']
end
data = { 'draft' => { key => blurb_hash['draft content'] },
'published' => { key => blurb_hash['published content'] } }
project.update(data)
end
end

Expand All @@ -24,7 +19,7 @@
end

Then /^the "([^"]*)" project should have the following blurbs:$/ do |api_key, table|
project = FakeCopycopterApp.projects[api_key]
project = FakeCopycopterApp.project(api_key)
table.hashes.each do |blurb_hash|
key = blurb_hash['key']

Expand Down
19 changes: 12 additions & 7 deletions features/step_definitions/rails_steps.rb
Expand Up @@ -20,18 +20,23 @@

When "I start the application" do
in_current_dir do
require 'config/environment'
RailsServer.start(ENV['RAILS_PORT'])
end
end

When "I visit /$path" do |path|
app = ActionController::Dispatcher.new
request = Rack::MockRequest.new(app)
response = request.get(path)
@last_stdout = response.body
@last_stdout = RailsServer.get(path)
end

if defined?(ActionController) && ActionController::Reloader.default_lock
ActionController::Reloader.default_lock.unlock
When /^I configure the copycopter client to used published data$/ do
in_current_dir do
config_path = "config/initializers/copycopter.rb"
contents = IO.read(config_path)
contents.sub!("end", " config.development_environments = []\nend")
File.open(config_path, "w") { |file| file.write(contents) }
end
end

After do
RailsServer.stop
end
42 changes: 42 additions & 0 deletions features/support/rails_server.rb
@@ -0,0 +1,42 @@
class RailsServer
class << self
attr_accessor :instance
end

def self.start(port = nil)
self.instance = new(port)
end

def self.stop
self.instance.stop if instance
self.instance = nil
end

def self.get(path)
self.instance.get(path)
end

def initialize(port)
@port = (port || 3001).to_i
@output = StringIO.new("")
@pid = fork do
$stdout = self.output
$stderr = self.output
require 'config/environment'
app = ActionController::Dispatcher.new
Rack::Handler::Thin.run(app, :Port => @port, :AccessLog => [])
end
sleep(5) # wait for app server to start
end

attr_accessor :output

def stop
Process.kill('INT', @pid)
Process.wait(@pid)
end

def get(path)
Net::HTTP.get(URI.parse("http://localhost:#{@port}").merge(path))
end
end
16 changes: 14 additions & 2 deletions lib/copycopter_client/client.rb
Expand Up @@ -2,14 +2,14 @@ module CopycopterClient
# Communicates with the Copycopter server
class Client
def initialize(options)
[:protocol, :api_key, :host, :port].each do |option|
[:protocol, :api_key, :host, :port, :public].each do |option|
instance_variable_set("@#{option}", options[option])
end
end

def download
connect do |http|
response = http.request_get(uri("published_blurbs"))
response = http.request_get(uri(download_resource))
JSON.parse(response.body)
end
end
Expand All @@ -24,10 +24,22 @@ def upload(data)

attr_reader :protocol, :host, :port, :api_key

def public?
@public
end

def uri(resource)
"/api/v2/projects/#{api_key}/#{resource}"
end

def download_resource
if public?
"published_blurbs"
else
"draft_blurbs"
end
end

def connect
result = nil
Net::HTTP.start(host, port) do |http|
Expand Down
3 changes: 2 additions & 1 deletion lib/copycopter_client/configuration.rb
Expand Up @@ -96,7 +96,8 @@ def [](option)

# Returns a hash of all configurable options
def to_hash
OPTIONS.inject({}) do |hash, option|
base_options = { :public => public? }
OPTIONS.inject(base_options) do |hash, option|
hash.merge(option.to_sym => send(option))
end
end
Expand Down
37 changes: 32 additions & 5 deletions spec/copycopter_client/client_spec.rb
Expand Up @@ -20,10 +20,16 @@ def add_project

it "downloads published blurbs for an existing project" do
project = add_project
project.draft['key.one'] = "unexpected one"
project.draft['key.three'] = "unexpected three"
project.published['key.one'] = "expected one"
project.published['key.two'] = "expected two"
project.update({
'draft' => {
'key.one' => "unexpected one",
'key.three' => "unexpected three"
},
'published' => {
'key.one' => "expected one",
'key.two' => "expected two"
}
})

blurbs = build_client(:api_key => project.api_key, :public => true).download

Expand All @@ -33,6 +39,27 @@ def add_project
}
end

it "downloads draft blurbs for an existing project" do
project = add_project
project.update({
'draft' => {
'key.one' => "expected one",
'key.two' => "expected two"
},
'published' => {
'key.one' => "unexpected one",
'key.three' => "unexpected three"
}
})

blurbs = build_client(:api_key => project.api_key, :public => false).download

blurbs.should == {
'key.one' => 'expected one',
'key.two' => 'expected two'
}
end

it "uploads defaults for missing blurbs in an existing project" do
project = add_project

Expand All @@ -44,7 +71,7 @@ def add_project
client = build_client(:api_key => project.api_key, :public => true)
client.upload(blurbs)

project.draft.should == blurbs
project.reload.draft.should == blurbs
end
end

1 change: 1 addition & 0 deletions spec/copycopter_client/configuration_spec.rb
Expand Up @@ -75,6 +75,7 @@
:proxy_user, :secure, :development_environments].each do |option|
hash[option].should == config[option]
end
hash[:public].should == config.public?
end

it "should be mergable" do
Expand Down

0 comments on commit ecc7f65

Please sign in to comment.