poptart 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -3
- data/lib/poptart/model.rb +49 -1
- data/lib/poptart/question.rb +1 -2
- data/lib/poptart/request.rb +1 -1
- data/lib/poptart/root.rb +0 -5
- data/lib/poptart/survey.rb +5 -6
- data/lib/poptart/survey_question.rb +2 -1
- data/lib/poptart/user.rb +16 -12
- data/lib/poptart.rb +2 -1
- data/lib/version.rb +1 -1
- data/poptart.gemspec +1 -0
- data/spec/requests/answering_survey_questions_spec.rb +32 -9
- data/spec/requests/root_spec.rb +6 -3
- data/spec/requests/user_management_spec.rb +8 -8
- data/spec/vcr/answering/survey_questions_answers_a_multiple_choice_question.yml +5444 -274
- data/spec/vcr/answering/survey_questions_answers_a_survey_question.yml +10066 -450
- data/spec/vcr/answering/survey_questions_creates_and_returns_a_random_question_survey.yml +883 -69
- data/spec/vcr/answering/survey_questions_creates_and_returns_an_empty_survey.yml +665 -56
- data/spec/vcr/answering/survey_questions_finds_survey_question_for_id.yml +1072 -74
- data/spec/vcr/answering/survey_questions_returns_all_answered_survey_questions_for_a_question.yml +5009 -0
- data/spec/vcr/poptart/root/builds_top_level_uri.yml +72 -0
- data/spec/vcr/poptart/root/builds_urls.yml +72 -0
- data/spec/vcr/user/management_creates_a_user.yml +63 -0
- data/spec/vcr/user/management_returns_a_user.yml +201 -14
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f25834bcd5de7c7d86e441b4d9e4934db4beb1b4
|
4
|
+
data.tar.gz: f5778a090c1b45c9e433763533c1227f439fcfcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1742e82de647cae81f9d9e4eb91e737b44e7b708df50810830e8ca8677d390b709a511bec4d331aa9a3d0d45be6fac8f4be74505852d4e0e8fae2f9ed0ada36
|
7
|
+
data.tar.gz: 76a51e841b06c6df233cafea45d3701a3c2f6eeb1821090167cb4afc923e0c4d366e734adf1d54b2e6071b6b6b96f40778e19a29590d2ca4c04e1880a333201e
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
poptart (0.0.
|
4
|
+
poptart (0.0.5)
|
5
5
|
activesupport
|
6
|
+
addressable
|
6
7
|
faraday
|
7
8
|
hashie
|
8
9
|
|
@@ -37,8 +38,8 @@ GEM
|
|
37
38
|
guard-rspec (4.0.0)
|
38
39
|
guard (~> 2.0)
|
39
40
|
rspec (~> 2.14)
|
40
|
-
hashie (3.1
|
41
|
-
i18n (0.6.
|
41
|
+
hashie (3.3.1)
|
42
|
+
i18n (0.6.11)
|
42
43
|
json (1.8.1)
|
43
44
|
listen (2.1.0)
|
44
45
|
celluloid (>= 0.15.2)
|
data/lib/poptart/model.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module Poptart
|
2
2
|
class Model
|
3
|
-
|
3
|
+
extend Poptart::Request
|
4
|
+
|
5
|
+
attr_accessor :id, :links, :params, :root
|
4
6
|
|
5
7
|
def initialize(response)
|
6
8
|
if response.respond_to?(:has_key?)
|
@@ -13,5 +15,51 @@ module Poptart
|
|
13
15
|
@id = @params['id']
|
14
16
|
@links = Hashie::Mash.new(@params['_links'])
|
15
17
|
end
|
18
|
+
|
19
|
+
def self.root
|
20
|
+
return @root if @root
|
21
|
+
response = get("/")
|
22
|
+
@root = Poptart::Root.new(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
def root
|
26
|
+
Poptart::Model.root
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_url(resource, id: nil, nested_resource: nil, query: nil)
|
30
|
+
template_string = "#{scheme}://#{host}/{+resource}/{id}"
|
31
|
+
if nested_resource
|
32
|
+
template_string += "/{nested_resource}"
|
33
|
+
end
|
34
|
+
template_string += "{?query*}"
|
35
|
+
template = Addressable::Template.new(template_string)
|
36
|
+
template.expand(resource: resource, id: id, nested_resource: nested_resource, query: query).to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
def users_url(id: nil, query: nil)
|
40
|
+
build_url(links.users.href, id: id, query: query)
|
41
|
+
end
|
42
|
+
|
43
|
+
def surveys_url(id: nil, query: nil)
|
44
|
+
build_url(links.surveys.href, id: id, query: query)
|
45
|
+
end
|
46
|
+
|
47
|
+
def questions_url(id: nil, query: nil)
|
48
|
+
build_url(links.questions.href, id: id, query: query)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def root_uri
|
54
|
+
@root_uri = URI.parse(root.links.self.href)
|
55
|
+
end
|
56
|
+
|
57
|
+
def scheme
|
58
|
+
root_uri.scheme
|
59
|
+
end
|
60
|
+
|
61
|
+
def host
|
62
|
+
root_uri.port ? "#{root_uri.host}:#{root_uri.port}" : root_uri.host
|
63
|
+
end
|
16
64
|
end
|
17
65
|
end
|
data/lib/poptart/question.rb
CHANGED
@@ -11,8 +11,7 @@ module Poptart
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.all(params = {})
|
14
|
-
|
15
|
-
response = get(root.links.questions.href)
|
14
|
+
response = get(root.questions_url)
|
16
15
|
JSON.parse(response.body).map do |question|
|
17
16
|
if params[:type]
|
18
17
|
if question['question_type'] == params[:type]
|
data/lib/poptart/request.rb
CHANGED
data/lib/poptart/root.rb
CHANGED
data/lib/poptart/survey.rb
CHANGED
@@ -6,6 +6,7 @@ module Poptart
|
|
6
6
|
def initialize(response)
|
7
7
|
super
|
8
8
|
@user_id = params['user_id']
|
9
|
+
@completed = params['completed']
|
9
10
|
|
10
11
|
@survey_questions = params['survey_questions'].map do |survey_question|
|
11
12
|
if survey_question['type'] == 'BooleanQuestion'
|
@@ -17,8 +18,8 @@ module Poptart
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def add_question(question)
|
20
|
-
|
21
|
-
|
21
|
+
url = build_url(links.survey_questions.post.href)
|
22
|
+
response = post(url, { survey_question: { question_id: question.id } } )
|
22
23
|
response.status == 201
|
23
24
|
end
|
24
25
|
|
@@ -28,10 +29,8 @@ module Poptart
|
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
|
-
def
|
32
|
-
|
33
|
-
SurveyQuestion.for_url(links['next']['href'])
|
34
|
-
end
|
32
|
+
def completed?
|
33
|
+
@completed
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
@@ -35,7 +35,8 @@ module Poptart
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def submit
|
38
|
-
|
38
|
+
url = build_url(links.put.href)
|
39
|
+
response = put(url, { id: id, survey_question: { answer: answer} })
|
39
40
|
response.status == 204
|
40
41
|
end
|
41
42
|
end
|
data/lib/poptart/user.rb
CHANGED
@@ -2,39 +2,39 @@ module Poptart
|
|
2
2
|
class User < Model
|
3
3
|
extend Poptart::Request
|
4
4
|
include Poptart::Request
|
5
|
-
attr_accessor :
|
5
|
+
attr_accessor :service_user_id, :token
|
6
6
|
|
7
7
|
def initialize(response)
|
8
8
|
super
|
9
|
-
@
|
9
|
+
@service_user_id = params['service_user_id']
|
10
10
|
@token = params['token']
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.create
|
14
|
-
|
15
|
-
response = post(root.links.users.href, user: { external_user_id: external_user_id })
|
13
|
+
def self.create
|
14
|
+
response = post(root.users_url)
|
16
15
|
Poptart::User.new(response)
|
17
16
|
end
|
18
17
|
|
19
|
-
def self.for_id(
|
20
|
-
|
21
|
-
response = get(
|
18
|
+
def self.for_id(service_user_id)
|
19
|
+
url = root.users_url(id: service_user_id)
|
20
|
+
response = get(url)
|
22
21
|
Poptart::User.new(response)
|
23
22
|
end
|
24
23
|
|
25
24
|
def create_survey
|
26
|
-
|
27
|
-
response = post(root.links.surveys.href, survey: { user_id: id })
|
25
|
+
response = post(root.surveys_url, survey: { user_id: id })
|
28
26
|
Poptart::Survey.new(response)
|
29
27
|
end
|
30
28
|
|
31
29
|
def create_random_survey
|
32
|
-
|
30
|
+
url = root.surveys_url(query: {random: true})
|
31
|
+
response = post(url, survey: { user_id: id })
|
33
32
|
Poptart::Survey.new(response)
|
34
33
|
end
|
35
34
|
|
36
35
|
def survey_for_id(id)
|
37
|
-
|
36
|
+
url = root.surveys_url(id: id)
|
37
|
+
response = get(url)
|
38
38
|
Poptart::Survey.new(response)
|
39
39
|
end
|
40
40
|
|
@@ -42,5 +42,9 @@ module Poptart
|
|
42
42
|
response = get(url)
|
43
43
|
Survey.new.extend(SurveyRepresenter).from_json(response.body)
|
44
44
|
end
|
45
|
+
|
46
|
+
def survey_questions_for_question_id(question_id)
|
47
|
+
|
48
|
+
end
|
45
49
|
end
|
46
50
|
end
|
data/lib/poptart.rb
CHANGED
@@ -19,10 +19,11 @@ end
|
|
19
19
|
require 'faraday'
|
20
20
|
require 'json'
|
21
21
|
require 'hashie'
|
22
|
+
require "addressable/template"
|
22
23
|
|
23
24
|
require_relative 'version'
|
24
|
-
require_relative 'poptart/model'
|
25
25
|
require_relative 'poptart/request'
|
26
|
+
require_relative 'poptart/model'
|
26
27
|
require_relative 'poptart/root'
|
27
28
|
require_relative 'poptart/survey'
|
28
29
|
require_relative 'poptart/survey_question'
|
data/lib/version.rb
CHANGED
data/poptart.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.add_dependency 'activesupport'
|
19
19
|
gem.add_dependency 'hashie'
|
20
20
|
gem.add_dependency 'faraday'
|
21
|
+
gem.add_dependency 'addressable'
|
21
22
|
|
22
23
|
gem.add_development_dependency 'bourne', "1.5.0"
|
23
24
|
gem.add_development_dependency 'guard'
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'Answering survey questions'
|
3
|
+
describe 'Answering survey questions' do
|
4
4
|
it "creates and returns an empty survey", :vcr do
|
5
|
-
user = Poptart::User.create
|
5
|
+
user = Poptart::User.create
|
6
6
|
survey = user.create_survey
|
7
7
|
survey.user_id.should == user.id
|
8
8
|
survey.survey_questions.count.should == 0
|
9
9
|
end
|
10
10
|
|
11
11
|
it "creates and returns a random question survey", :vcr do
|
12
|
-
user = Poptart::User.create
|
12
|
+
user = Poptart::User.create
|
13
13
|
survey = user.create_random_survey
|
14
14
|
survey.user_id.should == user.id
|
15
|
-
survey.survey_questions.count.should ==
|
15
|
+
survey.survey_questions.count.should == 5
|
16
16
|
end
|
17
17
|
|
18
18
|
it "answers a survey question", :vcr do
|
19
|
-
user = Poptart::User.create
|
19
|
+
user = Poptart::User.create
|
20
20
|
survey = user.create_random_survey
|
21
21
|
survey_question = survey.survey_questions.first
|
22
22
|
survey_question.text.should be
|
@@ -29,7 +29,7 @@ describe 'Answering survey questions', :vcr do
|
|
29
29
|
|
30
30
|
it "answers a survey question", :vcr do
|
31
31
|
boolean_questions = Poptart::Question.all(type: 'boolean')
|
32
|
-
user = Poptart::User.create
|
32
|
+
user = Poptart::User.create
|
33
33
|
survey = user.create_survey
|
34
34
|
survey.add_question(boolean_questions.first).should be
|
35
35
|
|
@@ -49,7 +49,7 @@ describe 'Answering survey questions', :vcr do
|
|
49
49
|
it "answers a multiple choice question", :vcr do
|
50
50
|
questions = Poptart::Question.all(type: 'multiple')
|
51
51
|
question = questions.find { |question| question.responses.include?('At Home') }
|
52
|
-
user = Poptart::User.create
|
52
|
+
user = Poptart::User.create
|
53
53
|
survey = user.create_survey
|
54
54
|
survey.add_question(question).should be
|
55
55
|
|
@@ -64,14 +64,37 @@ describe 'Answering survey questions', :vcr do
|
|
64
64
|
|
65
65
|
survey = user.survey_for_id(survey.id)
|
66
66
|
survey.survey_questions.first.answer.should == 'At Home'
|
67
|
-
survey.completed?.should be
|
68
67
|
end
|
69
68
|
|
70
69
|
it "finds survey question for id", :vcr do
|
71
|
-
user = Poptart::User.create
|
70
|
+
user = Poptart::User.create
|
72
71
|
survey = user.create_random_survey
|
73
72
|
first_survey_question = survey.survey_questions.first
|
74
73
|
survey_question = survey.survey_question_for_id(first_survey_question.id)
|
75
74
|
first_survey_question.should == survey_question
|
76
75
|
end
|
76
|
+
|
77
|
+
xit "returns all answered survey questions for a question", :vcr, :record => :all do
|
78
|
+
questions = Poptart::Question.all(type: 'multiple')
|
79
|
+
question = questions.find { |question| question.responses.include?('At Home') }
|
80
|
+
user = Poptart::User.create
|
81
|
+
|
82
|
+
survey = user.create_survey
|
83
|
+
survey.add_question(question).should be
|
84
|
+
second_survey = user.create_survey
|
85
|
+
second_survey.add_question(question).should be
|
86
|
+
|
87
|
+
survey = user.survey_for_id(survey.id)
|
88
|
+
survey_question = survey.survey_questions.first
|
89
|
+
survey_question.answer = 'At Home'
|
90
|
+
survey_question.submit.should be
|
91
|
+
|
92
|
+
survey = user.survey_for_id(second_survey.id)
|
93
|
+
survey_question = survey.survey_questions.first
|
94
|
+
survey_question.answer = 'At Work'
|
95
|
+
survey_question.submit.should be
|
96
|
+
|
97
|
+
answered_questions = user.survey_questions_for_question_id(question.id)
|
98
|
+
answered_questions.map(&:answer).should =~ ['At Home', 'At Work']
|
99
|
+
end
|
77
100
|
end
|
data/spec/requests/root_spec.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Poptart::Root do
|
4
|
-
it "
|
5
|
-
root = Poptart::Root.
|
6
|
-
root.
|
4
|
+
it "builds urls", :vcr do
|
5
|
+
root = Poptart::Root.root
|
6
|
+
root.build_url('api/questions').should == 'http://localhost:3000/api/questions/'
|
7
|
+
root.build_url('api/questions', id: 1).should == 'http://localhost:3000/api/questions/1'
|
8
|
+
root.build_url('api/questions', id: 1, nested_resource: 'poptarts').should == 'http://localhost:3000/api/questions/1/poptarts'
|
9
|
+
root.build_url('api/questions', query: {space: 'cat'}).should == 'http://localhost:3000/api/questions/?space=cat'
|
7
10
|
end
|
8
11
|
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "User Management"
|
4
|
-
it "creates a user" do
|
5
|
-
user = Poptart::User.create
|
3
|
+
describe "User Management" do
|
4
|
+
it "creates a user", :vcr do
|
5
|
+
user = Poptart::User.create
|
6
6
|
user.id.should be
|
7
|
-
user.
|
7
|
+
user.service_user_id.should be
|
8
8
|
user.token.should be
|
9
9
|
end
|
10
10
|
|
11
|
-
it "returns a user" do
|
12
|
-
user = Poptart::User.create
|
13
|
-
other_user = Poptart::User.for_id(user.
|
11
|
+
it "returns a user", :vcr do
|
12
|
+
user = Poptart::User.create
|
13
|
+
other_user = Poptart::User.for_id(user.service_user_id)
|
14
14
|
user.id.should == other_user.id
|
15
|
-
user.
|
15
|
+
user.service_user_id.should == other_user.service_user_id
|
16
16
|
end
|
17
17
|
end
|