poptart 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/lib/poptart.rb +1 -4
- data/lib/poptart/link.rb +9 -0
- data/lib/poptart/model.rb +13 -29
- data/lib/poptart/question.rb +21 -17
- data/lib/poptart/root.rb +10 -1
- data/lib/poptart/survey.rb +34 -7
- data/lib/poptart/survey_question.rb +32 -12
- data/lib/poptart/user.rb +4 -21
- data/lib/version.rb +1 -1
- data/poptart.gemspec +0 -4
- data/spec/lib/poptart/model_spec.rb +95 -0
- data/spec/lib/poptart/question_spec.rb +47 -0
- data/spec/lib/poptart/root_spec.rb +50 -0
- data/spec/lib/poptart/survey_question_spec.rb +166 -0
- data/spec/lib/poptart/survey_spec.rb +105 -0
- data/spec/lib/poptart/user_spec.rb +45 -0
- data/spec/requests/questions_spec.rb +31 -0
- data/spec/requests/survey_questions_spec.rb +150 -0
- data/spec/requests/surveys_spec.rb +34 -5
- data/spec/requests/{user_management_spec.rb → users_spec.rb} +5 -5
- data/spec/spec_helper.rb +1 -4
- data/spec/vcr/answering/survey_questions_answers_a_multiple_choice_question.yml +1527 -1596
- data/spec/vcr/answering/survey_questions_answers_a_survey_question.yml +2942 -2035
- data/spec/vcr/answering/survey_questions_creates_and_returns_a_random_question_survey.yml +254 -70
- data/spec/vcr/answering/survey_questions_creates_and_returns_an_empty_survey.yml +110 -48
- data/spec/vcr/answering/survey_questions_finds_survey_question_for_id.yml +1618 -286
- data/spec/vcr/poptart/survey/adds_a_question_to_a_survey.yml +1939 -0
- data/spec/vcr/poptart/survey/creates_a_random_survey.yml +318 -0
- data/spec/vcr/poptart/survey/creates_an_empty_survey.yml +702 -0
- data/spec/vcr/poptart/survey/returns_a_survey_by_id.yml +618 -0
- data/spec/vcr/poptart/user/creates_a_user.yml +179 -0
- data/spec/vcr/{user/management_creates_a_user.yml → poptart/user/returns_a_user.yml} +72 -69
- data/spec/vcr/questions/creates_a_question.yml +186 -0
- data/spec/vcr/questions/finds_a_question_by_id.yml +326 -0
- data/spec/vcr/questions/finds_a_question_by_key.yml +144 -0
- data/spec/vcr/questions/returns_all_answered_survey_questions_for_a_question.yml +1021 -0
- data/spec/vcr/retrieving/answers_returns_all_answered_survey_questions_for_a_question.yml +533 -479
- data/spec/vcr/survey/questions_answers_a_multiple_choice_question.yml +1454 -0
- data/spec/vcr/survey/questions_answers_a_survey_question.yml +1772 -0
- data/spec/vcr/survey/questions_creates_and_returns_an_empty_survey.yml +372 -0
- data/spec/vcr/survey/questions_finds_survey_question_for_id.yml +3083 -0
- data/spec/vcr/survey/questions_returns_all_answered_survey_questions_for_a_question.yml +5756 -0
- data/spec/vcr/survey/questions_returns_all_answered_survey_questions_for_a_question_by_key.yml +973 -0
- data/spec/vcr/survey/questions_returns_all_answered_survey_questions_for_a_survey.yml +620 -0
- metadata +55 -72
- data/lib/poptart/boolean_question.rb +0 -14
- data/lib/poptart/multiple_response_question.rb +0 -9
- data/lib/poptart/range_question.rb +0 -9
- data/lib/poptart/time_question.rb +0 -9
- data/spec/requests/answering_survey_questions_spec.rb +0 -79
- data/spec/requests/answers_spec.rb +0 -45
- data/spec/requests/creating_questions_spec.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 805d1e873fdd7fe9a06cdbd4f8753cc6d8900ac0
|
4
|
+
data.tar.gz: 7a7d7ee6954629aea9ae683b5c91598fa524603b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84a0a219316b81765ee869926ca6230b60af2890cb4975e493d86116610b78b213800ab08a85990e2f8921ee4847aa1c4e61b8aa6601ead3492642e74275e819
|
7
|
+
data.tar.gz: 9c5b3e0cc0fd80ba004ddca6dcbf22e8a07e5eab53e2ac949103b38dff69a357364c27ac45c87a3edf44d09e0ef2f00ff501780fb213b708d8484aba9d2dc7ed
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.1.
|
1
|
+
ruby-2.1.3
|
data/lib/poptart.rb
CHANGED
@@ -41,13 +41,10 @@ require "addressable/template"
|
|
41
41
|
|
42
42
|
require_relative 'version'
|
43
43
|
require_relative 'poptart/request'
|
44
|
+
require_relative 'poptart/link'
|
44
45
|
require_relative 'poptart/model'
|
45
46
|
require_relative 'poptart/root'
|
46
47
|
require_relative 'poptart/survey'
|
47
48
|
require_relative 'poptart/survey_question'
|
48
49
|
require_relative 'poptart/question'
|
49
|
-
require_relative 'poptart/boolean_question'
|
50
|
-
require_relative 'poptart/multiple_response_question'
|
51
|
-
require_relative 'poptart/range_question'
|
52
|
-
require_relative 'poptart/time_question'
|
53
50
|
require_relative 'poptart/user'
|
data/lib/poptart/link.rb
ADDED
data/lib/poptart/model.rb
CHANGED
@@ -13,7 +13,13 @@ module Poptart
|
|
13
13
|
end
|
14
14
|
|
15
15
|
@id = @params['id']
|
16
|
-
|
16
|
+
|
17
|
+
@links = []
|
18
|
+
if @params['_links']
|
19
|
+
@params['_links'].each do |link|
|
20
|
+
@links << Link.new(href: link['href'], rel: link['rel'], method: link['method'])
|
21
|
+
end
|
22
|
+
end
|
17
23
|
end
|
18
24
|
|
19
25
|
def self.root
|
@@ -26,38 +32,16 @@ module Poptart
|
|
26
32
|
Poptart::Model.root
|
27
33
|
end
|
28
34
|
|
29
|
-
def
|
30
|
-
|
31
|
-
template.
|
32
|
-
|
33
|
-
|
34
|
-
def surveys_url(id: nil, query: nil)
|
35
|
-
template = Addressable::Template.new(links.surveys.href)
|
36
|
-
template.expand(id: id, query: query).to_s
|
37
|
-
end
|
38
|
-
|
39
|
-
def questions_url(id: nil, query: nil)
|
40
|
-
template = Addressable::Template.new(links.questions.href)
|
41
|
-
template.expand(id: id, query: query).to_s
|
42
|
-
end
|
43
|
-
|
44
|
-
def survey_questions_url(question_id: nil, query: nil)
|
45
|
-
template = Addressable::Template.new(links.survey_questions.href)
|
46
|
-
template.expand(question_id: question_id, query: query).to_s
|
35
|
+
def url(relation: , method: 'GET', query: nil, survey_id: nil, id: nil)
|
36
|
+
link = find_link(relation, method)
|
37
|
+
template = Addressable::Template.new(link.href)
|
38
|
+
template.expand(survey_id: survey_id, id: id, query: query).to_s
|
47
39
|
end
|
48
40
|
|
49
41
|
private
|
50
42
|
|
51
|
-
def
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
def scheme
|
56
|
-
root_uri.scheme
|
57
|
-
end
|
58
|
-
|
59
|
-
def host
|
60
|
-
root_uri.port ? "#{root_uri.host}:#{root_uri.port}" : root_uri.host
|
43
|
+
def find_link(relation, method)
|
44
|
+
links.find { |link| link.rel == relation && link.method == method }
|
61
45
|
end
|
62
46
|
end
|
63
47
|
end
|
data/lib/poptart/question.rb
CHANGED
@@ -2,31 +2,39 @@ module Poptart
|
|
2
2
|
class Question < Model
|
3
3
|
extend Poptart::Request
|
4
4
|
|
5
|
-
attr_accessor :responses, :
|
5
|
+
attr_accessor :responses, :question_type, :key, :text
|
6
6
|
|
7
7
|
def initialize(response)
|
8
8
|
super
|
9
9
|
@responses = params['responses']
|
10
|
-
@freeform = params['freeform']
|
11
10
|
@question_type = params['question_type']
|
12
|
-
@
|
13
|
-
@parent_question_id = params['parent_question_id']
|
11
|
+
@text = params['text']
|
14
12
|
@key = params['key']
|
15
13
|
end
|
16
14
|
|
17
|
-
def self.create(text,
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
def self.create(text,
|
16
|
+
responses: [],
|
17
|
+
question_type: nil,
|
18
|
+
key: nil)
|
19
|
+
|
20
|
+
question_data = {
|
21
|
+
'question_type' => question_type,
|
22
|
+
'responses' => responses,
|
23
|
+
'text' => text,
|
24
|
+
'key' => key
|
25
|
+
}
|
26
|
+
url = root.url(relation: 'questions', method: 'POST')
|
27
|
+
response = post(url, 'question' => question_data)
|
28
|
+
new(response)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find(id_or_key)
|
32
|
+
response = get(root.url(relation: 'questions', id: id_or_key))
|
25
33
|
new(response)
|
26
34
|
end
|
27
35
|
|
28
36
|
def self.all(params = {})
|
29
|
-
response = get(root.
|
37
|
+
response = get(root.url(relation: 'questions'))
|
30
38
|
JSON.parse(response.body).map do |question|
|
31
39
|
if params[:type]
|
32
40
|
if question['question_type'] == params[:type]
|
@@ -37,9 +45,5 @@ module Poptart
|
|
37
45
|
end
|
38
46
|
end.compact
|
39
47
|
end
|
40
|
-
|
41
|
-
def freeform?
|
42
|
-
@freeform
|
43
|
-
end
|
44
48
|
end
|
45
49
|
end
|
data/lib/poptart/root.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
module Poptart
|
2
2
|
class Root < Model
|
3
|
-
|
3
|
+
def root_uri
|
4
|
+
@root_uri = URI.parse(root.links.self.href)
|
5
|
+
end
|
4
6
|
|
7
|
+
def scheme
|
8
|
+
root_uri.scheme
|
9
|
+
end
|
10
|
+
|
11
|
+
def host
|
12
|
+
root_uri.port ? "#{root_uri.host}:#{root_uri.port}" : root_uri.host
|
13
|
+
end
|
5
14
|
end
|
6
15
|
end
|
data/lib/poptart/survey.rb
CHANGED
@@ -8,19 +8,27 @@ module Poptart
|
|
8
8
|
@service_user_id = params['service_user_id']
|
9
9
|
@completed = params['completed']
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
BooleanQuestion.new(survey_question)
|
14
|
-
else
|
11
|
+
if params['survey_questions']
|
12
|
+
@survey_questions = params['survey_questions'].map do |survey_question|
|
15
13
|
SurveyQuestion.new(survey_question)
|
16
14
|
end
|
15
|
+
else
|
16
|
+
@survey_questions = []
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
20
|
+
def add_survey_question(survey_question)
|
21
|
+
url = url(relation: 'survey-questions', method: 'POST')
|
22
|
+
response = post(url, {
|
23
|
+
'survey_question' => {
|
24
|
+
'question_id' => survey_question.question_id,
|
25
|
+
'responses' => survey_question.responses
|
26
|
+
}
|
27
|
+
})
|
22
28
|
if response.status == 201
|
23
|
-
Poptart::SurveyQuestion.new(response)
|
29
|
+
survey_question = Poptart::SurveyQuestion.new(response)
|
30
|
+
survey_questions << survey_question
|
31
|
+
survey_question
|
24
32
|
end
|
25
33
|
end
|
26
34
|
|
@@ -33,5 +41,24 @@ module Poptart
|
|
33
41
|
def completed?
|
34
42
|
@completed
|
35
43
|
end
|
44
|
+
|
45
|
+
def self.create
|
46
|
+
url = root.url(relation: 'surveys', method: 'POST')
|
47
|
+
response = post(url)
|
48
|
+
Poptart::Survey.new(response)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.all
|
52
|
+
response = get(root.url(relation: 'surveys'))
|
53
|
+
JSON.parse(response.body)['surveys'].map do |survey|
|
54
|
+
Poptart::Survey.new(survey)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.find(id)
|
59
|
+
url = root.url(relation: 'surveys', id: id)
|
60
|
+
response = get(url)
|
61
|
+
Poptart::Survey.new(response)
|
62
|
+
end
|
36
63
|
end
|
37
64
|
end
|
@@ -3,17 +3,17 @@ module Poptart
|
|
3
3
|
extend Poptart::Request
|
4
4
|
include Poptart::Request
|
5
5
|
|
6
|
-
attr_accessor :text, :answer, :
|
6
|
+
attr_accessor :text, :answer, :question_type, :responses, :key, :question_id
|
7
7
|
|
8
8
|
def initialize(response)
|
9
9
|
super
|
10
10
|
@text = params['text']
|
11
|
-
@
|
12
|
-
@freeform = params['freeform']
|
11
|
+
@question_type = params['question_type']
|
13
12
|
@responses = params['responses']
|
14
13
|
@answer = params['answer']
|
15
14
|
@created_at = params['created_at']
|
16
15
|
@key = params['key']
|
16
|
+
@question_id = params['question_id']
|
17
17
|
|
18
18
|
if @answer == 't'
|
19
19
|
@answer = true
|
@@ -23,31 +23,51 @@ module Poptart
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def boolean?
|
26
|
-
|
26
|
+
question_type == "boolean"
|
27
27
|
end
|
28
28
|
|
29
29
|
def multiple?
|
30
|
-
|
30
|
+
question_type == "multiple"
|
31
31
|
end
|
32
32
|
|
33
33
|
def range?
|
34
|
-
|
34
|
+
question_type == "range"
|
35
35
|
end
|
36
36
|
|
37
37
|
def time?
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
def freeform?
|
42
|
-
@freeform == true
|
38
|
+
question_type == "time"
|
43
39
|
end
|
44
40
|
|
45
41
|
def created_at
|
46
42
|
DateTime.parse(@created_at)
|
47
43
|
end
|
48
44
|
|
45
|
+
def self.find_all(question_id: nil, key: nil, survey_id: nil)
|
46
|
+
if question_id
|
47
|
+
query = { question_id: question_id }
|
48
|
+
elsif key
|
49
|
+
query = { key: key }
|
50
|
+
elsif survey_id
|
51
|
+
query = { survey_id: survey_id }
|
52
|
+
end
|
53
|
+
|
54
|
+
url = root.url(relation: 'survey-questions-by-query', query: query)
|
55
|
+
response = get(url)
|
56
|
+
JSON.parse(response.body)["survey_questions"].map do |response_body|
|
57
|
+
Poptart::SurveyQuestion.new(response_body)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
49
61
|
def submit
|
50
|
-
|
62
|
+
url = url(method: 'PUT', relation: 'self')
|
63
|
+
response = put(url,
|
64
|
+
{
|
65
|
+
'id' => id,
|
66
|
+
'survey_question' => {
|
67
|
+
'answer' => answer
|
68
|
+
}
|
69
|
+
}
|
70
|
+
)
|
51
71
|
response.status == 204
|
52
72
|
end
|
53
73
|
end
|
data/lib/poptart/user.rb
CHANGED
@@ -11,33 +11,16 @@ module Poptart
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.create
|
14
|
-
|
14
|
+
url = root.url(relation: 'users', method: 'POST')
|
15
|
+
response = post(url)
|
15
16
|
Poptart::User.new(response)
|
16
17
|
end
|
17
18
|
|
18
|
-
def self.
|
19
|
-
|
20
|
-
response = get(url)
|
19
|
+
def self.get_user
|
20
|
+
response = get(root.url(relation: 'users'))
|
21
21
|
Poptart::User.new(response)
|
22
22
|
end
|
23
23
|
|
24
|
-
def create_survey
|
25
|
-
response = post(root.surveys_url, { survey: { service_user_id: service_user_id } })
|
26
|
-
Poptart::Survey.new(response)
|
27
|
-
end
|
28
|
-
|
29
|
-
def create_random_survey
|
30
|
-
url = root.surveys_url(query: {random: true})
|
31
|
-
response = post(url, { survey: { service_user_id: service_user_id } })
|
32
|
-
Poptart::Survey.new(response)
|
33
|
-
end
|
34
|
-
|
35
|
-
def survey_for_id(id)
|
36
|
-
url = root.surveys_url(id: id)
|
37
|
-
response = get(url)
|
38
|
-
Poptart::Survey.new(response)
|
39
|
-
end
|
40
|
-
|
41
24
|
def survey_questions_for_question_id(question_id)
|
42
25
|
url = root.survey_questions_url(question_id: question_id)
|
43
26
|
response = get(url)
|
data/lib/version.rb
CHANGED
data/poptart.gemspec
CHANGED
@@ -20,10 +20,6 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.add_dependency 'faraday'
|
21
21
|
gem.add_dependency 'addressable'
|
22
22
|
|
23
|
-
gem.add_development_dependency 'bourne', "1.5.0"
|
24
|
-
gem.add_development_dependency 'guard'
|
25
|
-
gem.add_development_dependency 'guard-rspec'
|
26
|
-
gem.add_development_dependency 'mocha', "~> 0.14.0"
|
27
23
|
gem.add_development_dependency 'pry'
|
28
24
|
gem.add_development_dependency 'rspec'
|
29
25
|
gem.add_development_dependency 'rake'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Poptart::Model do
|
4
|
+
class Poptart::TestModel < Poptart::Model; end
|
5
|
+
|
6
|
+
context '.new' do
|
7
|
+
context 'response is a hash' do
|
8
|
+
it 'sets id' do
|
9
|
+
response = { 'id' => 1 }
|
10
|
+
model = Poptart::TestModel.new(response)
|
11
|
+
expect(model.id).to eq(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'sets links' do
|
15
|
+
response = {
|
16
|
+
'_links' => [
|
17
|
+
{ 'rel' => 'self',
|
18
|
+
'href' => 'http://example.com'
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
22
|
+
model = Poptart::TestModel.new(response)
|
23
|
+
expect(model.links.count).to eq(1)
|
24
|
+
link = model.links.first
|
25
|
+
expect(link.rel).to eq('self')
|
26
|
+
expect(link.href).to eq('http://example.com')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'response is http response object' do
|
31
|
+
it 'sets id' do
|
32
|
+
response = double(:response, status: 200, body: { 'id' => 1, status: 200 }.to_json)
|
33
|
+
model = Poptart::TestModel.new(response)
|
34
|
+
expect(model.id).to eq(1)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'sets links' do
|
38
|
+
response = double(:response, status: 200, body: {
|
39
|
+
'_links' => [
|
40
|
+
{ 'rel' => 'self',
|
41
|
+
'href' => 'http://example.com'
|
42
|
+
}
|
43
|
+
]
|
44
|
+
}.to_json)
|
45
|
+
|
46
|
+
model = Poptart::TestModel.new(response)
|
47
|
+
expect(model.links.count).to eq(1)
|
48
|
+
link = model.links.first
|
49
|
+
expect(link.rel).to eq('self')
|
50
|
+
expect(link.href).to eq('http://example.com')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'invalid response' do
|
55
|
+
it 'raises unauthorized exception' do
|
56
|
+
response = double(:response, status: 401)
|
57
|
+
expect{
|
58
|
+
Poptart::TestModel.new(response)
|
59
|
+
}.to raise_exception('Unauthorized')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context '.root' do
|
65
|
+
it 'returns Root' do
|
66
|
+
root_response = double(:response)
|
67
|
+
root = double(:root)
|
68
|
+
allow(Poptart::Model).to receive(:get).with('/').and_return(root_response)
|
69
|
+
allow(Poptart::Root).to receive(:new).with(root_response).and_return(root)
|
70
|
+
|
71
|
+
returned_root = Poptart::TestModel.root
|
72
|
+
|
73
|
+
expect(Poptart::Model).to have_received(:get).with('/')
|
74
|
+
expect(Poptart::Root).to have_received(:new).with(root_response)
|
75
|
+
expect(returned_root).to eq(root)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context '#root' do
|
80
|
+
it 'returns root' do
|
81
|
+
root_response = double(:response)
|
82
|
+
root = double(:root)
|
83
|
+
allow(Poptart::Model).to receive(:get).with('/').and_return(root_response)
|
84
|
+
allow(Poptart::Root).to receive(:new).with(root_response).and_return(root)
|
85
|
+
|
86
|
+
model = Poptart::TestModel.new({})
|
87
|
+
returned_root = model.root
|
88
|
+
|
89
|
+
expect(Poptart::Model).to have_received(:get).with('/')
|
90
|
+
expect(Poptart::Root).to have_received(:new).with(root_response)
|
91
|
+
expect(returned_root).to eq(root)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|