Class: BlackStack::Base

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

Overview

Base class. List of methods you have to overload if you develop a profile type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Base

Returns a new instance of Base.



100
101
102
# File 'lib/functions.rb', line 100

def initialize(h)
    self.desc = h
end

Instance Attribute Details

#descObject

object json descriptor



94
95
96
# File 'lib/functions.rb', line 94

def desc
  @desc
end

Class Method Details

.account_value(field:) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/functions.rb', line 110

def self.(field:)
    params = {}
    params['field'] = field
    # call the API
    ret = BlackStack::API.post(
        endpoint: "account_value",
        params: params
    )
    raise "Error calling account_value endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result']
end

.countObject

Get array of hash descriptors of profiles.

Parameters:

  • id: guid. Id of the profile to bring.



151
152
153
154
155
156
157
158
159
160
# File 'lib/functions.rb', line 151

def self.count
    params = {}
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/count",
        params: params
    )
    raise "Error calling count endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result'].to_i
end

.get(id) ⇒ Object

Get array of hash descriptors of profiles.

Parameters:

  • id: guid. Id of the profile to bring.



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/functions.rb', line 167

def self.get(id)
    params = {}
    params['id'] = id
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/get",
        params: params
    )
    raise "Error calling get endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.insert(desc) ⇒ Object

Submit a hash descriptor to the server for an insert



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/functions.rb', line 221

def self.insert(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/insert",
        params: params
    )
    raise "Error calling insert endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.object_nameObject



96
97
98
# File 'lib/functions.rb', line 96

def self.object_name
    raise 'You have to overload this method in your class.'
end

.page(page:, limit:, filters: {}) ⇒ Object

Get array of hash descriptor of profile.

Parameters:

  • page: integer. Page number.

  • limit: integer. Number of profiles per page.

  • params: hash. Additional filter parameters used by the specific child class.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/functions.rb', line 130

def self.page(page:, limit:, filters: {})
    # add page and limit to the params
    params = {}
    params['page'] = page
    params['limit'] = limit
    params['filters'] = filters
    params['backtrace'] = BlackStack::API.backtrace
    # call the API
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/page",
        params: params
    )
    raise "Error calling page endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['results'].map { |h| self.new(h).child_class_instance }
end

.update(desc) ⇒ Object

Submit a hash descriptor to the server for an update



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/functions.rb', line 181

def self.update(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/update",
        params: params
    )
    raise "Error calling update endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.update_status(desc) ⇒ Object

Submit a hash descriptor to the server for an update of status only



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/functions.rb', line 195

def self.update_status(desc)
  params = {}
  params['desc'] = desc
  params['backtrace'] = BlackStack::API.backtrace
  ret = BlackStack::API.post(
      endpoint: "#{self.object_name}/update_status",
      params: params
  )
  raise "Error calling update_status endpoint: #{ret['status']}" if ret['status'] != 'success'
  return
end

.upsert(desc) ⇒ Object

Submit a hash descriptor to the server for an upsert



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

def self.upsert(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/upsert",
        params: params
    )
    raise "Error calling upsert endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result'] == {} ? nil : self.new(ret['result']).child_class_instance
end

Instance Method Details

#child_class_instanceObject

Crate an instance of a child class using speicfications in the ‘desc` attribute. By default, returns the same instance.



106
107
108
# File 'lib/functions.rb', line 106

def child_class_instance
    return self
end

#updateObject

Submit a hash descriptor to the server for an update



209
210
211
# File 'lib/functions.rb', line 209

def update
    self.class.update(self.desc)
end

#update_statusObject

Submit a hash descriptor to the server for an update of status only



215
216
217
# File 'lib/functions.rb', line 215

def update_status
  self.class.update(self.desc)
end

#upsertObject

Submit a hash descriptor to the server for an upsert



250
251
252
# File 'lib/functions.rb', line 250

def upsert
    self.class.upsert(self.desc)
end

#zyte_html(url, api_key:, options:, data_filename:) ⇒ Object

return the HTML of a page downloaded by Zyte.

Parameters:

  • url: the URL of the page to download.

  • api_key: the Zyte API key.

  • options: the options to pass to Zyte.



262
263
264
265
266
267
268
269
# File 'lib/functions.rb', line 262

def zyte_html(url, api_key:, options:, data_filename:)
    ret = nil
    # getting the HTML
    zyte = ZyteClient.new(key: api_key)
    html = zyte.extract(url: url, options: options, data_filename: data_filename) 
    # return the URL of the file in the cloud
    return html
end

#zyte_snapshot(url, api_key:, options:, data_filename:, dropbox_folder: nil, retry_times: 3) ⇒ Object

create a file in the cloud with the HTML of a page downloaded by Zyte. return the URL of the file.

Parameters:

  • url: the URL of the page to download.

  • api_key: the Zyte API key.

  • options: the options to pass to Zyte.

  • dropbox_folder: the folder in the cloud where to store the file. If nil, it will use the self.desc value.

  • retry_times: the number of times to retry the download until the HTML is valid.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/functions.rb', line 281

def zyte_snapshot(url, api_key:, options:, data_filename:, dropbox_folder:nil, retry_times: 3)
    # "The garbage character must be due to the 520 error code which was caused on the second request."
    garbage = "\x9E\xE9e"
    
    ret = nil
    raise "Either dropbox_folder parameter or self.desc['id_account'] are required." if dropbox_folder.nil? && self.desc['id_account'].nil?
    dropbox_folder = self.desc['id_account'] if dropbox_folder.nil?
    # build path to the local file in /tmp
    id = SecureRandom.uuid
    filename = "#{id}.html"
    tmp_path = "/tmp/#{filename}"
    # build path to the file in the cloud
    year = Time.now.year.to_s.rjust(4,'0')
    month = Time.now.month.to_s.rjust(2,'0')
    dropbox_folder = "/massprospecting.bots/#{dropbox_folder}.#{year}.#{month}"
    dropbox_path = "#{dropbox_folder}/#{filename}"
    # getting the HTML - Retry mechanism
    zyte = ZyteClient.new(key: api_key)
    try = 0
    html = garbage
    while try < retry_times && html == garbage
        html = zyte.extract(url: url, options: options, data_filename: data_filename) 
        try += 1
    end
    # save the HTML in the local file in /tmp
    File.open(tmp_path, 'w') { |file| file.write(html) }
    # create the folder in the cloud and upload the file
    BlackStack::DropBox.dropbox_create_folder(dropbox_folder)
    BlackStack::DropBox.dropbox_upload_file(tmp_path, dropbox_path)
    # delete the local file
    File.delete(tmp_path)
    # return the URL of the file in the cloud
    return BlackStack::DropBox.get_file_url(dropbox_path)
end