ifd_tools 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/app/views/api/batch_track.builder +8 -0
- data/app/views/api/create_customer.builder +18 -0
- data/lib/generators/ifd_tools/install/templates/controllers/api_controller.rb +25 -0
- data/lib/ifd_tools/api_controller/customers.rb +44 -0
- data/lib/ifd_tools/api_controller/tracking.rb +43 -0
- data/lib/ifd_tools/api_controller_extensions.rb +2 -0
- data/lib/ifd_tools/version.rb +1 -1
- metadata +5 -2
data/README.md
CHANGED
@@ -25,4 +25,5 @@ To install:
|
|
25
25
|
1. There are several configuration options for color choices in the `ifd_tools.rb` initializer.
|
26
26
|
2. To track specific events, create subclasses of `IfdTools::Tracking::Event` in your application, in `app/models/ifd_tools/tracking` folder.
|
27
27
|
3. Be sure to override the `assign_trackable_item_by_id` method per subclass (default implement is to raise an exception)
|
28
|
-
4. Tracking requests can be initiated via the API at `/api/track.xml?type=product_event&id=14`. The `type` parameter is the class name without the module prefixes, underscored -> "IfdTools::Tracking::ProductEvent" should be sent as "product_event".
|
28
|
+
4. Tracking requests can be initiated via the API at `/api/track.xml?type=product_event&id=14`. The `type` parameter is the class name without the module prefixes, underscored -> "IfdTools::Tracking::ProductEvent" should be sent as "product_event".
|
29
|
+
5. Also have "batch tracking" functionality available, which will accept JSON or XML in a request to process multiple entries. Each "event" is id, type and optionally timestamp (UNIX) or platform.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
xml.instruct!
|
2
|
+
xml.response do
|
3
|
+
xml.error { "validations failed" }
|
4
|
+
xml.failures do
|
5
|
+
@failures.each do |attribute, messages|
|
6
|
+
xml.attribute(key: attribute) do
|
7
|
+
messages.each do |error|
|
8
|
+
xml.message { xml.cdata! error }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
xml.validations do
|
14
|
+
@customer.errors.full_messages.uniq.each do |message|
|
15
|
+
xml.message { xml.cdata! message }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -4,5 +4,30 @@ class ApiController < ApplicationController
|
|
4
4
|
respond_to :json, :xml
|
5
5
|
|
6
6
|
include IfdTools::ApiControllerExtensions
|
7
|
+
|
8
|
+
# Use this block to initialize the given Customer instance
|
9
|
+
# The return value of this block will be assigned back to the
|
10
|
+
# @customer ivar, which is used to generate the HTTP response (validations failed, etc)
|
11
|
+
#
|
12
|
+
# The request must be to "/api/create_customer" and the params namespaced under "params[:customer]"
|
13
|
+
# If you have special params that will throw exception if passed to Customer.new(params[:customer]), for example params[:customer][:account_level]
|
14
|
+
# then you should move that special attribute outside of the :customer hash & process it separately
|
15
|
+
#
|
16
|
+
create_customer do |customer|
|
17
|
+
|
18
|
+
# Notes on parameter
|
19
|
+
#
|
20
|
+
# "customer" is initially set to this before it is passed into this block
|
21
|
+
# "customer" is also available as @customer
|
22
|
+
# customer = Customer.new params[:customer]
|
23
|
+
|
24
|
+
# Example request
|
25
|
+
# To make use of some parameter that's not directly an attribute, for example :account_level
|
26
|
+
# Just make sure that :account_level is outside of the params[:customer] hash
|
27
|
+
#
|
28
|
+
# customer.account_level = CustomerAccountLevel.find_by_abbreviation(params[:account_level])
|
29
|
+
|
30
|
+
customer
|
31
|
+
end
|
7
32
|
|
8
33
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module IfdTools
|
2
|
+
module ApiController
|
3
|
+
module Customers
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def create_customer
|
8
|
+
if params[:customer]
|
9
|
+
@customer = Customer.new params[:customer]
|
10
|
+
@customer = instance_exec @customer, &self.class.create_customer_block
|
11
|
+
if @customer.valid?
|
12
|
+
@customer.save
|
13
|
+
respond_to do |format|
|
14
|
+
format.json { render json: { success: "Registered" }, status: :ok }
|
15
|
+
format.xml { render xml: { success: "Registered" }, status: :ok }
|
16
|
+
end
|
17
|
+
else
|
18
|
+
respond_to do |format|
|
19
|
+
@failures = @customer.errors.keys.inject({}) { |failures, key| failures[key] = @customer.errors[key].uniq; failures }
|
20
|
+
format.json { render json: { error: "validations failed", validations: @customer.errors.full_messages.uniq, failures: @failures }, status: :unprocessable_entity }
|
21
|
+
format.xml { render status: :unprocessable_entity }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
respond_to do |format|
|
26
|
+
format.json { render json: { error: "Missing parameters" }, status: :unprocessable_entity }
|
27
|
+
format.xml { render xml: { error: "Missing parameters" }, status: :unprocessable_entity }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
included do
|
33
|
+
cattr_accessor :create_customer_block
|
34
|
+
end
|
35
|
+
|
36
|
+
module ClassMethods
|
37
|
+
def create_customer(&block)
|
38
|
+
self.create_customer_block = block
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -38,6 +38,49 @@ module IfdTools
|
|
38
38
|
render status: :unprocessable_entity
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
# Used to record events from the application, more than one at a time, by submitting a request via JSON or XML
|
43
|
+
#
|
44
|
+
# Example request stucture:
|
45
|
+
# events
|
46
|
+
# event
|
47
|
+
# type product_event
|
48
|
+
# id 14
|
49
|
+
# timestamp 1233452 (optional)
|
50
|
+
# platform mac (optional)
|
51
|
+
#
|
52
|
+
def batch_track
|
53
|
+
|
54
|
+
begin
|
55
|
+
|
56
|
+
@events = params[:events] || []
|
57
|
+
@events.map(&:symbolize_keys!)
|
58
|
+
@events.each do |event|
|
59
|
+
|
60
|
+
case event[:type]
|
61
|
+
when "application_event"
|
62
|
+
@event = IfdTools::Tracking::ApplicationEvent.new
|
63
|
+
|
64
|
+
else
|
65
|
+
@event = "ifd_tools/tracking/#{event[:type]}".camelize.constantize.new
|
66
|
+
@event.assign_trackable_item_by_id(event[:id])
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
@event.ip_address = request.remote_ip
|
71
|
+
@event.customer = current_customer
|
72
|
+
@event.platform = event[:platform] || IfdTools::Tracking::Event.platform_for_request(request)
|
73
|
+
@event.timestamp = Time.at(event[:timestamp]) || Time.now
|
74
|
+
@event.save!
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
rescue Exception => e
|
79
|
+
@error = e.message
|
80
|
+
render status: :unprocessable_entity
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
41
84
|
|
42
85
|
end
|
43
86
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'ifd_tools/api_controller/emergency_messages'
|
2
2
|
require 'ifd_tools/api_controller/software_updates'
|
3
3
|
require 'ifd_tools/api_controller/notifier_updates'
|
4
|
+
require 'ifd_tools/api_controller/customers'
|
4
5
|
require 'ifd_tools/api_controller/contact_requests'
|
5
6
|
require 'ifd_tools/api_controller/tracking'
|
6
7
|
|
@@ -14,6 +15,7 @@ module IfdTools
|
|
14
15
|
include IfdTools::ApiController::NotifierUpdates
|
15
16
|
include IfdTools::ApiController::Tracking
|
16
17
|
include IfdTools::ApiController::ContactRequests
|
18
|
+
include IfdTools::ApiController::Customers
|
17
19
|
end
|
18
20
|
|
19
21
|
end
|
data/lib/ifd_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ifd_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -298,7 +298,9 @@ files:
|
|
298
298
|
- app/views/admin/shared/_footer.html.haml
|
299
299
|
- app/views/admin/software_updates/_form.html.haml
|
300
300
|
- app/views/admin/users/_form.html.haml
|
301
|
+
- app/views/api/batch_track.builder
|
301
302
|
- app/views/api/contact.builder
|
303
|
+
- app/views/api/create_customer.builder
|
302
304
|
- app/views/api/emergency_messages.builder
|
303
305
|
- app/views/api/notifier_updates.builder
|
304
306
|
- app/views/api/software_updates.builder
|
@@ -344,6 +346,7 @@ files:
|
|
344
346
|
- lib/ifd_tools/active_admin/base.rb
|
345
347
|
- lib/ifd_tools/active_admin/can_can/action_items.rb
|
346
348
|
- lib/ifd_tools/api_controller/contact_requests.rb
|
349
|
+
- lib/ifd_tools/api_controller/customers.rb
|
347
350
|
- lib/ifd_tools/api_controller/emergency_messages.rb
|
348
351
|
- lib/ifd_tools/api_controller/notifier_updates.rb
|
349
352
|
- lib/ifd_tools/api_controller/software_updates.rb
|