Class: BlackStack::Base
- Inherits:
-
Object
- Object
- BlackStack::Base
- 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
-
#desc ⇒ Object
object json descriptor.
Class Method Summary collapse
-
.account_value(id_account: nil, field:) ⇒ Object
sysowner must provide the id_account for getting an account value.
-
.count ⇒ Object
Get array of hash descriptors of profiles.
-
.get(id) ⇒ Object
Get array of hash descriptors of profiles.
-
.insert(desc) ⇒ Object
Submit a hash descriptor to the server for an insert.
- .object_name ⇒ Object
-
.page(id_account: nil, promiscuous: false, page:, limit:, filters: {}) ⇒ Object
Get array of hash descriptor of profile.
-
.update(desc) ⇒ Object
Submit a hash descriptor to the server for an update.
-
.update_status(desc) ⇒ Object
Submit a hash descriptor to the server for an update of status only.
-
.upsert(desc) ⇒ Object
Submit a hash descriptor to the server for an upsert.
Instance Method Summary collapse
-
#child_class_instance ⇒ Object
Crate an instance of a child class using speicfications in the ‘desc` attribute.
-
#initialize(h) ⇒ Base
constructor
A new instance of Base.
-
#update ⇒ Object
Submit a hash descriptor to the server for an update.
-
#update_status ⇒ Object
Submit a hash descriptor to the server for an update of status only.
-
#upsert ⇒ Object
Submit a hash descriptor to the server for an upsert.
-
#zyte_html(url, api_key:, options:, data_filename:) ⇒ Object
return the HTML of a page downloaded by Zyte.
-
#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.
Constructor Details
#initialize(h) ⇒ Base
100 101 102 |
# File 'lib/functions.rb', line 100 def initialize(h) self.desc = h end |
Instance Attribute Details
#desc ⇒ Object
object json descriptor
94 95 96 |
# File 'lib/functions.rb', line 94 def desc @desc end |
Class Method Details
.account_value(id_account: nil, field:) ⇒ Object
sysowner must provide the id_account for getting an account value. for non sysowner it is assigned to his account.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/functions.rb', line 112 def self.account_value(id_account:nil, field:) params = {} params['id_account'] = id_account 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 |
.count ⇒ Object
Get array of hash descriptors of profiles.
Parameters:
-
id: guid. Id of the profile to bring.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/functions.rb', line 158 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.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/functions.rb', line 174 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
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/functions.rb', line 228 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_name ⇒ Object
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(id_account: nil, promiscuous: false, page:, limit:, filters: {}) ⇒ Object
Get array of hash descriptor of profile.
Parameters:
-
id_account: guid. Id of the account to bring the profiles. Sysowner must provide the id_account for getting an account value. For non sysowner it is assigned to his account.
-
promiscuous: boolean. It works only for Sysowner. If true, it will bring all non-deleted rows. If false, it will bring only rows matching id_profile. Default: false.
-
page: integer. Page number.
-
limit: integer. Number of profiles per page.
-
params: hash. Additional filter parameters used by the specific child class.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/functions.rb', line 135 def self.page(id_account: nil, promiscuous: false, page:, limit:, filters: {}) # add page and limit to the params params = {} params['id_account'] = id_account params['promiscuous'] = promiscuous 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
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/functions.rb', line 188 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
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/functions.rb', line 202 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
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/functions.rb', line 243 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_instance ⇒ Object
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 |
#update ⇒ Object
Submit a hash descriptor to the server for an update
216 217 218 |
# File 'lib/functions.rb', line 216 def update self.class.update(self.desc) end |
#update_status ⇒ Object
Submit a hash descriptor to the server for an update of status only
222 223 224 |
# File 'lib/functions.rb', line 222 def update_status self.class.update(self.desc) end |
#upsert ⇒ Object
Submit a hash descriptor to the server for an upsert
257 258 259 |
# File 'lib/functions.rb', line 257 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.
269 270 271 272 273 274 275 276 |
# File 'lib/functions.rb', line 269 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: , 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.
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 315 316 317 318 319 320 321 |
# File 'lib/functions.rb', line 288 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: , 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 |