class MarketoChef::Client
Faraday wrapper to handle communication with Marketo API
Public Instance Methods
sync_lead(lead)
click to toggle source
# File lib/marketo_chef/client.rb, line 31 def sync_lead(lead) authenticate! connection.post do |req| req.url '/rest/v1/leads.json' req.headers[:authorization] = "Bearer #{@token}" req.body = { action: 'createOrUpdate', input: [lead] } end.body end
trigger_campaign(campaign_id, lead_id)
click to toggle source
# File lib/marketo_chef/client.rb, line 41 def trigger_campaign(campaign_id, lead_id) authenticate! connection.post do |req| req.url "/rest/v1/campaigns/#{campaign_id}/trigger.json" req.headers[:authorization] = "Bearer #{@token}" req.body = { input: { leads: [{ id: lead_id }] } } end.body end
Private Instance Methods
auth_params()
click to toggle source
# File lib/marketo_chef/client.rb, line 91 def auth_params { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret } end
authenticate()
click to toggle source
# File lib/marketo_chef/client.rb, line 76 def authenticate connection.get('/identity/oauth/token', auth_params).tap do |res| inspect_auth_response(res.body) save_authentication(res.body) end end
authenticate!()
click to toggle source
# File lib/marketo_chef/client.rb, line 62 def authenticate! authenticate unless authenticated? end
authenticated?()
click to toggle source
# File lib/marketo_chef/client.rb, line 66 def authenticated? @token && valid_token? end
connection()
click to toggle source
# File lib/marketo_chef/client.rb, line 53 def connection @connection ||= Faraday.new(url: "https://#{host}") do |conn| conn.request :multipart conn.request :json conn.response :json, content_type: /\bjson$/ conn.adapter Faraday.default_adapter end end
inspect_auth_response(body)
click to toggle source
# File lib/marketo_chef/client.rb, line 84 def inspect_auth_response(body) # res.body may be an HTML string, "The document has moved" message, # which occurs if the host is mktoapi.com instead of mktorest.com. raise body if body.is_a?(String) raise body['errors'][0]['message'] if body.key?('errors') end
save_authentication(data)
click to toggle source
# File lib/marketo_chef/client.rb, line 99 def save_authentication(data) @token = data.fetch('access_token') @token_type = data.fetch('token_type') @valid_until = Time.now + data.fetch('expires_in') @scope = data.fetch('scope') end
valid_token?()
click to toggle source
# File lib/marketo_chef/client.rb, line 70 def valid_token? return false unless defined?(@valid_until) Time.now < @valid_until end