dalia_api_survey_platform 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +18 -0
- data/Rakefile +11 -0
- data/dalia_api_survey_platform.gemspec +28 -0
- data/etc/fake_responses/create_query.json +23 -0
- data/etc/fake_responses/fetch_completion.json +17 -0
- data/etc/fake_responses/fetch_completions.csv +8 -0
- data/etc/fake_responses/fetch_completions.json +49 -0
- data/etc/fake_responses/fetch_survey.json +65 -0
- data/etc/fake_responses/fetch_survey_price.json +115 -0
- data/etc/fake_responses/fetch_surveys.json +56 -0
- data/etc/fake_responses/send_survey.json +24 -0
- data/etc/fake_responses/update_survey.json +23 -0
- data/lib/dalia_api_survey_platform.rb +15 -0
- data/lib/dalia_api_survey_platform/client.rb +136 -0
- data/lib/dalia_api_survey_platform/exception.rb +2 -0
- data/lib/dalia_api_survey_platform/http_client.rb +4 -0
- data/lib/dalia_api_survey_platform/json_extension.rb +5 -0
- data/lib/dalia_api_survey_platform/log.rb +41 -0
- data/lib/dalia_api_survey_platform/mock_client.rb +79 -0
- data/lib/dalia_api_survey_platform/version.rb +7 -0
- data/test/client_test.rb +138 -0
- data/test/fixtures/fake_responses/create_query.json +23 -0
- data/test/fixtures/fake_responses/fetch_completion.json +10 -0
- data/test/fixtures/fake_responses/fetch_completions.csv +8 -0
- data/test/fixtures/fake_responses/fetch_completions.json +28 -0
- data/test/fixtures/fake_responses/fetch_survey.json +64 -0
- data/test/fixtures/fake_responses/fetch_survey_price.json +115 -0
- data/test/fixtures/fake_responses/fetch_surveys.json +56 -0
- data/test/fixtures/fake_responses/send_survey.json +23 -0
- data/test/mock_client_test.rb +63 -0
- data/test/test_helper.rb +11 -0
- metadata +174 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"survey": {
|
3
|
+
"id": "001",
|
4
|
+
"title": "The survey 001",
|
5
|
+
"tagline": "This is an explanation of the survey",
|
6
|
+
"prequalification": "This survey require you to be a sport man older than 40",
|
7
|
+
"kind": "mr_survey",
|
8
|
+
"published": false,
|
9
|
+
"cap_completions": 10,
|
10
|
+
"credits": {
|
11
|
+
"kind": "stars",
|
12
|
+
"amount": "280"
|
13
|
+
},
|
14
|
+
"questions": [
|
15
|
+
{
|
16
|
+
"id": "Qa01e47",
|
17
|
+
"kind": "page",
|
18
|
+
"body": {
|
19
|
+
"page": "The page"
|
20
|
+
}
|
21
|
+
}
|
22
|
+
]
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"survey": {
|
3
|
+
"id": "001",
|
4
|
+
"title": "The survey 001",
|
5
|
+
"tagline": "This is an explanation of the survey",
|
6
|
+
"prequalification": "This survey require you to be a sport man older than 40",
|
7
|
+
"kind": "mr_survey",
|
8
|
+
"published": false,
|
9
|
+
"credits": {
|
10
|
+
"kind": "stars",
|
11
|
+
"amount": "280"
|
12
|
+
},
|
13
|
+
"questions": [
|
14
|
+
{
|
15
|
+
"id": "Qa01e47",
|
16
|
+
"kind": "page",
|
17
|
+
"body": {
|
18
|
+
"page": "The page"
|
19
|
+
}
|
20
|
+
}
|
21
|
+
]
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "ostruct"
|
3
|
+
require "json"
|
4
|
+
require "recursive_open_struct"
|
5
|
+
|
6
|
+
require_relative "dalia_api_survey_platform/version"
|
7
|
+
require_relative "dalia_api_survey_platform/http_client"
|
8
|
+
require_relative "dalia_api_survey_platform/json_extension"
|
9
|
+
require_relative "dalia_api_survey_platform/log"
|
10
|
+
require_relative "dalia_api_survey_platform/exception"
|
11
|
+
require_relative "dalia_api_survey_platform/client"
|
12
|
+
require_relative "dalia_api_survey_platform/mock_client"
|
13
|
+
|
14
|
+
module Dalia::Api::SurveyPlatform
|
15
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
class Dalia::Api::SurveyPlatform::Client
|
2
|
+
attr_accessor :options
|
3
|
+
attr_reader :log
|
4
|
+
attr_reader :response
|
5
|
+
|
6
|
+
def initialize(opts = {})
|
7
|
+
@options = {
|
8
|
+
:debug_mode => false,
|
9
|
+
:api_host => "http://daliaresearch.com"
|
10
|
+
}.merge!(opts)
|
11
|
+
|
12
|
+
@log = Dalia::Api::SurveyPlatform::Log.new(options[:debug_mode])
|
13
|
+
|
14
|
+
log.log_options(options)
|
15
|
+
|
16
|
+
check_required_options(options, :api_host, :account_id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def fetch_surveys
|
20
|
+
make_request_fetch_surveys({})
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch_survey(opts)
|
24
|
+
check_required_options(opts, :survey_id)
|
25
|
+
make_request_fetch_survey(opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def send_survey(opts)
|
29
|
+
check_required_options(opts, :data)
|
30
|
+
make_request_send_survey(opts)
|
31
|
+
end
|
32
|
+
|
33
|
+
def update_survey(opts)
|
34
|
+
check_required_options(opts, :survey_id, :data)
|
35
|
+
make_request_update_survey(opts)
|
36
|
+
end
|
37
|
+
|
38
|
+
def fetch_completions(opts)
|
39
|
+
check_required_options(opts, :survey_id)
|
40
|
+
make_request_fetch_completions(opts)
|
41
|
+
end
|
42
|
+
|
43
|
+
def fetch_completions_csv(opts)
|
44
|
+
check_required_options(opts, :survey_id)
|
45
|
+
make_request_fetch_completions_csv(opts)
|
46
|
+
end
|
47
|
+
|
48
|
+
def fetch_completion(opts)
|
49
|
+
check_required_options(opts, :survey_id, :completion_id)
|
50
|
+
make_request_fetch_completion(opts)
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch_survey_price(opts)
|
54
|
+
check_required_options(opts)
|
55
|
+
make_request_fetch_survey_price(opts)
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_query(opts)
|
59
|
+
check_required_options(opts, :survey_id, :question_id)
|
60
|
+
make_request_create_query(opts)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def make_request_fetch_surveys(query)
|
66
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys", query)
|
67
|
+
end
|
68
|
+
|
69
|
+
def make_request_fetch_survey(query)
|
70
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}", query)
|
71
|
+
end
|
72
|
+
|
73
|
+
def make_request_send_survey(query)
|
74
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/", query, :method => :post)
|
75
|
+
end
|
76
|
+
|
77
|
+
def make_request_update_survey(query)
|
78
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}", query, :method => :put)
|
79
|
+
end
|
80
|
+
|
81
|
+
def make_request_fetch_completions(query)
|
82
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}/completions", query)
|
83
|
+
end
|
84
|
+
|
85
|
+
def make_request_fetch_completions_csv(query)
|
86
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}/completions/index_to_csv", query)
|
87
|
+
end
|
88
|
+
|
89
|
+
def make_request_fetch_completion(query)
|
90
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}/completions/#{query.delete(:completion_id)}", query)
|
91
|
+
end
|
92
|
+
|
93
|
+
def make_request_fetch_survey_price(query)
|
94
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/price", query, :method => :post)
|
95
|
+
end
|
96
|
+
|
97
|
+
def make_request_create_query(query)
|
98
|
+
make_request("/api/control_panel/control_panel_users/1/researcher_users/#{@options[:account_id]}/surveys/#{query.delete(:survey_id)}/query", query)
|
99
|
+
end
|
100
|
+
|
101
|
+
def make_request(api_path, query, opts = {})
|
102
|
+
opts[:method] ||= :get
|
103
|
+
|
104
|
+
api_url = "#{options[:api_host]}#{api_path}"
|
105
|
+
|
106
|
+
log.debug "api_url: #{api_url}"
|
107
|
+
log.debug "api_query: #{query.inspect}"
|
108
|
+
|
109
|
+
response =
|
110
|
+
case opts[:method]
|
111
|
+
when :get then Dalia::Api::SurveyPlatform::HTTPClient.get(api_url, :query => query)
|
112
|
+
when :post then Dalia::Api::SurveyPlatform::HTTPClient.post(api_url, :body => query)
|
113
|
+
when :put then Dalia::Api::SurveyPlatform::HTTPClient.put(api_url, :body => query)
|
114
|
+
end
|
115
|
+
|
116
|
+
log.log_response response
|
117
|
+
|
118
|
+
raise Dalia::Api::SurveyPlatform::Exception, response.message if response.code != 200
|
119
|
+
|
120
|
+
if response.content_type == "application/json"
|
121
|
+
JSON.parse_sym(response.body)
|
122
|
+
else
|
123
|
+
response.body
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def check_required_options(options_hash, *required_options)
|
128
|
+
errors =
|
129
|
+
required_options.map do |required_option|
|
130
|
+
"#{required_option} required" if !options_hash[required_option]
|
131
|
+
end.compact.join(", ")
|
132
|
+
|
133
|
+
raise Dalia::Api::SurveyPlatform::Exception, errors if !errors.empty?
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Dalia::Api::SurveyPlatform::Log
|
2
|
+
attr_reader :debug_mode
|
3
|
+
|
4
|
+
def initialize(debug_mode = true)
|
5
|
+
@debug_mode = debug_mode
|
6
|
+
end
|
7
|
+
|
8
|
+
def debug(message)
|
9
|
+
return unless debug_mode
|
10
|
+
|
11
|
+
result = "Dalia::Api::SurveyPlatform [#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}]: #{message}"
|
12
|
+
|
13
|
+
if defined? ::Rails
|
14
|
+
::Rails.logger.info result
|
15
|
+
else
|
16
|
+
Kernel.puts result
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def log_options(options)
|
21
|
+
debug "Options:"
|
22
|
+
debug "-----------"
|
23
|
+
options.each { |k,v| debug "#{k}: #{v}" }
|
24
|
+
end
|
25
|
+
|
26
|
+
def log_response(response)
|
27
|
+
if response.content_type == "application/json"
|
28
|
+
body = JSON.pretty_generate(JSON.parse_sym(response.body))
|
29
|
+
else
|
30
|
+
body = response.body
|
31
|
+
end
|
32
|
+
|
33
|
+
debug "Response:"
|
34
|
+
debug "-----------"
|
35
|
+
debug "response.request: #{response.request.last_uri}"
|
36
|
+
debug "response.body: #{body}"
|
37
|
+
debug "response.content_type: #{response.content_type}"
|
38
|
+
debug "response.code: #{response.code}"
|
39
|
+
debug "response.message: #{response.message}"
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
class Dalia::Api::SurveyPlatform::MockClient < Dalia::Api::SurveyPlatform::Client
|
2
|
+
def initialize(opts = {})
|
3
|
+
opts = {
|
4
|
+
:account_id => "ACCOUNT_ID"
|
5
|
+
}.merge(opts)
|
6
|
+
|
7
|
+
super(opts)
|
8
|
+
|
9
|
+
log.debug "Initialize MockClient"
|
10
|
+
end
|
11
|
+
|
12
|
+
def make_request_fetch_surveys(query)
|
13
|
+
log.debug "make_request_fetch_surveys FAKE"
|
14
|
+
response("fetch_surveys.json")
|
15
|
+
end
|
16
|
+
|
17
|
+
def make_request_fetch_survey(query)
|
18
|
+
log.debug "make_request_fetch_survey FAKE"
|
19
|
+
response("fetch_survey.json")
|
20
|
+
end
|
21
|
+
|
22
|
+
def make_request_send_survey(query)
|
23
|
+
log.debug "make_request_send_survey FAKE"
|
24
|
+
response("send_survey.json")
|
25
|
+
end
|
26
|
+
|
27
|
+
def make_request_update_survey(query)
|
28
|
+
log.debug "make_request_update_survey FAKE"
|
29
|
+
response("update_survey.json")
|
30
|
+
end
|
31
|
+
|
32
|
+
def make_request_fetch_completions(query)
|
33
|
+
log.debug "make_request_fetch_completions FAKE"
|
34
|
+
response("fetch_completions.json")
|
35
|
+
end
|
36
|
+
|
37
|
+
def make_request_fetch_completions_csv(query)
|
38
|
+
log.debug "make_request_fetch_completions_csv FAKE"
|
39
|
+
response("fetch_completions.csv")
|
40
|
+
end
|
41
|
+
|
42
|
+
def make_request_fetch_completion(query)
|
43
|
+
log.debug "make_request_fetch_completion FAKE"
|
44
|
+
response("fetch_completion.json")
|
45
|
+
end
|
46
|
+
|
47
|
+
def make_request_fetch_survey_price(query)
|
48
|
+
log.debug "make_request_fetch_survey_price FAKE"
|
49
|
+
response("fetch_survey_price.json")
|
50
|
+
end
|
51
|
+
|
52
|
+
def make_request_create_query(query)
|
53
|
+
log.debug "make_request_create_query FAKE"
|
54
|
+
response("create_query.json")
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def response(file_response)
|
60
|
+
response =
|
61
|
+
RecursiveOpenStruct.new(
|
62
|
+
:body => File.read("#{File.dirname(__FILE__)}/../../etc/fake_responses/#{file_response}"),
|
63
|
+
:code => 200,
|
64
|
+
:content_type => file_response.match(/\.json$/) ? "application/json" : "text/csv",
|
65
|
+
:message => "<message>",
|
66
|
+
:request => {
|
67
|
+
:last_url => "<last_url>"
|
68
|
+
}
|
69
|
+
)
|
70
|
+
|
71
|
+
log.log_response response
|
72
|
+
|
73
|
+
if response.content_type == "application/json"
|
74
|
+
JSON.parse_sym(response.body)
|
75
|
+
else
|
76
|
+
response.body
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/test/client_test.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class ClientTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeWeb.allow_net_connect = false
|
6
|
+
@client_opts = { :account_id => "RESEARCHER_ACCOUNT_UUID" }
|
7
|
+
|
8
|
+
@client = Dalia::Api::SurveyPlatform::Client.new @client_opts
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_fetch_surveys
|
12
|
+
FakeWeb.register_uri(
|
13
|
+
:get,
|
14
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys",
|
15
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_surveys.json"),
|
16
|
+
:content_type => "application/json",
|
17
|
+
:status => ["200", "Success"]
|
18
|
+
)
|
19
|
+
|
20
|
+
response = @client.fetch_surveys
|
21
|
+
|
22
|
+
assert_equal(3, response[:surveys].length)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_fetch_survey
|
26
|
+
FakeWeb.register_uri(
|
27
|
+
:get,
|
28
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID",
|
29
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_survey.json"),
|
30
|
+
:content_type => "application/json",
|
31
|
+
:status => ["200", "Success"]
|
32
|
+
)
|
33
|
+
|
34
|
+
response = @client.fetch_survey(:survey_id => "SURVEY_UUID")
|
35
|
+
|
36
|
+
assert_equal(4, response[:survey][:questions].length)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_send_survey
|
40
|
+
FakeWeb.register_uri(
|
41
|
+
:post,
|
42
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/",
|
43
|
+
:body => File.read("#{FIXTURES}/fake_responses/send_survey.json"),
|
44
|
+
:content_type => "application/json",
|
45
|
+
:parameters => { :data => "DATA" },
|
46
|
+
:status => ["200", "Success"]
|
47
|
+
)
|
48
|
+
|
49
|
+
response = @client.send_survey(:data => "DATA")
|
50
|
+
|
51
|
+
assert_equal("280", response[:survey][:credits][:amount])
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_update_survey
|
55
|
+
FakeWeb.register_uri(
|
56
|
+
:put,
|
57
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID",
|
58
|
+
:body => File.read("#{FIXTURES}/fake_responses/send_survey.json"),
|
59
|
+
:content_type => "application/json",
|
60
|
+
:parameters => { :survey_id => "SURVEY_UUID", :data => "DATA" },
|
61
|
+
:status => ["200", "Success"]
|
62
|
+
)
|
63
|
+
|
64
|
+
response = @client.update_survey(:survey_id => "SURVEY_UUID", :data => "DATA")
|
65
|
+
|
66
|
+
assert_equal("280", response[:survey][:credits][:amount])
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_fetch_completions
|
70
|
+
FakeWeb.register_uri(
|
71
|
+
:get,
|
72
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions",
|
73
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_completions.json"),
|
74
|
+
:content_type => "application/json",
|
75
|
+
:status => ["200", "Success"]
|
76
|
+
)
|
77
|
+
|
78
|
+
response = @client.fetch_completions(:survey_id => "SURVEY_UUID")
|
79
|
+
|
80
|
+
assert_equal(3, response[:completions].length)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_fetch_completions_csv
|
84
|
+
FakeWeb.register_uri(
|
85
|
+
:get,
|
86
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions/index_to_csv",
|
87
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_completions.csv"),
|
88
|
+
:content_type => "text/csv",
|
89
|
+
:status => ["200", "Success"]
|
90
|
+
)
|
91
|
+
|
92
|
+
response = @client.fetch_completions_csv(:survey_id => "SURVEY_UUID")
|
93
|
+
|
94
|
+
assert_equal(8, response.lines.length)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_fetch_completion
|
98
|
+
FakeWeb.register_uri(
|
99
|
+
:get,
|
100
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions/COMPLETION_UUID",
|
101
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_completion.json"),
|
102
|
+
:content_type => "application/json",
|
103
|
+
:status => ["200", "Success"]
|
104
|
+
)
|
105
|
+
|
106
|
+
response = @client.fetch_completion(:survey_id => "SURVEY_UUID", :completion_id => "COMPLETION_UUID")
|
107
|
+
|
108
|
+
assert_equal("completed", response[:completion][:state])
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_fetch_survey_price
|
112
|
+
FakeWeb.register_uri(
|
113
|
+
:post,
|
114
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/price",
|
115
|
+
:body => File.read("#{FIXTURES}/fake_responses/fetch_survey_price.json"),
|
116
|
+
:content_type => "application/json",
|
117
|
+
:status => ["200", "Success"]
|
118
|
+
)
|
119
|
+
|
120
|
+
response = @client.fetch_survey_price(:account_id => "RESEARCHER_ACCOUNT_UUID")
|
121
|
+
|
122
|
+
assert_equal(1.0, response[:price][:total])
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_create_query
|
126
|
+
FakeWeb.register_uri(
|
127
|
+
:get,
|
128
|
+
"http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/query?question_id=QUESTION_UUID",
|
129
|
+
:body => File.read("#{FIXTURES}/fake_responses/create_query.json"),
|
130
|
+
:content_type => "application/json",
|
131
|
+
:status => ["200", "Success"]
|
132
|
+
)
|
133
|
+
|
134
|
+
response = @client.create_query(:survey_id => "SURVEY_UUID", :question_id => "QUESTION_UUID")
|
135
|
+
|
136
|
+
assert_equal("Qb5faf2", response[:question_id])
|
137
|
+
end
|
138
|
+
end
|