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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64b322723a131db8bb8a376500573598aee988e0
4
- data.tar.gz: 80e1c8afe85c907e2d7d1a577717e8f324f8fcdf
3
+ metadata.gz: f25834bcd5de7c7d86e441b4d9e4934db4beb1b4
4
+ data.tar.gz: f5778a090c1b45c9e433763533c1227f439fcfcb
5
5
  SHA512:
6
- metadata.gz: 073ecece14fb149678c5989d5e54b7eec2458cd0902b0f12a011af374fe9a67aa48c4455c5ae7c91a5e85bda9bc6bb8842c03172b652f15c27d77499cf2f422e
7
- data.tar.gz: 6e6fa0b9bc27db04f054a2800e8d326b2be9df08abfe1583272d59821321a34e370ad7daf997079b2d5450215adc1588bcc622611e6a2d51be6a656d967f70f0
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)
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.0)
41
- i18n (0.6.9)
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
- attr_accessor :id, :links, :params
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
@@ -11,8 +11,7 @@ module Poptart
11
11
  end
12
12
 
13
13
  def self.all(params = {})
14
- root = Poptart::Root.get_root
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]
@@ -19,7 +19,7 @@ module Poptart
19
19
  end
20
20
  end
21
21
 
22
- def post(url, data, headers = {})
22
+ def post(url, data = {}, headers = {})
23
23
  connection.post do |req|
24
24
  req.url(url)
25
25
  req.body = data.to_json if data
data/lib/poptart/root.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  module Poptart
2
2
  class Root < Model
3
- extend Poptart::Request
4
3
  attr_accessor :links
5
4
 
6
- def self.get_root
7
- response = get("/")
8
- Poptart::Root.new(response)
9
- end
10
5
  end
11
6
  end
@@ -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
- response = post("#{links.self.href}/survey_questions",
21
- { survey_question: { question_id: question.id } } )
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 next_question
32
- if links['next']
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
- response = put(links.submit.href, { id: id, survey_question: { answer: answer} })
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 :external_user_id, :token
5
+ attr_accessor :service_user_id, :token
6
6
 
7
7
  def initialize(response)
8
8
  super
9
- @external_user_id = params['external_user_id']
9
+ @service_user_id = params['service_user_id']
10
10
  @token = params['token']
11
11
  end
12
12
 
13
- def self.create(external_user_id)
14
- root = Poptart::Root.get_root
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(external_user_id)
20
- root = Poptart::Root.get_root
21
- response = get("#{root.links.users.href}/#{external_user_id}")
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
- root = Poptart::Root.get_root
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
- response = post("/api/surveys?random=true", survey: { user_id: id })
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
- response = get("/api/surveys/#{id}")
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
@@ -1,3 +1,3 @@
1
1
  module Poptart
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
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', :vcr do
3
+ describe 'Answering survey questions' do
4
4
  it "creates and returns an empty survey", :vcr do
5
- user = Poptart::User.create(42)
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(42)
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 == 6
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(42)
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(42)
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(42)
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(42)
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
@@ -1,8 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Poptart::Root do
4
- it "returns survey links", :vcr do
5
- root = Poptart::Root.get_root
6
- root.links.self.href.should == 'http://localhost:3000/api'
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", :vcr do
4
- it "creates a user" do
5
- user = Poptart::User.create(42)
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.external_user_id.should == 42
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(43)
13
- other_user = Poptart::User.for_id(user.external_user_id)
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.external_user_id.should == other_user.external_user_id
15
+ user.service_user_id.should == other_user.service_user_id
16
16
  end
17
17
  end