Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/pkg/
/spec/reports/
/tmp/
/.ruby-version
/tags
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
docile (1.1.5)
faraday (0.9.1)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.9.1)
faraday_middleware (0.9.2)
faraday (>= 0.7.4, < 0.10)
json (1.8.2)
method_source (0.8.2)
Expand Down Expand Up @@ -86,4 +86,4 @@ DEPENDENCIES
webmock (~> 1.21.0)

BUNDLED WITH
1.10.6
1.11.2
7 changes: 7 additions & 0 deletions lib/mrkt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

require 'mrkt/concerns/connection'
require 'mrkt/concerns/authentication'
require 'mrkt/concerns/activities'
require 'mrkt/concerns/opportunities'
require 'mrkt/concerns/paging_token'
require 'mrkt/concerns/crud_helpers'
require 'mrkt/concerns/crud_campaigns'
require 'mrkt/concerns/crud_leads'
Expand All @@ -13,11 +16,14 @@ module Mrkt
class Client
include Connection
include Authentication
include Activities
include PagingToken
include CrudHelpers
include CrudCampaigns
include CrudLeads
include CrudLists
include ImportLeads
include Opportunities

attr_accessor :debug

Expand All @@ -35,6 +41,7 @@ def initialize(options = {})
authenticate!

resp = connection.send(http_method, path, payload) do |req|
req.options.params_encoder = Faraday::FlatParamsEncoder
add_authorization(req)
block.call(req) unless block.nil?
end
Expand Down
12 changes: 12 additions & 0 deletions lib/mrkt/concerns/activities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Mrkt
module Activities
def get_activity_types
get("/rest/v1/activities/types.json")
end

def get_activities(page_token, date, activity_type_ids)
params = { nextPageToken: page_token, activityTypeIds: activity_type_ids }
get("/rest/v1/activities.json", params)
end
end
end
4 changes: 4 additions & 0 deletions lib/mrkt/concerns/crud_leads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def get_leads(filter_type, filter_values, fields: nil, batch_size: nil, next_pag
get('/rest/v1/leads.json', params)
end

def describe_lead
get('/rest/v1/leads/describe.json')
end

def createupdate_leads(leads, action: 'createOrUpdate', lookup_field: nil, partition_name: nil, async_processing: nil)
post('/rest/v1/leads.json') do |req|
params = {
Expand Down
19 changes: 19 additions & 0 deletions lib/mrkt/concerns/opportunities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Mrkt
module Opportunities
def get_opportunities(filter_type, filter_values, fields: nil, batch_size: nil, next_page_token: nil)
params = {
filterType: filter_type,
filterValues: filter_values.join(',')
}
params[:fields] = fields if fields
params[:batchSize] = batch_size if batch_size
params[:nextPageToken] = next_page_token if next_page_token

get('/rest/v1/opportunities.json', params)
end

def describe_opportunity
get('/rest/v1/opportunities/describe.json')
end
end
end
21 changes: 21 additions & 0 deletions lib/mrkt/concerns/paging_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Mrkt
module PagingToken
def get_paging_token(date)
params = { sinceDatetime: format_date(date) }

get("/rest/v1/activities/pagingtoken.json", params)[:nextPageToken]
end

private

def format_date(date)
date = Date.parse(date) if date.is_a? String

unless date.is_a? Date
fail ArgumentError.new("Expected Date or String. Got #{date.class}")
end

date.strftime("%FT%T")
end
end
end
101 changes: 101 additions & 0 deletions spec/concerns/activities_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
describe Mrkt::Activities do
include_context 'initialized client'

describe '#get_activity_types' do
let(:response_stub) do
{
"requestId":"6e78#148ad3b76f1",
"success":true,
"result":[
{
"id":1,
"name":"Visit Webpage",
"description":"User visits web page",
"primaryAttribute":{
"name":"Webpage ID",
"dataType":"integer"
},
"attributes":[
{ "name":"Client IP Address", "dataType":"string" },
{ "name":"Query Parameters", "dataType":"string" },
{ "name":"Referrer URL", "dataType":"string" },
{ "name":"Search Engine", "dataType":"string" },
{ "name":"Search Query", "dataType":"string" },
{ "name":"User Agent", "dataType":"string" },
{ "name":"Webpage URL", "dataType":"string" }
]
},
{
"id":2,
"name":"Fill Out Form",
"description":"User fills out and submits form on web page",
"primaryAttribute":{
"name":"Webform ID",
"dataType":"integer"
},
"attributes":[
{ "name":"Client IP Address", "dataType":"string" },
{ "name":"Form Fields", "dataType":"text" },
{ "name":"Query Parameters", "dataType":"string" },
{ "name":"Referrer URL", "dataType":"string" },
{ "name":"User Agent", "dataType":"string" },
{ "name":"Webpage ID", "dataType":"integer" }
]
}
]
}
end
subject { client.get_activity_types }

before do
stub_request(:get, "https://#{host}/rest/v1/activities/types.json")
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
end

describe '#get_activities' do
let(:date) { Date.new(2013, 9, 25) }
let(:first_page_token) { "GIYDAOBNGEYS2MBWKQYDAORQGA5DAMBOGAYDAKZQGAYDALBQ" }
let(:activity_type_ids) { [1, 12] }
let(:first_response_stub) do
{
"requestId":"a9ae#148add1e53d",
"success":true,
"nextPageToken":"EYS2MBWKQYDAORQGA5D",
"moreResult":true,
"result":[
{
"id":2,
"leadId":6,
"activityDate":"2013-09-25T00:39:45+0000",
"activityTypeId":12,
"primaryAttributeValueId":6,
"primaryAttributeValue":"Owyliphys Iledil",
"attributes":[ { "name":"Source Type", "value":"Web page visit" } ]
},
{
"id":3,
"leadId":9,
"activityDate":"2013-09-25T06:56:35+0000",
"activityTypeId":1,
"primaryAttributeValueId":4,
"primaryAttributeValue":"anti-phishing",
"attributes":[ { "name":"Query Parameters", "value":nil } ]
}
]
}
end

subject { client.get_activities(first_page_token, date, activity_type_ids) }

before do
activity_type_ids_params = activity_type_ids.map { |id| "activityTypeIds=#{id}" }.join("&")
stub_request(:get, "https://#{host}/rest/v1/activities.json?nextPageToken=#{first_page_token}&#{activity_type_ids_params}")
.to_return(json_stub(first_response_stub))
end

it { is_expected.to eq(first_response_stub) }
end
end
48 changes: 48 additions & 0 deletions spec/concerns/crud_leads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@
it { is_expected.to eq(response_stub) }
end

describe '#describe_lead' do
let(:response_stub) do
{
"requestId":"37ca#1475b74e276",
"success":true,
"result":[
{
"id":2,
"displayName":"Company Name",
"dataType":"string",
"length":255,
"rest":{
"name":"company",
"readOnly":false
},
"soap":{
"name":"Company",
"readOnly":false
}
},
{
"id":3,
"displayName":"Site",
"dataType":"string",
"length":255,
"rest":{
"name":"site",
"readOnly":false
},
"soap":{
"name":"Site",
"readOnly":false
}
}
]
}
end

subject { client.describe_lead }

before do
stub_request(:get, "https://#{host}/rest/v1/leads/describe.json")
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
end

describe '#createupdate_leads' do
let(:leads) do
[
Expand Down
109 changes: 109 additions & 0 deletions spec/concerns/opportunities_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
describe Mrkt::Opportunities do
include_context 'initialized client'

describe '#get_opportunities' do
let(:filter_type) { 'seq' }
let(:filter_values) { [0, 1] }
let(:response_stub) do
{
"requestId":"e42b#14272d07d78",
"success":true,
"result":[
{
"seq":0,
"marketoGUID":"da42707c-4dc4-4fc1-9fef-f30a3017240a",
"externalOpportunityId":"19UYA31581L000000",
"name":"Chairs",
"description":"Chairs",
"amount":"1604.47",
"source":"Inbound Sales Call/Email"
},
{
"seq":1,
"marketoGUID":"da42707c-4dc4-4fc1-9fef-f30a3017240b",
"externalOpportunityId":"29UYA31581L000000",
"name":"Big Dog Day Care-Phase12",
"description":"Big Dog Day Care-Phase12",
"amount":"1604.47",
"source":"Email"
}
]
}
end
subject { client.get_opportunities(filter_type, filter_values) }

before do
stub_request(:get, "https://#{host}/rest/v1/opportunities.json")
.with(query: { filterType: filter_type, filterValues: filter_values.join(',') })
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
end

describe '#describe_opportunity' do
let(:response_stub) do
{
"requestId":"185d6#14b51985ff0",
"success":true,
"result":[
{
"name":"opportunity",
"displayName":"Opportunity",
"createdAt":"2015-02-03T22:36:23Z",
"updatedAt":"2015-02-03T22:36:24Z",
"idField":"marketoGUID",
"dedupeFields":[
"externalOpportunityId"
],
"searchableFields":[
[
"externalOpportunityId"
],
[
"marketoGUID"
]
],
"fields":[
{
"name":"marketoGUID",
"displayName":"Marketo GUID",
"dataType":"string",
"length":36,
"updateable":false
},
{
"name":"createdAt",
"displayName":"Created At",
"dataType":"datetime",
"updateable":false
},
{
"name":"updatedAt",
"displayName":"Updated At",
"dataType":"datetime",
"updateable":false
},
{
"name":"externalOpportunityId",
"displayName":"External Opportunity Id",
"dataType":"string",
"length":50,
"updateable":false
}
]
}
]
}
end

subject { client.describe_opportunity }

before do
stub_request(:get, "https://#{host}/rest/v1/opportunities/describe.json")
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
end
end
Loading