Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Farrar committed May 28, 2015
2 parents 3db802d + b21268f commit 730bd62
Show file tree
Hide file tree
Showing 52 changed files with 4,906 additions and 2,813 deletions.
Binary file added Interact_API_Guide.pdf
Binary file not shown.
36 changes: 36 additions & 0 deletions app/controllers/api/v1/list_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'responsys_api'
module Api
module V1
class ListController < ApplicationController
before_action :validate_params, only: [:create]

def create
list = Responsys::Api::Object::InteractObject.new(params[:folder], params[:list])
record_data = Responsys::Api::Object::RecordData.new(params[:record_data])
if params[:record_data].size > 150
render(status: :bad_request, json: { 'error_message': 'Maximum of 150 records exceeded.' })
else
response = Responsys::Api::Client.new.merge_list_members(list, record_data, Responsys::Api::Object::ListMergeRule.new(insertOnNoMatch: true))

if response[:status] == 'ok'
render json: json(response[:data][0][:result])
else
render json: json(response)
end
end
end

private

def validate_params
# rubocop:disable Lint/NonLocalExitFromIterator
required_params.each { |param| validate_param_exists(param) && return }
# rubocop:enable Lint/NonLocalExitFromIterator
end

def required_params
[:list, :folder, :record_data]
end
end
end
end
52 changes: 52 additions & 0 deletions app/controllers/api/v1/trigger_campaign_message_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'responsys_api'
module Api
module V1
class TriggerCampaignMessageController < ApplicationController
before_action :validate_params, only: [:create]

def create
list = Responsys::Api::Object::InteractObject.new(params[:folder], params[:list])
campaign = Responsys::Api::Object::InteractObject.new(params[:folder], params[:campaign])

recipients = []

if params[:recipients].size > 150
logger.error "recipients size = #{params[:recipients].size}"
render(status: :bad_request, json: json(error_message: 'Maximum of 150 recipients'))
return
end

params[:recipients].each do |r|
recipient_data = Responsys::Api::Object::RecipientData.new(Responsys::Api::Object::Recipient.new(emailAddress: r[:email], listName: list))
r[:optional_data].each do |k, v|
recipient_data.optional_data << Responsys::Api::Object::OptionalData.new(k, v)
end if r[:optional_data]
recipients << recipient_data
end

response = Responsys::Api::Client.new.trigger_message(campaign, recipients)
if (response[:status] == 'ok')
if response[:result]
render(json: json(response[:result]))
else
render(json: json(response[:data].map { |entry| entry[:result] }))
end
else
render(status: 500, json: json(error_code: response[:error][:code], error_message: response[:error][:message]))
end
end

private

def validate_params
# rubocop:disable Lint/NonLocalExitFromIterator
required_params.each { |param| validate_param_exists(param) && return }
# rubocop:enable Lint/NonLocalExitFromIterator
end

def required_params
[:list, :folder, :campaign, :recipients]
end
end
end
end
18 changes: 11 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session

rescue_from Exception do |exception|
json_request? ? render_error_json(exception.message) : fail(exception)
if json_request?
render_error_json(exception.message)
logger.error(exception)
else
fail(exception)
end
end

def json(data)
params[:pretty] ? JSON.pretty_generate(data) : data
end

def validate_param_exists(param)
render_error_json("Missing #{param} parameter") unless params.key?(param)
render_error_json("Missing '#{param}' parameter") unless params[param]
end

def render_error_json(message)
logger.error(message)
render(status: :bad_request, json: json_error(message))
end

Expand All @@ -26,11 +32,9 @@ def json_request?
end

def json_error(message)
result = { 'status' => 'failure',
'error' => {
'message' => message
}
}
result = {
'error_message' => message
}
json result
end
end
7 changes: 6 additions & 1 deletion app/services/service_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'sys/filesystem'

class ServiceStatus
attr_reader :name, :version, :hostname, :errors, :checks, :timestamp, :status
attr_reader :name, :version, :hostname, :errors, :checks, :timestamp

def initialize(name, version, boot_time)
@boot_time = boot_time
Expand All @@ -17,6 +17,11 @@ def initialize(name, version, boot_time)
@errors = []
end

def add_check(name, ok)
@checks << name
@errors << name unless ok
end

def add_http_get_check(name, url)
@checks << name
uri = URI(url)
Expand Down
115 changes: 115 additions & 0 deletions app/views/help/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,119 @@ Transfer-Encoding: chunked
</pre>



<h2><a name="trigger_campaign_message">TriggerCampaignMessage</a></h2>

<p>Trigger Campaign Message (send transactional email)</p>
<h3>URL Structure</h3>
<pre>
POST <%= api_v1_trigger_campaign_message_url %>
</pre>


<h3>Example</h3>

<pre class="terminal">
<code class="language-bash">
curl -X POST -i -L -H "Content-Type: application/json" <%= api_v1_trigger_campaign_message_url %>\?pretty\=true -d '
{
"list" : "Empty_Stock_Alerts_List",
"folder" : "TBP_Prog_Stock_Alert",
"campaign": "Stock_Alerts_Campaign",
"recipients": [
{
"email": "luke.farrar@thebookpeople.co.uk",
"optional_data" : {
"PRODUCT_TITLE": "title",
"AUTHOR": "author",
"PRICE": "123.45",
"FIRST_NAME": "Bob",
"PRODUCT_PAGE_URL": "moo.com",
"SAVING": "99.99",
"PRODUCT_IMAGE_URL": "moo.com/logo.png"
}
}
]
}
'
</code>
<code class="language-http">
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-TBP-Version: 0.1.1
Content-Type: application/json; charset=utf-8
ETag: W/"814c7637376a5f718c89c49046b5e5f8"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: f22766ca-39de-4bc6-9668-58fea65f3778
X-Runtime: 3.172549
Transfer-Encoding: chunked
</code>
<code class="language-javascript">

[
{
"recipient_id": "452639455",
"success": true,
"error_message": null
}
]
</code>
</pre>

<h2><a name="merge_list_members">MergeListMembers</a></h2>

<p>Merge List Members (add addresses to a list)</p>
<h3>URL Structure</h3>
<pre>
POST <%= api_v1_merge_list_members_url %>
</pre>

<h3>Example</h3>

<pre class="terminal">
<code class="language-bash">
curl -XPOST -i -L -H "Content-Type: application/json" <%= api_v1_merge_list_members_url %>\?pretty\=true -d '
{
"list" : "Empty_Stock_Alerts_List",
"folder" : "TBP_Prog_Stock_Alert",
"record_data": [
{
"EMAIL_ADDRESS_": "test@thebookpeople.co.uk",
"CUSTOMER_ID_": "9876543"
},
{
"EMAIL_ADDRESS_": "test2@thebookpeople.co.uk",
"CUSTOMER_ID_": "123456789"
}
]
}
'
</code>
<code class="language-http">
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-TBP-Version: 0.2.0
Content-Type: application/json; charset=utf-8
ETag: W/"8a76c27403cd87beb90698c4841a4e1a"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 7dbd404a-85a3-489a-9ea3-d8d14fb04878
X-Runtime: 2.163590
Transfer-Encoding: chunked
</code>
<code class="language-javascript">
{
"insert_count": "0",
"update_count": "2",
"rejected_count": "0",
"total_count": "2",
"error_message": null
}
</code>
</pre>


</section>
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Application < Rails::Application
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
#
config.app_version = '0.1.1'
config.app_version = '0.2.0'
config.boot_time = Time.now
config.action_dispatch.default_headers.merge!('X-TBP-Version' => config.app_version)

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace :v1 do
post '/search' => 'search#create'
post '/profile_extensions' => 'profile_extension#create'
post '/trigger_campaign_message' => 'trigger_campaign_message#create'
post '/merge_list_members' => 'list#create'
get '/status' => 'status#index'
end
end
Expand Down
Loading

0 comments on commit 730bd62

Please sign in to comment.