Skip to content

Commit

Permalink
Adding basic campaign querying and creating
Browse files Browse the repository at this point in the history
  • Loading branch information
ericrvass committed May 14, 2014
1 parent c2c3510 commit ec553c6
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ttdrest

TODO: Write a gem description
TTD API Ruby Client

## Installation

Expand Down
5 changes: 1 addition & 4 deletions lib/ttdrest.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
require "ttdrest/version"

module Ttdrest
# Your code goes here...
end
require "ttdrest/client"
36 changes: 36 additions & 0 deletions lib/ttdrest/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "ttdrest/concerns/base"
require "ttdrest/concerns/campaign"

module Ttdrest
# Interface for using the TTD REST API
class Client
include Ttdrest::Concerns::Base
include Ttdrest::Concerns::Campaign

# The OAuth client login
attr_accessor :client_login
# The OAuth client password
attr_accessor :client_password
# The OAuth access token
attr_accessor :auth_token
# The Host
attr_accessor :host
# The Advertiser ID
attr_accessor :advertiser_id

def initialize(options = {})
if options.is_a?(String)
@options = YAML.load_file(options)
else
@options = options
end
@options.symbolize_keys!

self.client_login = ENV['TTD_CLIENT_LOGIN'] || @options[:client_login]
self.client_password = ENV['TTD_CLIENT_PASSWORD'] || @options[:client_password]
self.host = ENV['TTD_HOST'] || @options[:host]
self.advertiser_id = ENV['TTD_ADVERTISER_ID'] || @options[:advertiser_id]
end

end
end
118 changes: 118 additions & 0 deletions lib/ttdrest/concerns/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module Ttdrest
class AuthorizationFailedError < StandardError; end

module Concerns
module Base

VERSION = "v2"
RETRIES = 2

def authenticate(options = {})
client_id = self.client_login || options[:client_login]
client_secret = self.client_password || options[:client_password]
result = auth_post(client_login, client_password)
self.auth_token = result["Token"]
return self.auth_token
end

def check_response(response)
if response.code.eql?("403")
raise AuthorizationFailedError
end
end

def get(path, params)
tries = RETRIES
begin
request = Net::HTTP::Get.new(params.blank? ? path : "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')))
request['TTD-Auth'] = self.auth_token
response = http_connection.request(request)
check_response(response)
result = response.body.blank? ? "" : JSON.parse(response.body)
return result
rescue AuthorizationFailedError
tries -= 1
if tries > 0
self.auth_token = authenticate
retry
end
rescue Exception => e
puts 'Error In GET: ' + e.message
end
end

def data_post(path, content_type, json_data)
tries = RETRIES
begin
request = Net::HTTP::Post.new(path, initheader = {'Content-Type' => content_type})
request['TTD-Auth'] = self.auth_token
request.body = json_data
response = http_connection.request(request)
check_response(response)
result = response.body.blank? ? "" : JSON.parse(response.body)
return result
rescue AuthorizationFailedError
tries -= 1
if tries > 0
self.auth_token = authenticate
retry
end
rescue Exception => e
puts 'Error In Data POST: ' + e.message
end
end

def auth_post(client_login, client_password)
begin
path = "/#{VERSION}/authentication"
request = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
auth_data = {"Login" => client_login, "Password" => client_password}.to_json
request.body = auth_data
response = http_connection.request(request)
result = JSON.parse(response.body)
return result
rescue Exception => e
puts 'Error Authenticating: ' + e.message
end
end

def file_post(path, content_type, file)
tries = RETRIES
begin
request = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>content_type})
request['TTD-Auth'] = self.auth_token
request.body = file
response = http_connection.request(request)
check_response(response)
result = response.body.blank? ? "" : JSON.parse(response.body)
return result
rescue AuthorizationFailedError
tries -= 1
if tries > 0
self.auth_token = authenticate
retry
end
rescue Exception => e
puts 'Error In File POST: ' + e.message
end
end

def http_connection(options = {})
uri = URI.parse("https://#{self.host || options[:host]}")
connection = Net::HTTP.new(uri.host, uri.port)
connection.use_ssl = true
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
return connection
end

def auth_header(client_id, client_secret)
credential = "#{client_id}:#{client_secret}"
"Basic #{Base64.encode64(credential)}".delete("\n")
end

def api_header(oauth_token)
"Bearer #{oauth_token}"
end
end
end
end
48 changes: 48 additions & 0 deletions lib/ttdrest/concerns/campaign.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Ttdrest
module Concerns
module Campaign

def get_campaigns(options = {})
auth_token = self.auth_token || options[:auth_token]
advertiser_id = self.advertiser_id || options[:advertiser_id]
path = "/campaigns/#{advertiser_id}"
params = {}
result = get(path, params)
return result
end

def create_campaign(name, budget_in_dollars, start_date, campaign_conversion_reporting_columns = [], options = {})
auth_token = self.auth_token || options[:auth_token]
advertiser_id = self.advertiser_id || options[:advertiser_id]
path = "/campaign"
content_type = 'application/json'
campaign_data = {
"AdvertiserId" => advertiser_id,
"CampaignName" => name,
"BudgetInUSDollars" => budget_in_dollars,
"StartDate" => start_date,
"CampaignConversionReportingColumns" => campaign_conversion_reporting_columns
}
params = options[:params] || {}
if !params[:description].nil?
campaign_data = campaign_data.merge({"Description" => params[:description]})
end
if !params[:end_date].nil?
campaign_data = campaign_data.merge({"EndDate" => params[:end_date]})
end
if !params[:budget_in_impressions].nil?
campaign_data = campaign_data.merge({"BudgetInImpressions" => params[:budget_in_impressions]})
end
if !params[:daily_budget_in_dollars].nil?
campaign_data = campaign_data.merge({"DailyBudgetInUSDollars" => params[:daily_budget_in_dollars]})
end
if !params[:daily_budget_in_impressions].nil?
campaign_data = campaign_data.merge({"DailyBudgetInImpressions" => params[:daily_budget_in_impressions]})
end
result = data_post(path, content_type, campaign_data.to_json)
return result
end

end
end
end
7 changes: 4 additions & 3 deletions ttdrest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Gem::Specification.new do |spec|
spec.version = Ttdrest::VERSION
spec.authors = ["Eric Vass"]
spec.email = ["[email protected]"]
spec.summary = %q{TODO: Write a short summary. Required.}
spec.description = %q{TODO: Write a longer description. Optional.}
spec.summary = %q{TTD API Ruby Client}
#spec.description = %q{TODO: Write a longer description. Optional.}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
#spec.files = `git ls-files -z`.split("\x0")
spec.files = Dir["README.md","Gemfile","Rakefile", "lib/*", "lib/ttdrest/*", "lib/ttdrest/concerns/*"]
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
Expand Down

0 comments on commit ec553c6

Please sign in to comment.