-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimenting: try out parallel ad requests
- Loading branch information
Showing
23 changed files
with
248 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$:.unshift(File.dirname(__FILE__)) | ||
|
||
require 'multi/base' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.