Skip to content

Commit

Permalink
experimenting: try out parallel ad requests
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis committed Feb 26, 2010
1 parent f4606fe commit f9987bf
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 51 deletions.
56 changes: 51 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
= 140Proof API Wrapper

A Ruby wrapper around the 140Proof API. Documentation can be found here - http://developers.140proof.com/docs
This is a branch experimenting with using Typhoeus (http://github.com/pauldix/typhoeus) to call multiple ads in parallel.

It currently only supports JSON.
Documentation for the 140Proof API can be found here - http://developers.140proof.com/docs

== Install

gem sources -a http://gemcutter.org
sudo gem install one40_proof
git clone git@github.com:reddavis/One40Proof.git
git checkout -b multi origin/multi
rake install

== How To Use

<b>Making Parallel Requests</b>

require 'rubygems'
require 'one40_proof/multi'

# Queries are created using a hash and then placed in an array
queries = []

# One40Proof's "Test" method
queries << {:method => :test}

# One40Proof's "user" method
queries << {:method => :user, :user_id => 'sferik', :app_id => 'test'}

# One40Proof's "search" method
queries << {:method => :search, :user_id => 'sferik', :app_id => 'test', :q => 'New York Mets'}

# We then initialize the calls to the service
a = One40Proof::Multi::Base.new(queries)

# Then we have access to all our ads
a.ads.each do |ad|
# The Ad
ad.image_url
ad.byline
ad.text

# User
ad.user.screen_name
ad.user.user_id
ad.user.profile_image_url
ad.user.name

# Action URLS
ad.action_urls.click_url
ad.action_urls.favorite_url # Or ad.action_urls.favourite_url for the English
ad.action_urls.impression_url
ad.action_urls.friendship_url
ad.action_urls.reply_url
ad.action_urls.retweet_url
end


<b>Making Single Requests</b>

Testing ad placement while in development

require 'rubygems'
require 'one40_proof'
require 'one40_proof/simple'

ad = One40Proof::Test.new

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
spec.spec_files = FileList['spec/**/*_spec.rb'].uniq
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
Expand Down
2 changes: 1 addition & 1 deletion lib/one40_proof/attributes/action_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module One40Proof
class ActionUrls

def initialize(data)
@data = data['ads'][0]['action_urls']
@data = data
end

def click_url
Expand Down
32 changes: 32 additions & 0 deletions lib/one40_proof/attributes/ad.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'attributes/user'
require 'attributes/action_urls'

module One40Proof
class Ad

def initialize(data)
@data = data['ads'][0]
end

def image_url
@data['image_url']
end

def byline
@data['byline']
end

def text
@data['text']
end

def user
@user ||= User.new(@data['user'])
end

def action_urls
@action_urls ||= ActionUrls.new(@data['action_urls'])
end

end
end
2 changes: 1 addition & 1 deletion lib/one40_proof/attributes/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module One40Proof
class User

def initialize(data)
@data = data['ads'][0]['user']
@data = data
end

def profile_image_url
Expand Down
3 changes: 3 additions & 0 deletions lib/one40_proof/multi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$:.unshift(File.dirname(__FILE__))

require 'multi/base'
64 changes: 64 additions & 0 deletions lib/one40_proof/multi/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'typhoeus'
require 'json'
require 'attributes/ad'

module One40Proof
module Multi
class Base

def initialize(ad_requests)
hydra = Typhoeus::Hydra.new

ad_requests.each do |ad_request|
url = build_url(ad_request.delete(:method))

params = ad_request.empty? ? {} : {:params => turn_keys_to_strings(ad_request)}
request = Typhoeus::Request.new(url, params)

request.on_complete do |response|
ads << Ad.new(JSON.parse(response.body))
end

hydra.queue(request)
end

hydra.run
end

def ads
@ads ||= []
end

private

# I get undefined method `<=>' for :user_id:Symbol in Ruby 1.8.6
def turn_keys_to_strings(hash)
new_hash = {}
hash.each {|k,v| new_hash[k.to_s] = v}
new_hash
end

def build_url(method)
case method
when :test
path = '/test/ads.json'
when :user
path = '/ads/user.json'
when :search
path = '/ads/search.json'
end

base_uri + path
end

def base_uri
"http://api.140proof.com"
end

def json
@json ||= JSON.parse(@response.body)
end

end
end
end
Empty file removed lib/one40_proof/multi/search.rb
Empty file.
Empty file removed lib/one40_proof/multi/test.rb
Empty file.
Empty file removed lib/one40_proof/multi/user_ad.rb
Empty file.
12 changes: 6 additions & 6 deletions lib/one40_proof/simple/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ def initialize(url, options={})
end

def action_urls
@action_urls ||= ActionUrls.new(json)
@action_urls ||= ActionUrls.new(json['action_urls'])
end

def user
@user ||= User.new(json)
@user ||= User.new(json['user'])
end

def image_url
json['ads'][0]['image_url']
json['image_url']
end

# e.g "ads by Pizza Hut"
def byline
json['ads'][0]['byline']
json['byline']
end

# Ad text
def text
json['ads'][0]['text']
json['text']
end

private

def json
@json ||= JSON.parse(@response.body)
@json ||= JSON.parse(@response.body)['ads'][0]
end

def validate_response(response)
Expand Down
3 changes: 2 additions & 1 deletion spec/one40_proof/attributes/action_urls_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'one40_proof/multi'

describe "ActionUrls" do
before(:all) do
parsed_json = JSON.parse(test_ad_data)
parsed_json = JSON.parse(test_ad_data)['ads'][0]['action_urls']
@action_urls = One40Proof::ActionUrls.new(parsed_json)
end

Expand Down
29 changes: 29 additions & 0 deletions spec/one40_proof/attributes/ad_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'one40_proof/multi'

describe "Ad" do
before(:all) do
parsed_json = JSON.parse(test_ad_data)
@ad = One40Proof::Ad.new(parsed_json)
end

it "should have an image_url" do
@ad.image_url.should_not be_nil
end

it "should have byline" do
@ad.byline.should_not be_nil
end

it "should have text" do
@ad.text.should_not be_nil
end

it "should return a User object" do
@ad.user.should be_a(One40Proof::User)
end

it "should return an ActionUrls object" do
@ad.action_urls.should be_a(One40Proof::ActionUrls)
end
end
3 changes: 2 additions & 1 deletion spec/one40_proof/attributes/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'one40_proof/multi'

describe "User" do
before(:all) do
parsed_json = JSON.parse(test_ad_data)
parsed_json = JSON.parse(test_ad_data)['ads'][0]['user']
@user = One40Proof::User.new(parsed_json)
end

Expand Down
36 changes: 35 additions & 1 deletion spec/one40_proof/multi/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'one40_proof/multi'

describe "Base" do
describe "MultiBase" do
before(:all) do
hydra = Typhoeus::Hydra.new
test_response = Response.new(:code => 200, :body => test_ad_data)
user_response = Response.new(:code => 200, :body => user_ad_data)

# I receive undefined method `request=' for #<WebMock::Response:0x7fc658> without these:
test_response.stub!(:request=)
user_response.stub!(:request=)

hydra.stub(:get, "http://api.140proof.com/test/ads.json").and_return(test_response)
hydra.stub(:get, "http://api.140proof.com/ads/user.json?app_id=test&user_id=sferik").and_return(user_response)
hydra.stub(:get, "http://api.140proof.com/ads/search.json?app_id=test&q=New+York+Mets&user_id=sferik").and_return(user_response)

Typhoeus::Hydra.stub!(:new).and_return(hydra)

@a = One40Proof::Multi::Base.new(queries)
@a.ads.should be_an(Array)
end

it "should return an Array of Ads" do
@a.ads.should be_an(Array)
end

it "should create 3 ads" do
@a.ads.size.should == 3
end

def queries
[
{:method => :test},
{:method => :user, :user_id => 'sferik', :app_id => 'test'},
{:method => :search, :user_id => 'sferik', :app_id => 'test', :q => 'New York Mets'}
]
end
end
5 changes: 0 additions & 5 deletions spec/one40_proof/multi/search_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/one40_proof/multi/test_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/one40_proof/multi/user_ad_spec.rb

This file was deleted.

3 changes: 2 additions & 1 deletion spec/one40_proof/simple/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'one40_proof/simple'

describe "Base" do

describe "200 OK" do
before(:all) do
stub_request(:get, base_url).to_return(:body => user_ad_data)
Expand Down
10 changes: 5 additions & 5 deletions spec/one40_proof/simple/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

describe "Search" do
it "should request GET /ads/search.json..." do
url = base_url + '/ads/search.json?user_id=sferik&app_id=test&q=New%20York%20Mets'
stub_request(:get, url)
One40Proof::Search.new(:user_id => 'sferik', :app_id => 'test', :q => 'New York Mets')
WebMock.should have_requested(:get, url)
url = base_url + '/ads/search.json?user_id=sferik&app_id=test&q=New%20York%20Mets'
stub_request(:get, url)
One40Proof::Search.new(:user_id => 'sferik', :app_id => 'test', :q => 'New York Mets')

WebMock.should have_requested(:get, url)
end
end
Loading

0 comments on commit f9987bf

Please sign in to comment.