Class: GoodDataMarketo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gooddata_marketo/models/tags.rb,
lib/gooddata_marketo/client.rb,
lib/gooddata_marketo/models/channels.rb

Overview

Constant Summary collapse

DEFAULT_CONFIG =
{
    wsdl: 'http://app.marketo.com/soap/mktows/2_4?WSDL',
    read_timeout:            500,
    open_timeout:            500,
    namespace_identifier:    :ns1,
    env_namespace:           'SOAP-ENV',
    namespaces:              { 'xmlns:ns1' => 'http://www.marketo.com/mktows/' },
    pretty_print_xml:        true,
    api_subdomain:           '363-IXI-287',
    api_version:             '2_7',
    log:                     false,
    log_level:               :debug
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gooddata_marketo/client.rb', line 79

def initialize(config = {})

  GoodDataMarketo.logging = true if config[:log]

  @api_limit = config[:api_limit] || MARKETO_API_LIMIT

  @loads = nil # Default is no jobs will be active.
  @load = nil # No job is currently set.
  @leads = nil

  config = DEFAULT_CONFIG.merge(config)

  @api_version = config.delete(:api_version).freeze
  @subdomain = config.delete(:api_subdomain).freeze
  @webdav = config.delete(:webdav)
  @logger = config.delete(:logger)

  @activity_types = ActivityTypes.new.values

  user_id = config.delete(:user_id)
  encryption_key = config.delete(:encryption_key)

  @auth = AuthHeader.new(user_id, encryption_key)

  @wsdl = "http://app.marketo.com/soap/mktows/#{@api_version}?WSDL".freeze
  @endpoint = "https://#{@subdomain}.mktoapi.com/soap/mktows/#{@api_version}".freeze

  #Create SOAP Header
  @savon = Savon.client(config.merge(endpoint: @endpoint))

  if GoodDataMarketo.logging
    puts use = self.usage
    puts "#{Time.now} => Marketo:SOAP/REST:Used of #{MARKETO_API_LIMIT}"

  end

end

Instance Attribute Details

#activity_typesObject

Returns the value of attribute activity_types.



77
78
79
# File 'lib/gooddata_marketo/client.rb', line 77

def activity_types
  @activity_types
end

#api_limitObject

Returns the value of attribute api_limit.



76
77
78
# File 'lib/gooddata_marketo/client.rb', line 76

def api_limit
  @api_limit
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/gooddata_marketo/models/tags.rb', line 7

def client
  @client
end

#loadObject

Returns the value of attribute load.



74
75
76
# File 'lib/gooddata_marketo/client.rb', line 74

def load
  @load
end

#webdavObject

Returns the value of attribute webdav.



75
76
77
# File 'lib/gooddata_marketo/client.rb', line 75

def webdav
  @webdav
end

Instance Method Details

#call(web_method, params, config = {}) ⇒ Object

:nodoc:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/gooddata_marketo/client.rb', line 141

def call(web_method, params, config = {}) #:nodoc:
  @api_limit -= 1
  @params = params
  @timeouts = 0
  @error = nil

  # If the time limit is up, sect the process to wait
  if @api_limit < 1

    now = DateTime.now.utc
    cst = now.in_time_zone('Central Time (US & Canada)')
    delay = (24 - cst.hours) * 2
    delay = 1 if delay == 0

    sleep (delay * 60 * 60)
    puts "#{Time.now} => API_LIMIT:Sleeping: #{delay}"

  end

  begin

    def timed_call web_method, params, config = {}

      puts "#{Time.now} => API Limit: #{@api_limit}" if GoodDataMarketo.logging
      puts "#{Time.now} => Call: #{web_method}: #{@params.to_s}" if GoodDataMarketo.logging

      Timeout.timeout(config[:timeout] || 4999) do

        response = @savon.call(
            web_method,
            message: params,
            soap_header: { 'ns1:AuthenticationHeader' => @auth.signature }
        ).to_hash

        # Add control flow to the root call because Marketo API changes for structure for just getLeadActivities
        if response[:success_get_lead_activity]
          response[:success_get_lead_activity][:lead_activity_list]
        else
          response[response.keys.first][:result]
        end

      end

    end

    timed_call web_method, @params, config

  rescue Timeout::Error => e
      @timeouts += 1

      forward_days = config[:forward_days] || 30

      def move_forward_30_days original_time, days
        smart_time = Date.parse(original_time) + days
        Time.parse(smart_time.to_s).to_s
      end

      if @params[:start_position]

        last_created_at = @params[:start_position][:last_created_at]
        latest_created_at = @params[:start_position][:latest_created_at]
        oldest_created_at = @params[:start_position][:oldest_created_at]
        last_updated_at = @params[:start_position][:last_updated_at]

        if oldest_created_at
          new_time = move_forward_30_days(oldest_created_at, forward_days)
          @params[:start_position][:oldest_created_at] = new_time
        elsif last_created_at
          new_time = move_forward_30_days(last_created_at, forward_days)
          @params[:start_position][:last_created_at] = new_time
        elsif latest_created_at
          new_time = move_forward_30_days(latest_created_at, forward_days)
          @params[:start_position][:latest_created_at] = new_time
        elsif last_updated_at
          new_time = move_forward_30_days(last_updated_at, forward_days)
          @params[:start_position][:last_updated_at] = new_time
        else
          exit
        end

        puts "#{Time.now} => #{e}:Retrying requested date +#{forward_days} days:attempts:#{@timeouts}/12" if GoodDataMarketo.logging
        retry unless @timeouts > 12

      else
        exit
      end

  end

rescue Exception => e
  puts @error = e
  @logger.log(e) if @logger
  nil
end

#campaignsObject



348
349
350
# File 'lib/gooddata_marketo/client.rb', line 348

def campaigns # http://developers.marketo.com/documentation/soap/campaigns/
  GoodDataMarketo::Campaigns.new :client => self
end

#channels(config = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gooddata_marketo/models/channels.rb', line 3

def channels config = {} # http://developers.marketo.com/documentation/soap/getchannels/

  values = config[:values] || config[:channels]
  if values.is_a? String
    values = [values]
  end

  request = {
      :tag => {
          :values => {
              :string_item => []
          }
      }
  }

  if values
    request[:tag][:values][:string_item] = values
  end


  self.call(:get_campaigns_for_source, request)

end

#configurationObject



137
138
139
# File 'lib/gooddata_marketo/client.rb', line 137

def configuration
  DEFAULT_CONFIG
end

#died(json = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gooddata_marketo/client.rb', line 236

def died(json = nil)

  if json
    file = File.open('marketo_connector_log.json','wb')
    file.write(json.to_json)
    exit
  else
    json = File.open('marketo_connector_log.json', 'r')
    json.to_h
  end

end

#get_all_leads(config = {}) ⇒ Object Also known as: write_all_lead_ids_to_csv



328
329
330
331
# File 'lib/gooddata_marketo/client.rb', line 328

def get_all_leads config = {}
  rest = GoodDataMarketo::RESTAdapter.new :webdav => @webdav
  rest.get_all_leads
end

#leadsObject Also known as: lead



339
340
341
# File 'lib/gooddata_marketo/client.rb', line 339

def leads # http://developers.marketo.com/documentation/soap/getleadchanges/
  GoodDataMarketo::Leads.new :client => self
end

#loads(config = {}) ⇒ Object



360
361
362
363
# File 'lib/gooddata_marketo/client.rb', line 360

def loads(config = {})
  self.load = true
  @loads = GoodDataMarketo::Loads.new config
end

#mobjectsObject Also known as: objects



352
353
354
# File 'lib/gooddata_marketo/client.rb', line 352

def mobjects # http://developers.marketo.com/documentation/soap/getmobjects/
  GoodDataMarketo::MObjects.new :client => self
end

#operationsObject



356
357
358
# File 'lib/gooddata_marketo/client.rb', line 356

def operations
  @savon.operations
end

#restObject



324
325
326
# File 'lib/gooddata_marketo/client.rb', line 324

def rest
  GoodDataMarketo::RESTAdapter.new :webdav => @webdav
end

#safe_stream(web_method, params, config) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/gooddata_marketo/client.rb', line 262

def safe_stream web_method, params, config
  begin
    #Timeout.timeout(config[:timeout] || 18000) do
    GoodDataMarketo::Stream.new web_method, params, :client => self
    #end
  rescue Timeout::Error => e
    @timeouts += 1
    puts e if GoodDataMarketo.logging
    params[:timeouts] = @timeouts
    self.load.log('TIMEOUT') if self.load
    self.died params
  end

end

#set_load(load) ⇒ Object



335
336
337
# File 'lib/gooddata_marketo/client.rb', line 335

def set_load load
  @load = load
end

#stream(web_method, params, config = {}) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/gooddata_marketo/client.rb', line 249

def stream(web_method, params, config = {})

  # If the stream is part of a load.

  if self.load
    "#{Time.now} => Load:#{self.load.json[:type]}:#{self.load.json[:method]}" if GoodDataMarketo.logging
  end

  puts "#{Time.now} => Stream: #{web_method}: #{params.to_s}" if GoodDataMarketo.logging

  safe = config[:safe] || true
  @timeouts = 0

  def safe_stream web_method, params, config
    begin
      #Timeout.timeout(config[:timeout] || 18000) do
      GoodDataMarketo::Stream.new web_method, params, :client => self
      #end
    rescue Timeout::Error => e
      @timeouts += 1
      puts e if GoodDataMarketo.logging
      params[:timeouts] = @timeouts
      self.load.log('TIMEOUT') if self.load
      self.died params
    end

  end

  if safe
    safe_stream web_method, params, config
  else
    GoodDataMarketo::Stream.new web_method, params, :client => self
  end


end

#tags(config = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gooddata_marketo/models/tags.rb', line 9

def tags config = {} # http://developers.marketo.com/documentation/soap/gettags/

  values = config[:values] || config[:value] || config[:lead]
  values = [values] if values.is_a? String

  if config[:type]
    request = {
        :tag_list => {
            :tag => {
                :type => config[:type],
                :values => {
                    :string_item => values
                }
            }
        }
    }
  else
    request = {}
  end

  response = self.call(:get_tags, request)

end

#test_restObject



117
118
119
120
121
122
123
124
125
# File 'lib/gooddata_marketo/client.rb', line 117

def test_rest
  puts "#{Time.now} => SETUP: Connected to Marketo REST API:#{@subdomain}" if GoodDataMarketo.logging
  begin
    self.usage
    true
  rescue
    false
  end
end

#test_soapObject



127
128
129
130
131
132
133
134
135
# File 'lib/gooddata_marketo/client.rb', line 127

def test_soap
  puts "#{Time.now} => SETUP: Connected to Marketo SOAP API:#{@subdomain}" if GoodDataMarketo.logging
  begin
    self.leads.get_by_email('[email protected]')
    true
  rescue
    false
  end
end

#usage(config = {}) ⇒ Object



343
344
345
346
# File 'lib/gooddata_marketo/client.rb', line 343

def usage(config = {})
  rest = GoodDataMarketo::RESTAdapter.new :webdav => @webdav
  rest.usage
end