diff --git a/backend/Gemfile b/backend/Gemfile index f2a3faf61..c2ad1a783 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -65,6 +65,8 @@ gem 'kaminari-mongoid' gem 'kaminari-actionview' gem 'rack-cors', require: 'rack/cors' +gem 'httparty' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index 51d862f0a..1420b1013 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -114,6 +114,8 @@ GEM activerecord (>= 4.2.0, < 4.3) hashdiff (0.3.0) hashie (3.4.3) + httparty (0.16.0) + multi_xml (>= 0.5.2) httpclient (2.7.1) i18n (0.7.0) i18n_data (0.7.0) @@ -329,6 +331,7 @@ DEPENDENCIES foreman geocoder globalize (~> 5.0.0) + httparty kaminari-actionview kaminari-mongoid letter_opener (~> 1.4) diff --git a/backend/app/controllers/api/v1/checkins_controller.rb b/backend/app/controllers/api/v1/checkins_controller.rb index 3b07acf7a..a50dff21b 100644 --- a/backend/app/controllers/api/v1/checkins_controller.rb +++ b/backend/app/controllers/api/v1/checkins_controller.rb @@ -17,6 +17,11 @@ def show def create date = params.require(:checkin).require(:date) checkin = Checkin::Creator.new(current_user.id, Date.parse(date)).create! + + SendDataToSendy.perform_async(name: current_user.screen_name, + email: current_user.email, + last_checkin_at: checkin.date) + render json: checkin end diff --git a/backend/app/jobs/send_data_to_sendy.rb b/backend/app/jobs/send_data_to_sendy.rb new file mode 100644 index 000000000..77d9d9efc --- /dev/null +++ b/backend/app/jobs/send_data_to_sendy.rb @@ -0,0 +1,18 @@ +class SendDataToSendy + include Sidekiq::Worker + + def perform(options) + email = options['email'] + return unless email.present? + + user = User.find_by(email: email) + return unless user + + last_checkin_at = options['last_checkin_at'] || '' + + SendyService.new(name: options['name'], + email: email, + signup_at: user.created_at.to_date, + last_checkin_at: last_checkin_at).send_data + end +end diff --git a/backend/app/models/registration.rb b/backend/app/models/registration.rb index 8becdb54a..bf4632228 100644 --- a/backend/app/models/registration.rb +++ b/backend/app/models/registration.rb @@ -23,6 +23,8 @@ def save! begin @user = User.create!(@user_params) @user.profile.update_attributes!(screen_name: screen_name) if screen_name.present? + + SendDataToSendy.perform_async(name: screen_name, email: @user.email) rescue ActiveRecord::RecordInvalid => e self.errors = e.record.errors raise ActiveRecord::RecordInvalid, self diff --git a/backend/app/services/sendy_service.rb b/backend/app/services/sendy_service.rb new file mode 100644 index 000000000..f48679428 --- /dev/null +++ b/backend/app/services/sendy_service.rb @@ -0,0 +1,55 @@ +require 'httparty' + +class SendyService + attr_accessor :api_key, :email, :name, :signup_at, :last_checkin_at + + # Default API endpoints + BASE_URL = ENV['SENDI_INSTALLATION'] + LIST_ID = ENV['SENDI_LIST_ID'] + + def initialize(options) + @api_key = ENV['SENDI_API_KEY'] + @email = options[:email] + @name = options[:name] + @signup_at = options[:signup_at] + @last_checkin_at = options[:last_checkin_at] + end + + def send_data + post('subscribe', name: name, + email: email, + signup_at: signup_at, + last_checkin_at: last_checkin_at, + boolean: true, api_key: api_key, list: LIST_ID) + end + + def get(path) + execute :get, path, nil + end + + def post(path, data = nil) + execute :post, path, data + end + + def execute(method, path, data = nil) + response = request(method, path, data) + + case response.code + when 200..299 + response + when 401 + raise AuthenticationFailed, response["message"] + when 404 + raise NotFoundError, response + else + raise RequestError, response + end + end + + def request(method, path, data = nil) + headers = {} + headers['content-type'] = "application/x-www-form-urlencoded" + + HTTParty.send(method, BASE_URL + path, headers: headers, body: data) + end +end