Class: GoogleScrapeRequestWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/google_scrape_request_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#restaurantObject

Returns the value of attribute restaurant.



5
6
7
# File 'app/workers/google_scrape_request_worker.rb', line 5

def restaurant
  @restaurant
end

Instance Method Details

#perform(restaurant_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/workers/google_scrape_request_worker.rb', line 7

def perform(restaurant_id)
  return unless Figaro.bool_env!('SIDEKIQ_ASK_GOOGLE_TO_SCRAPE_RESTAURANT_PAGE')

  self.restaurant = Restaurant.fetch(restaurant_id)

  return if locked?

  client_secrets_path = Figaro.env.GOOGLE_APPLICATION_CREDENTIALS!
  authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open(client_secrets_path),
                                                                  scope: 'https://www.googleapis.com/auth/indexing')
  authorizer.fetch_access_token!

  service = Google::Apis::IndexingV3::IndexingService.new
  service.authorization = authorizer

  url_notification_object = Google::Apis::IndexingV3::UrlNotification.new
  url_notification_object.url = restaurant.decorate.canonical_link
  url_notification_object.type = 'URL_UPDATED'

  service.publish_url_notification(url_notification_object)
  lock
rescue Google::Apis::RateLimitError
  lock
  true
end