Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit f4c55f12479e77b6bca3c4784ce9ed53555d8801 1 parent 7c81887
@reddavis authored
View
65 README.rdoc
@@ -1,17 +1,58 @@
-= One40Proof
+= 140Proof API Wrapper
-Description goes here.
+A Ruby wrapper around the 140Proof API. Documentation can be found here - http://developers.140proof.com/docs
-== Note on Patches/Pull Requests
-
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
+It currently only supports JSON.
+== Install
+
+ gem sources -a http://gemcutter.org
+ sudo gem install one40_proof
+
+== How To Use
+
+Testing ad placement while in development
+
+ require 'rubygems'
+ require 'one40_proof'
+
+ ad = One40Proof::Test.new
+
+ # 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
+
+
+To get an ad for a specific user
+
+ require 'rubygems'
+ require 'one40_proof'
+
+ ad = One40Proof::UserAd.new(:user_id => 'reddavis', :publisher_id => 'your publisher id')
+
+
+To get an ad for a specific query
+
+ require 'rubygems'
+ require 'one40_proof'
+
+ ad = One40Proof::Search.new(:user_id => 'reddavis', :publisher_id => 'your publisher id', :q => 'magic hats')
+
== Copyright
-Copyright (c) 2010 reddavis. See LICENSE for details.
+Copyright (c) 2010 Red Davis. See LICENSE for details.
View
8 Rakefile
@@ -4,13 +4,15 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
- gem.name = "One40Proof"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.name = "one40_proof"
+ gem.summary = %Q{A Ruby wrapper around the 140Proof API}
+ gem.description = %Q{A Ruby wrapper around the 140Proof API. Documentation can be found here - http://developers.140proof.com/docs/}
gem.email = "reddavis@gmail.com"
gem.homepage = "http://github.com/reddavis/One40Proof"
gem.authors = ["reddavis"]
gem.add_development_dependency "rspec", ">= 1.2.9"
+ gem.add_dependency "json"
+ gem.add_dependency "httparty"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
5 lib/one40_proof.rb
@@ -1,4 +1,5 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
-require 'httparty'
-require 'one40_proof/test'
+require 'one40_proof/test'
+require 'one40_proof/user_ad'
+require 'one40_proof/search'
View
34 lib/one40_proof/attributes/action_urls.rb
@@ -0,0 +1,34 @@
+module One40Proof
+ class ActionUrls
+
+ def initialize(data)
+ @data = data['ads'][0]['action_urls']
+ end
+
+ def click_url
+ @data['click_url']
+ end
+
+ def favorite_url
+ @data['favorite_url']
+ end
+ alias_method :favourite_url, :favorite_url # For the English ;)
+
+ def impression_url
+ @data['impression_url']
+ end
+
+ def friendship_url
+ @data['friendship_url']
+ end
+
+ def reply_url
+ @data['reply_url']
+ end
+
+ def retweet_url
+ @data['retweet_url']
+ end
+
+ end
+end
View
25 lib/one40_proof/attributes/user.rb
@@ -0,0 +1,25 @@
+module One40Proof
+ class User
+
+ def initialize(data)
+ @data = data['ads'][0]['user']
+ end
+
+ def profile_image_url
+ @data['profile_image_url']
+ end
+
+ def name
+ @data['name']
+ end
+
+ def user_id
+ @data['id']
+ end
+
+ def screen_name
+ @data['screen_name']
+ end
+
+ end
+end
View
44 lib/one40_proof/base.rb
@@ -1,9 +1,53 @@
+require 'httparty'
require 'json'
+require 'one40_proof/attributes/action_urls'
+require 'one40_proof/attributes/user'
+require 'one40_proof/exceptions'
module One40Proof
class Base
include HTTParty
format :json
base_uri "http://api.140proof.com"
+
+ def initialize(url, options={})
+ validate_response(Base.get(url, options))
+ end
+
+ def action_urls
+ @action_urls ||= ActionUrls.new(json)
+ end
+
+ def user
+ @user ||= User.new(json)
+ end
+
+ def image_url
+ json['ads'][0]['image_url']
+ end
+
+ # e.g "ads by Pizza Hut"
+ def byline
+ json['ads'][0]['byline']
+ end
+
+ # Ad text
+ def text
+ json['ads'][0]['text']
+ end
+
+ private
+
+ def json
+ @json ||= JSON.parse(@response.body)
+ end
+
+ def validate_response(response)
+ if response.code == 404
+ raise NotFound.new("Either user/publisher/targetted ad cannot be found")
+ else
+ @response = response
+ end
+ end
end
end
View
8 lib/one40_proof/exceptions.rb
@@ -0,0 +1,8 @@
+module One40Proof
+
+ # Raised:
+ # * When a user does not exists
+ # * When a publisher does not exist
+ # * If a targetted ad cannot be found
+ class NotFound < Exception; end
+end
View
24 lib/one40_proof/search.rb
@@ -0,0 +1,24 @@
+require 'one40_proof/base'
+
+# Returns an ad for a specified user.
+# Returns an HTTP 404 error if the user does not exist, if the publisher does not exist, or if a targetted ad could not be found for the user.
+
+module One40Proof
+ class Search < Base
+
+ # Options:
+ # * user_id. Required. The Twitter ID or screen name of a user.
+ # * publisher_id. Required. Your 140 Proof app id.
+ # * q. Required. The query the user entered. Query strings should be URL encoded. Queries are limited 140 URL encoded characters.
+ # * lat. Optional. The user's current latitude. Note: The valid ranges for latitude is -90.0 to +90.0 (North is positive) inclusive.
+ # This parameter will be ignored if outside that range, if it is not a number, or if there not a corresponding long parameter with this request.
+ # * long. Optional. The user's current longitude. Note:
+ # The valid ranges for longitude is -180.0 to +180.0 (East is positive) inclusive.
+ # This parameter will be ignored if outside that range, if it is not a number, or if there not a corresponding lat parameter with this request.
+ # * lang. Optional. Restricts ads to the given language, specified by an ISO 639-1 code - http://en.wikipedia.org/wiki/ISO_639-1
+ def initialize(options={})
+ super('/ads/search.json', {:query => options})
+ end
+
+ end
+end
View
25 lib/one40_proof/test.rb
@@ -1,30 +1,13 @@
require 'one40_proof/base'
+# Returns an ad. Use for testing and verifying your ad-serving code while in development.
+
module One40Proof
class Test < Base
def initialize
- @response = Test.get('/test/ads.json')
- end
-
- def click_url
- action_urls['click_url']
+ super('/test/ads.json')
end
-
- def favorite_url
- action_urls['favorite_url']
- end
- alias_method :favourite_url, :favorite_url # For the English
-
- private
-
- def action_urls
- json['action_urls']
- end
-
- def json
- @json ||= JSON.parse(@response.body)['ads'][0]
- end
-
+
end
end
View
0  lib/one40_proof/user.rb
No changes.
View
24 lib/one40_proof/user_ad.rb
@@ -0,0 +1,24 @@
+require 'one40_proof/base'
+
+# Returns an ad for a specified user.
+# Returns an HTTP 404 error if the user does not exist, if the publisher does not exist, or if a targetted ad could not be found for the user.
+
+module One40Proof
+ class UserAd < Base
+
+ # Options include
+ # * user_id. Required. The Twitter ID or screen name of a user.
+ # * publisher_id. Required. Your 140 Proof app id.
+ # * lat. Optional. The user's current latitude. Note:
+ # The valid ranges for latitude is -90.0 to +90.0 (North is positive) inclusive.
+ # This parameter will be ignored if outside that range, if it is not a number, or if there not a corresponding long parameter with this request.
+ # * long. Optional. The user's current longitude. Note:
+ # The valid ranges for longitude is -180.0 to +180.0 (East is positive) inclusive.
+ # This parameter will be ignored if outside that range, if it is not a number, or if there not a corresponding lat parameter with this request.
+ # * lang. Optional. Restricts ads to the given language, specified by an ISO 639-1 code - http://en.wikipedia.org/wiki/ISO_639-1
+ def initialize(options={})
+ super('/ads/user.json', {:query => options})
+ end
+
+ end
+end
View
32 spec/one40_proof/attributes/action_urls_spec.rb
@@ -0,0 +1,32 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe "ActionUrls" do
+ before(:all) do
+ parsed_json = JSON.parse(test_ad_data)
+ @action_urls = One40Proof::ActionUrls.new(parsed_json)
+ end
+
+ it "should return the click_url" do
+ @action_urls.click_url.should == "http://api.140proof.com/clicks/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+
+ it "should return favorite_url" do
+ @action_urls.favorite_url.should == "http://api.140proof.com/favorites/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+
+ it "should return impression_url" do
+ @action_urls.impression_url.should == "http://api.140proof.com/impressions/verify.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+
+ it "should return friendship_url" do
+ @action_urls.friendship_url.should == "http://api.140proof.com/friendships/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+
+ it "should return reply url" do
+ @action_urls.reply_url.should == "http://api.140proof.com/replies/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+
+ it "should return retweet_url" do
+ @action_urls.retweet_url.should == "http://api.140proof.com/retweets/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
+ end
+end
View
24 spec/one40_proof/attributes/user_spec.rb
@@ -0,0 +1,24 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe "User" do
+ before(:all) do
+ parsed_json = JSON.parse(test_ad_data)
+ @user = One40Proof::User.new(parsed_json)
+ end
+
+ it "should return profile_image_url" do
+ @user.profile_image_url.should == "http://img.tweetimag.es/i/Mets_n"
+ end
+
+ it "should return name" do
+ @user.name.should == "New York Mets"
+ end
+
+ it "should return user_id" do
+ @user.user_id.should == 39367703
+ end
+
+ it "should return screen_name" do
+ @user.screen_name.should == "Mets"
+ end
+end
View
42 spec/one40_proof/base_spec.rb
@@ -0,0 +1,42 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "Base" do
+
+ describe "200 OK" do
+ before(:all) do
+ stub_request(:get, base_url).to_return(:body => user_ad_data)
+ @base = One40Proof::Base.new(base_url)
+ end
+
+ it "should return an ActionUrls object" do
+ @base.action_urls.should be_a(One40Proof::ActionUrls)
+ end
+
+ it "should return a User object" do
+ @base.user.should be_a(One40Proof::User)
+ end
+
+ it "should return image_url" do
+ @base.image_url.should == "http://img.tweetimag.es/i/LittleSkillet_n"
+ end
+
+ it "should return byline" do
+ @base.byline.should == 'ads by 140 Proof'
+ end
+
+ it "should return text" do
+ @base.text.should == "Serving up farm-fresh soul food and San Francisco's finest chicken and waffles. Follow @LittleSkillet for today's specials."
+ end
+ end
+
+ describe "404" do
+ it "should raise a NotFound" do
+ stub_request(:get, base_url).to_return(:status => 404)
+
+ lambda do
+ @base = One40Proof::Base.new(base_url)
+ end.should raise_error(One40Proof::NotFound)
+ end
+ end
+
+end
View
11 spec/one40_proof/search_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "Search" do
+ it "should request GET /ads/search.json..." do
+ url = base_url + '/ads/search.json?user_id=sferik&publisher_id=test&q=New%20York%20Mets'
+ stub_request(:get, url)
+ One40Proof::Search.new(:user_id => 'sferik', :publisher_id => 'test', :q => 'New York Mets')
+
+ WebMock.should have_requested(:get, url)
+ end
+end
View
26 spec/one40_proof/test_spec.rb
@@ -1,7 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Test" do
-
describe "Connection to the service" do
it "should request GET /test/ads.json" do
url = base_url + '/test/ads.json'
@@ -11,29 +10,4 @@
WebMock.should have_requested(:get, url)
end
end
-
- describe "Test data parsing" do
- before(:all) do
- url = base_url + '/test/ads.json'
- stub_request(:get, url).to_return(:body => test_data)
- @request = One40Proof::Test.new
- end
-
- describe "Action URLS" do
- it "should return the click_url" do
- @request.click_url.should == "http://api.140proof.com/clicks/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
- end
-
- it "should return favorite_url (american)" do
- @request.favorite_url.should == "http://api.140proof.com/favorites/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"
- end
-
- it "should return impression_url"
- end
-
- end
-
- def test_data
- %{{"ads":[{"status":{"id":6017128450},"byline":"ads by 140 Proof","text":"NEW #Mets 2010 Alternate Jersey can be ordered here --> http://bit.ly/8P6xYm","action_urls":{"click_url":"http://api.140proof.com/clicks/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","favorite_url":"http://api.140proof.com/favorites/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","impression_url":"http://api.140proof.com/impressions/verify.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","friendship_url":"http://api.140proof.com/friendships/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","reply_url":"http://api.140proof.com/replies/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","retweet_url":"http://api.140proof.com/retweets/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"},"user":{"profile_image_url":"http://img.tweetimag.es/i/Mets_n","name":"New York Mets","id":39367703,"screen_name":"Mets"},"image_url":"http://img.tweetimag.es/i/mets_b"}]}}
- end
end
View
11 spec/one40_proof/user_ad_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "UserAd" do
+ it "should request GET /ads/user.json..." do
+ url = base_url + '/ads/user.json?user_id=sferik&publisher_id=test'
+ stub_request(:get, url)
+ One40Proof::UserAd.new(:user_id => 'sferik', :publisher_id => 'test')
+
+ WebMock.should have_requested(:get, url)
+ end
+end
View
4 spec/one40_proof_spec.rb
@@ -1,4 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "One40Proof" do
-end
View
10 spec/spec_helper.rb
@@ -12,6 +12,16 @@ def base_url
"http://api.140proof.com"
end
+# From http://api.140proof.com/test/ads.json
+def test_ad_data
+ %{{"ads":[{"status":{"id":6017128450},"byline":"ads by 140 Proof","text":"NEW #Mets 2010 Alternate Jersey can be ordered here --> http://bit.ly/8P6xYm","action_urls":{"click_url":"http://api.140proof.com/clicks/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","favorite_url":"http://api.140proof.com/favorites/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","impression_url":"http://api.140proof.com/impressions/verify.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","friendship_url":"http://api.140proof.com/friendships/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","reply_url":"http://api.140proof.com/replies/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7","retweet_url":"http://api.140proof.com/retweets/create.json?ad_id=1&impression_id=1&publisher_id=test&user_id=7"},"user":{"profile_image_url":"http://img.tweetimag.es/i/Mets_n","name":"New York Mets","id":39367703,"screen_name":"Mets"},"image_url":"http://img.tweetimag.es/i/mets_b"}]}}
+end
+
+# From http://api.140proof.com/ads/user.json?user_id=sferik&publisher_id=test
+def user_ad_data
+ %{{"ads":[{"status":{"id":null},"byline":"ads by 140 Proof","text":"Serving up farm-fresh soul food and San Francisco's finest chicken and waffles. Follow @LittleSkillet for today's specials.","action_urls":{"click_url":"http://api.140proof.com/clicks/create.json?ad_id=295&impression_id=7933117&publisher_id=test&user_id=7505382","favorite_url":null,"impression_url":"http://api.140proof.com/impressions/verify.json?ad_id=295&impression_id=7933117&publisher_id=test&user_id=7505382","friendship_url":"http://api.140proof.com/friendships/create.json?ad_id=295&impression_id=7933117&publisher_id=test&user_id=7505382","reply_url":"http://api.140proof.com/replies/create.json?ad_id=295&impression_id=7933117&publisher_id=test&user_id=7505382","retweet_url":null},"user":{"profile_image_url":"http://img.tweetimag.es/i/LittleSkillet_n","name":"Little Skillet","id":27710301,"screen_name":"LittleSkillet"},"image_url":"http://img.tweetimag.es/i/LittleSkillet_n"}]}}
+end
+
Spec::Runner.configure do |config|
end
Please sign in to comment.
Something went wrong with that request. Please try again.