Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 7c81887 commit f4c55f12479e77b6bca3c4784ce9ed53555d8801 @reddavis committed Feb 17, 2010
View
@@ -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
@@ -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
@@ -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'
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
No changes.
@@ -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
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit f4c55f1

Please sign in to comment.