Skip to content

Commit

Permalink
Tidy up and a facebook API stubbing example.
Browse files Browse the repository at this point in the history
  • Loading branch information
oesmith committed Oct 11, 2012
1 parent ec7dd4e commit 103cdbe
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
log/test.log
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ GEM
remote: https://rubygems.org/
specs:
addressable (2.3.2)
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
capybara-webkit (0.12.1)
capybara (>= 1.0.0, < 1.2)
json
childprocess (0.3.5)
ffi (~> 1.0, >= 1.0.6)
cookiejar (0.3.0)
daemons (1.1.9)
diff-lcs (1.1.3)
Expand All @@ -27,9 +39,26 @@ GEM
eventmachine_httpserver (0.2.1)
faraday (0.8.4)
multipart-post (~> 1.1)
faye-websocket (0.4.6)
eventmachine (>= 0.12.0)
ffi (1.1.5)
http_parser.rb (0.5.3)
json (1.7.5)
libwebsocket (0.1.5)
addressable
mime-types (1.19)
multi_json (1.3.6)
multipart-post (1.1.5)
nokogiri (1.5.5)
poltergeist (0.7.0)
capybara (~> 1.1)
childprocess (~> 0.3)
faye-websocket (~> 0.4, >= 0.4.4)
http_parser.rb (~> 0.5.3)
multi_json (~> 1.0)
rack (1.4.1)
rack-test (0.6.2)
rack (>= 1.0)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
Expand All @@ -38,17 +67,30 @@ GEM
rspec-expectations (2.11.3)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.3)
rubyzip (0.9.9)
selenium-webdriver (2.25.0)
childprocess (>= 0.2.5)
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
rubyzip
thin (1.4.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
xpath (0.1.4)
nokogiri (~> 1.3)
yajl-ruby (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
capybara
capybara-webkit
faraday
poltergeist
puffing-billy!
rack
rspec
selenium-webdriver
thin
23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,11 @@ interactions with remote HTTP(S) servers.

The thirty second version:

```html
<ul id='counting'></ul>
```

```javascript
$.ajax('https://example.com/count/?callback=?', function (data) {
for (var name in data) {
$('#counting').append($('<li>' + name + '</li>'));
}
});
```

```ruby
it 'should count to five' do
# this is where the magic happens!
proxy.stub('https://example.com/count/').and_return(:jsonp => %w[one two three four five])

within('#counting') do
all('li').map(&:text).should == %w[one two three four five]
end
it 'should stub google' do
proxy.stub('http://www.google.com').and_return(:text => "I'm not Google!")
visit 'http://www.google.com'
page.should have_content("I'm not Google!")
end
```

Expand Down
59 changes: 59 additions & 0 deletions examples/facebook_api.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<div id="fb-root"></div>
<script>
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));

// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : '408416075843608', // App ID
//channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me){
if (me.name) {
document.getElementById('auth-displayname').innerHTML = me.name;
}
})
document.getElementById('auth-loggedout').style.display = 'none';
document.getElementById('auth-loggedin').style.display = 'block';
} else {
// user has not auth'd your app, or is not logged into Facebook
document.getElementById('auth-loggedout').style.display = 'block';
document.getElementById('auth-loggedin').style.display = 'none';
}
});

// respond to clicks on the login and logout links
document.getElementById('auth-loginlink').addEventListener('click', function(){
FB.login();
});
document.getElementById('auth-logoutlink').addEventListener('click', function(){
FB.logout();
});
}
</script>

<h1>Facebook Client-side Authentication Example</h1>
<div id="auth-status">
<div id="auth-loggedout">
<a href="#" id="auth-loginlink">Login</a>
</div>
<div id="auth-loggedin" style="display:none">
Hi, <span id="auth-displayname"></span>
(<a href="#" id="auth-logoutlink">logout</a>)
</div>
</div>
Empty file added log/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions puffing-billy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "rspec"
gem.add_development_dependency "thin"
gem.add_development_dependency "faraday"
gem.add_development_dependency "capybara"
gem.add_development_dependency "poltergeist"
gem.add_development_dependency "selenium-webdriver"
gem.add_development_dependency "capybara-webkit"
gem.add_development_dependency "rack"
gem.add_runtime_dependency "eventmachine"
gem.add_runtime_dependency "em-http-request"
gem.add_runtime_dependency "eventmachine_httpserver"
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/examples/facebook_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'
require 'base64'

describe 'Facebook API example', :type => :request, :js => true do
before do
proxy.stub('https://www.facebook.com:443/dialog/oauth').and_return(Proc.new { |params,_,_|
# mock a signed request from facebook. the JS api never verifies the
# signature, so all it needs is the base64-encoded payload
signed_request = "xxxxxxxxxx.#{Base64.encode64('{"user_id":"1234567"}')}"
# redirect to the 'redirect_uri', with some extra crap in the query
# string
{:redirect_to => "#{params['redirect_uri'][0]}&access_token=foobar&expires_in=600&base_domain=localhost&https=1&signed_request=#{signed_request}"}
})

proxy.stub('https://graph.facebook.com:443/me').and_return(:jsonp => {:name => 'Tester 1'})
end

it 'should show me as logged-in', :js => true do
visit '/facebook_api.html'
page.should have_content "Hi, Tester 1"
end
end

File renamed without changes.
13 changes: 12 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}

require 'capybara/poltergeist'
require 'billy/rspec'
require 'capybara/rspec'
require 'rack'
require 'logger'

Capybara.app = Rack::Directory.new(File.expand_path("../../examples", __FILE__))
Capybara.javascript_driver = :poltergeist_billy

Billy.configure do |config|
config.logger = Logger.new(File.expand_path("../../log/test.log", __FILE__))
end

RSpec.configure do |config|
include Billy::TestServer
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.order = 'random'

include Billy::TestServer
config.before :all do
start_test_servers
end
Expand Down

0 comments on commit 103cdbe

Please sign in to comment.