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(field:) ⇒ Object
-
.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(page:, limit:, filters: {}) ⇒ Object
Get array of hash descriptor of profile.
-
.update(desc) ⇒ Object
Submit a hash descriptor to the server for an update.
-
.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.
-
#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.
99 100 101 |
# File 'lib/functions.rb', line 99 def initialize(h) self.desc = h end |
Instance Attribute Details
#desc ⇒ Object
object json descriptor
93 94 95 |
# File 'lib/functions.rb', line 93 def desc @desc end |
Class Method Details
.account_value(field:) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/functions.rb', line 109 def self.account_value(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 |
.count ⇒ Object
Get array of hash descriptors of profiles.
Parameters:
-
id: guid. Id of the profile to bring.
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/functions.rb', line 150 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.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/functions.rb', line 166 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
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/functions.rb', line 200 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
95 96 97 |
# File 'lib/functions.rb', line 95 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.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/functions.rb', line 129 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
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/functions.rb', line 180 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 |
.upsert(desc) ⇒ Object
Submit a hash descriptor to the server for an upsert
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/functions.rb', line 215 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 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.
105 106 107 |
# File 'lib/functions.rb', line 105 def child_class_instance return self end |
#update ⇒ Object
Submit a hash descriptor to the server for an update
194 195 196 |
# File 'lib/functions.rb', line 194 def update self.class.update(self.desc) end |
#upsert ⇒ Object
Submit a hash descriptor to the server for an upsert
229 230 231 |
# File 'lib/functions.rb', line 229 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.
241 242 243 244 245 246 247 248 |
# File 'lib/functions.rb', line 241 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.
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 285 286 287 288 289 290 291 292 293 |
# File 'lib/functions.rb', line 260 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 |