Last active
February 29, 2016 14:08
-
-
Save stereosupersonic/cc57f4ae4c7452a42a39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Gemfile | |
gem "optivo_api", ">= 0.8.1", github: "freeletics/optivo_api" | |
#initilizer | |
#./config/initializers/optivo_api.rb | |
require "optivo_api" | |
OptivoApi.configure do |config| | |
config.mandator_id = Rails.application.secrets.optivo_api_mandator_id | |
config.user = Rails.application.secrets.optivo_api_user | |
config.password = Rails.application.secrets.optivo_api_password | |
config.logger = Rails.logger | |
config.log_level = Rails.env.production? ? :info : :debug | |
config.cache = Rails.cache | |
config.disabled = !Rails.env.production? && !Rails.env.staging? | |
end | |
module OptivoApi | |
def self.list | |
if Rails.env.production? | |
{ | |
newsletter_subscribers: "120199092218" # newsletter_subscribers_all_products | |
} | |
else | |
{ | |
newsletter_subscribers: "120840736429", # newsletter_subscribers_all_products_backup | |
} | |
end | |
end | |
# app/jobs/optivo_sync_job.rb | |
class OptivoSyncJob < ActiveJob::Base | |
queue_as "default" | |
def perform(fl_uid) | |
return unless Rails.env.production? | |
first_training = get_first_training # TODO | |
last_training = get_last_training # TODO | |
coach_week_date = get_coach_week_date # TODO | |
coach_week = get_coach_week # TODO | |
... | |
attributes = { | |
running_first_training_at: format_date(first_training.created_at), | |
running_last_training_at: format_date(last_training.created_at), | |
run_coach_week: coach_week, | |
run_coach_week_start_date: format_date(coach_week_date) | |
} | |
recipient_service.update( | |
list_id: OptivoApi.list[:newsletter_subscribers] | |
recipient_id: fl_uid, | |
attribute_names: attributes.keys.map(&:to_s), | |
attribute_values: attributes.values | |
) | |
rescue OptivoApi::RecipientNotInList | |
Rails.logger.error("fl_uid: #{fl_uid} not found in list: #{OptivoApi.list[:newsletter_subscribers]}") | |
rescue Net::ReadTimeout, Net::OpenTimeout | |
raise JobFrequentFailureError | |
end | |
private | |
def recipient_service | |
@recipient_service ||= OptivoApi::WebServices::Recipient.new | |
end | |
def format_date(date) | |
date.strftime("%Y-%m-%d") if date | |
end | |
end | |
# call the this after a user has finished a training | |
OptivoSyncJob.perform_later(training.fl_uid) # or so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment