Class: Load

Inherits:
Object
  • Object
show all
Defined in:
lib/gooddata_marketo/loads.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Load

Returns a new instance of Load.



66
67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/gooddata_marketo/loads.rb', line 66

def initialize config = {}

  @id = Digest::SHA1.hexdigest(Time.now.to_s)

  def ensure_filetype_json string
    s = string.to_s
    if s.include? '.json'
      s
    else
      s+'_load.json'
    end
  end

  file = config[:file] || config[:name]
  @file = ensure_filetype_json(file)
  @webdav = config[:webdav]
  @client = config[:client]

  # So we don't make the JSON file a mess if we merge with config.
  config.delete(:webdav)
  config.delete(:client)

  # Check to see if the file exists locally first.
  if File.exists? @file

    @json = JSON.parse(IO.read(@file), :symbolize_names => true)

  # If not is the file on WebDAV, if so, download it.
  elsif self.on_webdav?

    self.refresh

  # Otherwise create a new load file locally.
  else

    config[:id] = self.id
    File.open(@file,'w'){ |f| JSON.dump(config, f) }
    @json = config

  end

  @saved = true
  @type = @json[:type] # "leads"
  @method = @json[:method] # "get_changes"
  @arguments = @json[:arguments] || @json[:parameters] # ARGS = ":oldest_created_at => 'March 26th 2014', :filters => ['Merge Leads']"

end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



62
63
64
# File 'lib/gooddata_marketo/loads.rb', line 62

def arguments
  @arguments
end

#clientObject

Returns the value of attribute client.



60
61
62
# File 'lib/gooddata_marketo/loads.rb', line 60

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



64
65
66
# File 'lib/gooddata_marketo/loads.rb', line 64

def id
  @id
end

#jsonObject (readonly)

Returns the value of attribute json.



63
64
65
# File 'lib/gooddata_marketo/loads.rb', line 63

def json
  @json
end

#storageObject

Returns the value of attribute storage.



61
62
63
# File 'lib/gooddata_marketo/loads.rb', line 61

def storage
  @storage
end

Instance Method Details

#ensure_filetype_json(string) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/gooddata_marketo/loads.rb', line 70

def ensure_filetype_json string
  s = string.to_s
  if s.include? '.json'
    s
  else
    s+'_load.json'
  end
end

#execute(config = {}) ⇒ Object Also known as: start, run



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gooddata_marketo/loads.rb', line 114

def execute config = {}

  # Assign the current load to the client.
  client = config[:client] || @client
  client.set_load(self)

  # EXAMPLE LOADS FOR TYPE "LEADS"
  # get_lead_changes_from_march = {
  #     :name => 'get_lead_changes_from_march',
  #     :type => 'leads',
  #     :method => 'get_changes',
  #     :arguments => {
  #         :oldest_created_at => 'March 26th 2014',
  #         :latest_created_at => 'March 27th 2014',
  #         :filters => ['Merge Leads']
  #     }
  # }
  #
  # get_multiple_from_january = {
  #     :name => 'get_all_leads_january',
  #     :type => 'leads',
  #     :method => 'get_multiple',
  #     :arguments => {
  #         :oldest_created_at => 'January 1st 2014',
  #         :latest_created_at => 'January 2nd 2014',
  #         :filters => ['']
  #     }
  # }

  case @type

    when "leads"
      self.log('REQUEST')
      client.leads.send(@method, @arguments)
    else
      raise 'Unable to load JOB type (Ex. "leads").'
  end
end

#get_argument_value(key) ⇒ Object Also known as: get_argument



184
185
186
# File 'lib/gooddata_marketo/loads.rb', line 184

def get_argument_value key
  @arguments[key]
end

#log(type) ⇒ Object

Upload the updated log to WebDAV



157
158
159
160
161
162
163
164
165
# File 'lib/gooddata_marketo/loads.rb', line 157

def log type

  file_name = 'marketo_load_log.txt'
  log_file = File.open(file_name,'a')
  log_file.puts("#{self.id} --TYPE #{self.json[:type]} --METHOD #{self.json[:method]} --TIMESTAMP #{Time.now} --STATE #{type}")

  self.upload(file_name)

end

#merge_with_load(load) ⇒ Object



194
195
196
# File 'lib/gooddata_marketo/loads.rb', line 194

def merge_with_load load
  @json = load
end

#on_webdav?(f = false) ⇒ Boolean

Check if the current object “@file” is on WebDAV

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gooddata_marketo/loads.rb', line 168

def on_webdav? f = false

  if f
    file = f
  else
    file = @file
  end

  if @webdav.exists? file
    true
  else
    false
  end

end

#refreshObject



206
207
208
209
210
211
212
# File 'lib/gooddata_marketo/loads.rb', line 206

def refresh
  if File.exist? (@file)
    File.delete(@file)
  end
  json = @webdav.download @file
  File.open(@file,'w'){ |f| JSON.dump(json, f) }
end

#saveObject



226
227
228
229
230
231
232
233
# File 'lib/gooddata_marketo/loads.rb', line 226

def save

  File.open(@file,'w'){ |f| JSON.dump(@json, f) }
  @saved = true

  self.upload

end

#set_argument_value(key, value) ⇒ Object



188
189
190
# File 'lib/gooddata_marketo/loads.rb', line 188

def set_argument_value key, value
  @arguments[key] = value
end

#terminateObject Also known as: kill



214
215
216
217
218
219
220
221
222
# File 'lib/gooddata_marketo/loads.rb', line 214

def terminate
  # Delete on local
  File.delete(@file) if File.exists? @file

  if @webdav.exists? @file
    # Delete on remote
    @webdav.delete(@file)
  end
end

#upload(file = nil) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/gooddata_marketo/loads.rb', line 198

def upload file=nil
  if file
    @webdav.upload file
  else
    @webdav.upload @file
  end
end