Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -329,6 +331,7 @@ DEPENDENCIES
foreman
geocoder
globalize (~> 5.0.0)
httparty
kaminari-actionview
kaminari-mongoid
letter_opener (~> 1.4)
Expand Down
5 changes: 5 additions & 0 deletions backend/app/controllers/api/v1/checkins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions backend/app/jobs/send_data_to_sendy.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions backend/app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions backend/app/services/sendy_service.rb
Original file line number Diff line number Diff line change
@@ -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