Created
November 27, 2012 00:39
-
-
Save iamnader/4151649 to your computer and use it in GitHub Desktop.
Eloqua Ruby API
This file contains hidden or 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
require 'net/https' | |
module Eloqua | |
class Client | |
attr_reader :site, :user | |
def initialize(site=nil, user=nil, password=nil) | |
@site = site | |
@user = user | |
@password = password | |
@https = Net::HTTP.new('secure.eloqua.com', 443) | |
@https.use_ssl = true | |
@https.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
end | |
METHODS = { | |
:get => ::Net::HTTP::Get, | |
:post => ::Net::HTTP::Post, | |
:put => ::Net::HTTP::Put, | |
:delete => ::Net::HTTP::Delete | |
} | |
def delete(path) | |
request(:delete, path) | |
end | |
def get(path) | |
request(:get, path) | |
end | |
def post(path, body={}) | |
request(:post, path, body) | |
end | |
def put(path, body={}) | |
request(:put, path, body) | |
end | |
def request(method, path, body={}) | |
request = METHODS[method].new(path, {'Content-Type' =>'application/json'}) | |
request.basic_auth @site + '\\' + @user, @password | |
case method | |
when :post, :put | |
request.body = body | |
end | |
response = @https.request(request) | |
return response | |
end | |
end | |
class RESTClient < Client | |
REST_API_PATH = "/API/REST/1.0" | |
def rest_path(path) | |
REST_API_PATH + '/' + path | |
end | |
# convenience methods | |
def create_segment(segment_definition) | |
post(rest_path("assets/contact/segment"), segment_definition) | |
end | |
def get_segment(segment_id) | |
get(rest_path("assets/contact/segment/#{segment_id}")) | |
end | |
def contact_activity(contact_id, options={}) | |
options["start_date"] ||= 1.year.ago.to_i | |
options["end_date"] ||= Time.now.to_i | |
options["type"] ||= "webVisit" | |
options["count"] ||= 1000 | |
get(rest_path("data/activities/contact/#{contact_id}?startDate=#{options["start_date"]}&endDate=#{options["end_date"]}&type=#{options["type"]}&count=#{options["count"]}")) | |
end | |
end | |
class BulkClient < Client | |
BULK_API_PATH = "/API/Bulk/1.0" | |
def bulk_path(path) | |
BULK_API_PATH + '/' + path | |
end | |
# convenience methods | |
def define_export(export_definition) | |
post(bulk_path("contact/export"), export_definition) | |
end | |
def sync(export_uri) | |
post(bulk_path("sync"), {"syncedInstanceUri" => export_uri}.to_json) | |
end | |
def sync_status(sync_uri) | |
get(bulk_path(sync_uri)) | |
end | |
def retrieve_export(export_id, options={}) | |
options[:page] ||= 1 | |
options[:page_size] ||= 50000 | |
get(bulk_path("contact/export/#{export_id}/data?page=#{options[:page]}&pageSize=#{options[:page_size]}")) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment