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
Returns a new instance of Base.
96 97 98 |
# File 'lib/functions.rb', line 96 def initialize(h) self.desc = h end |
Instance Attribute Details
#desc ⇒ Object
object json descriptor
90 91 92 |
# File 'lib/functions.rb', line 90 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.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/functions.rb', line 108 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.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/functions.rb', line 154 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.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/functions.rb', line 170 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
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/functions.rb', line 224 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
92 93 94 |
# File 'lib/functions.rb', line 92 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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/functions.rb', line 131 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
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/functions.rb', line 184 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
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/functions.rb', line 198 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
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/functions.rb', line 239 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.
102 103 104 |
# File 'lib/functions.rb', line 102 def child_class_instance return self end |
#update ⇒ Object
Submit a hash descriptor to the server for an update
212 213 214 |
# File 'lib/functions.rb', line 212 def update self.class.update(self.desc) end |
#update_status ⇒ Object
Submit a hash descriptor to the server for an update of status only
218 219 220 |
# File 'lib/functions.rb', line 218 def update_status self.class.update(self.desc) end |
#upsert ⇒ Object
Submit a hash descriptor to the server for an upsert
253 254 255 |
# File 'lib/functions.rb', line 253 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.
265 266 267 268 269 270 271 272 |
# File 'lib/functions.rb', line 265 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.
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 315 316 317 |
# File 'lib/functions.rb', line 284 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 |