poptart 0.0.1

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.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +3 -0
  6. data/Gemfile +3 -0
  7. data/Gemfile.lock +88 -0
  8. data/README.md +0 -0
  9. data/Rakefile +18 -0
  10. data/lib/poptart/.DS_Store +0 -0
  11. data/lib/poptart/model.rb +17 -0
  12. data/lib/poptart/question.rb +19 -0
  13. data/lib/poptart/request.rb +48 -0
  14. data/lib/poptart/root.rb +11 -0
  15. data/lib/poptart/survey.rb +37 -0
  16. data/lib/poptart/survey_question.rb +42 -0
  17. data/lib/poptart/user.rb +46 -0
  18. data/lib/poptart.rb +22 -0
  19. data/lib/version.rb +3 -0
  20. data/poptart.gemspec +31 -0
  21. data/spec/requests/answering_survey_questions_spec.rb +56 -0
  22. data/spec/requests/root_spec.rb +8 -0
  23. data/spec/requests/user_management_spec.rb +17 -0
  24. data/spec/spec_helper.rb +68 -0
  25. data/spec/vcr/answering/survey_questions_answers_a_survey_question.yml +1588 -0
  26. data/spec/vcr/answering/survey_questions_creates_and_returns_a_random_question_survey.yml +330 -0
  27. data/spec/vcr/answering/survey_questions_creates_and_returns_an_empty_survey.yml +272 -0
  28. data/spec/vcr/answering/survey_questions_finds_survey_question_for_id.yml +330 -0
  29. data/spec/vcr/poptart/root/returns_survey_links.yml +72 -0
  30. data/spec/vcr/poptart/user/answers_a_survey_question.yml +1580 -0
  31. data/spec/vcr/poptart/user/creates_a_user.yml +135 -0
  32. data/spec/vcr/poptart/user/creates_and_returns_a_random_question_survey.yml +341 -0
  33. data/spec/vcr/poptart/user/creates_and_returns_an_empty_survey.yml +272 -0
  34. data/spec/vcr/poptart/user/finds_survey_question_for_id.yml +327 -0
  35. data/spec/vcr/poptart/user/returns_a_user.yml +267 -0
  36. data/spec/vcr/user/management_creates_a_user.yml +135 -0
  37. data/spec/vcr/user/management_returns_a_user.yml +267 -0
  38. data/tags +72 -0
  39. metadata +266 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 865e91a53026b224e7979cf26cbc977dedaa826b
4
+ data.tar.gz: 602e1c41cbfd6dde4cdcc4bbab922c04d8186f73
5
+ SHA512:
6
+ metadata.gz: 78ac46448e35b3c2ea93fb1ceebf37621dae715e3bb212818e7ab587bd154ea33515c6e7dce8d66126d13a489b279383d5de77db8818b1f3e52bd65877d67548
7
+ data.tar.gz: 653ee77984bccdd17d6480ac8f4304191b847a76a09270fa007b32ad2b5d3cb374cb937dd6e80821a23b09df8324accef93386f46753f2aa981694c10afdfaad
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ pkg
20
+ tags
21
+ .rbenv-version
22
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ poptart (0.0.1)
5
+ activesupport (~> 3.2.13)
6
+ faraday
7
+ hashie
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (3.2.18)
13
+ i18n (~> 0.6, >= 0.6.4)
14
+ multi_json (~> 1.0)
15
+ addressable (2.3.5)
16
+ bourne (1.5.0)
17
+ mocha (>= 0.13.2, < 0.15)
18
+ celluloid (0.15.2)
19
+ timers (~> 1.1.0)
20
+ coderay (1.0.9)
21
+ crack (0.4.1)
22
+ safe_yaml (~> 0.9.0)
23
+ diff-lcs (1.2.4)
24
+ faraday (0.9.0)
25
+ multipart-post (>= 1.2, < 3)
26
+ ffi (1.9.0)
27
+ formatador (0.2.4)
28
+ guard (2.0.5)
29
+ formatador (>= 0.2.4)
30
+ listen (~> 2.0)
31
+ lumberjack (~> 1.0)
32
+ pry (>= 0.9.12)
33
+ thor (>= 0.18.1)
34
+ guard-rspec (4.0.0)
35
+ guard (~> 2.0)
36
+ rspec (~> 2.14)
37
+ hashie (2.1.1)
38
+ i18n (0.6.9)
39
+ listen (2.1.0)
40
+ celluloid (>= 0.15.2)
41
+ rb-fsevent (>= 0.9.3)
42
+ rb-inotify (>= 0.9)
43
+ lumberjack (1.0.4)
44
+ metaclass (0.0.1)
45
+ method_source (0.8.2)
46
+ mocha (0.14.0)
47
+ metaclass (~> 0.0.1)
48
+ multi_json (1.10.1)
49
+ multipart-post (2.0.0)
50
+ pry (0.9.12.2)
51
+ coderay (~> 1.0.5)
52
+ method_source (~> 0.8)
53
+ slop (~> 3.4)
54
+ rake (10.1.0)
55
+ rb-fsevent (0.9.3)
56
+ rb-inotify (0.9.2)
57
+ ffi (>= 0.5.0)
58
+ rspec (2.14.1)
59
+ rspec-core (~> 2.14.0)
60
+ rspec-expectations (~> 2.14.0)
61
+ rspec-mocks (~> 2.14.0)
62
+ rspec-core (2.14.5)
63
+ rspec-expectations (2.14.3)
64
+ diff-lcs (>= 1.1.3, < 2.0)
65
+ rspec-mocks (2.14.3)
66
+ safe_yaml (0.9.7)
67
+ slop (3.4.6)
68
+ thor (0.18.1)
69
+ timers (1.1.0)
70
+ vcr (2.6.0)
71
+ webmock (1.14.0)
72
+ addressable (>= 2.2.7)
73
+ crack (>= 0.3.2)
74
+
75
+ PLATFORMS
76
+ ruby
77
+
78
+ DEPENDENCIES
79
+ bourne (= 1.5.0)
80
+ guard
81
+ guard-rspec
82
+ mocha (~> 0.14.0)
83
+ poptart!
84
+ pry
85
+ rake
86
+ rspec
87
+ vcr
88
+ webmock
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ task :spec do
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.pattern = './spec/**/*_spec.rb'
11
+ end
12
+ end
13
+
14
+ namespace :spec do
15
+ task :clean do
16
+ FileUtils.rm_rf(Dir.glob('spec/vcr/*'))
17
+ end
18
+ end
Binary file
@@ -0,0 +1,17 @@
1
+ module Poptart
2
+ class Model
3
+ attr_accessor :id, :links, :params
4
+
5
+ def initialize(response)
6
+ if response.respond_to?(:has_key?)
7
+ @params = response
8
+ else
9
+ raise "Unauthorized"if response.status == 401
10
+ @params = JSON.parse(response.body)
11
+ end
12
+
13
+ @id = @params['id']
14
+ @links = Hashie::Mash.new(@params['_links'])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Poptart
2
+ class Question < Model
3
+ extend Poptart::Request
4
+
5
+ def self.all(params = {})
6
+ root = Poptart::Root.get_root
7
+ response = get(root.links.questions.href)
8
+ JSON.parse(response.body).map do |question|
9
+ if params[:type]
10
+ if question['question_type'] == params[:type]
11
+ new(question)
12
+ end
13
+ else
14
+ new(question)
15
+ end
16
+ end.compact
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ module Poptart
2
+ module Request
3
+ def get(url, headers = {})
4
+ connection.get do |req|
5
+ req.url(url)
6
+ req.headers['Content-Type'] = 'application/json'
7
+ req.headers['API-TOKEN'] = Poptart.api_token if Poptart.api_token
8
+ req.headers.merge!(headers)
9
+ end
10
+ end
11
+
12
+ def put(url, data, headers = {})
13
+ connection.put do |req|
14
+ req.url(url)
15
+ req.body = data.to_json if data
16
+ req.headers['Content-Type'] = 'application/json'
17
+ req.headers['API-TOKEN'] = Poptart.api_token if Poptart.api_token
18
+ req.headers.merge!(headers)
19
+ end
20
+ end
21
+
22
+ def post(url, data, headers = {})
23
+ connection.post do |req|
24
+ req.url(url)
25
+ req.body = data.to_json if data
26
+ req.headers['Content-Type'] = 'application/json'
27
+ req.headers['API-TOKEN'] = Poptart.api_token if Poptart.api_token
28
+ req.headers.merge!(headers)
29
+ end
30
+ end
31
+
32
+ def connection
33
+ return @connection if @connection
34
+
35
+ # if Rails.env.production?
36
+ # url = ""
37
+ # else
38
+ url = "http://localhost:3000"
39
+ # end
40
+
41
+ @connection = Faraday.new(:url => url) do |faraday|
42
+ faraday.request :url_encoded
43
+ faraday.response :logger
44
+ faraday.adapter Faraday.default_adapter
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ module Poptart
2
+ class Root < Model
3
+ extend Poptart::Request
4
+ attr_accessor :links
5
+
6
+ def self.get_root
7
+ response = get("/")
8
+ Poptart::Root.new(response)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ module Poptart
2
+ class Survey < Model
3
+ include Poptart::Request
4
+ attr_accessor :user_id, :survey_questions
5
+
6
+ def initialize(response)
7
+ super
8
+ @user_id = params['user_id']
9
+
10
+ @survey_questions = params['survey_questions'].map do |survey_question|
11
+ if survey_question['type'] == 'BooleanQuestion'
12
+ BooleanQuestion.new(survey_question)
13
+ else
14
+ SurveyQuestion.new(survey_question)
15
+ end
16
+ end.sort_by { |survey_question| survey_question.id }
17
+ end
18
+
19
+ def add_question(question)
20
+ response = post("#{links.self.href}/survey_questions",
21
+ { survey_question: { question_id: question.id } } )
22
+ response.status == 201
23
+ end
24
+
25
+ def survey_question_for_id(id)
26
+ survey_questions.find do |survey_question|
27
+ survey_question.id == id.to_i
28
+ end
29
+ end
30
+
31
+ def next_question
32
+ if links['next']
33
+ SurveyQuestion.for_url(links['next']['href'])
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ module Poptart
2
+ class SurveyQuestion < Model
3
+ extend Poptart::Request
4
+ include Poptart::Request
5
+
6
+ attr_accessor :text, :answer, :type, :responses
7
+
8
+ def initialize(response)
9
+ super
10
+ @text = params['text']
11
+ @answer = params['answer']
12
+ @type = params['type']
13
+ @freeform = params['freeform']
14
+ @responses = params['responses']
15
+ end
16
+
17
+ def boolean?
18
+ type == "boolean"
19
+ end
20
+
21
+ def multiple?
22
+ type == "multiple"
23
+ end
24
+
25
+ def range?
26
+ type == "range"
27
+ end
28
+
29
+ def time?
30
+ type == "time"
31
+ end
32
+
33
+ def freeform?
34
+ @freeform == true
35
+ end
36
+
37
+ def submit
38
+ response = put(links.submit.href, { id: id, survey_question: { answer: answer} })
39
+ response.status == 204
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ module Poptart
2
+ class User < Model
3
+ extend Poptart::Request
4
+ include Poptart::Request
5
+ attr_accessor :external_user_id, :token
6
+
7
+ def initialize(response)
8
+ super
9
+ @external_user_id = params['external_user_id']
10
+ @token = params['token']
11
+ end
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 })
16
+ Poptart::User.new(response)
17
+ end
18
+
19
+ def self.for_id(external_user_id)
20
+ root = Poptart::Root.get_root
21
+ response = get("#{root.links.users.href}/#{external_user_id}")
22
+ Poptart::User.new(response)
23
+ end
24
+
25
+ def create_survey
26
+ root = Poptart::Root.get_root
27
+ response = post(root.links.surveys.href, survey: { user_id: id })
28
+ Poptart::Survey.new(response)
29
+ end
30
+
31
+ def create_random_survey
32
+ response = post("/api/surveys?random=true", survey: { user_id: id })
33
+ Poptart::Survey.new(response)
34
+ end
35
+
36
+ def survey_for_id(id)
37
+ response = get("/api/surveys/#{id}")
38
+ Poptart::Survey.new(response)
39
+ end
40
+
41
+ def survey_for_url(url)
42
+ response = get(url)
43
+ Survey.new.extend(SurveyRepresenter).from_json(response.body)
44
+ end
45
+ end
46
+ end
data/lib/poptart.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Poptart
2
+ def self.api_token=(token)
3
+ @token = token
4
+ end
5
+
6
+ def self.api_token
7
+ @token
8
+ end
9
+ end
10
+
11
+ require 'faraday'
12
+ require 'json'
13
+ require 'hashie'
14
+
15
+ require_relative 'version'
16
+ require_relative 'poptart/model'
17
+ require_relative 'poptart/request'
18
+ require_relative 'poptart/root'
19
+ require_relative 'poptart/survey'
20
+ require_relative 'poptart/survey_question'
21
+ require_relative 'poptart/question'
22
+ require_relative 'poptart/user'
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Poptart
2
+ VERSION = "0.0.1"
3
+ end
data/poptart.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'poptart'
7
+ gem.version = Poptart::VERSION
8
+ gem.authors = ['Austen Ito']
9
+ gem.email = ['austen.dev@gmail.com']
10
+ gem.homepage = 'https://github.com/austenito/poptart'
11
+ gem.summary = 'The API client gem for the happines service'
12
+ gem.description = gem.summary
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.require_paths = ['lib']
16
+ gem.license = 'MIT'
17
+
18
+ gem.add_dependency 'activesupport', "~> 3.2.13"
19
+ gem.add_dependency 'hashie'
20
+ gem.add_dependency 'faraday'
21
+
22
+ gem.add_development_dependency 'bourne', "1.5.0"
23
+ gem.add_development_dependency 'guard'
24
+ gem.add_development_dependency 'guard-rspec'
25
+ gem.add_development_dependency 'mocha', "~> 0.14.0"
26
+ gem.add_development_dependency 'pry'
27
+ gem.add_development_dependency 'rspec'
28
+ gem.add_development_dependency 'rake'
29
+ gem.add_development_dependency 'vcr'
30
+ gem.add_development_dependency 'webmock'
31
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Answering survey questions', :vcr do
4
+ it "creates and returns an empty survey" do
5
+ user = Poptart::User.create(42)
6
+ survey = user.create_survey
7
+ survey.user_id.should == user.id
8
+ survey.survey_questions.count.should == 0
9
+ end
10
+
11
+ it "creates and returns a random question survey" do
12
+ user = Poptart::User.create(42)
13
+ survey = user.create_random_survey
14
+ survey.user_id.should == user.id
15
+ survey.survey_questions.count.should == 5
16
+ end
17
+
18
+ it "answers a survey question" do
19
+ user = Poptart::User.create(42)
20
+ survey = user.create_random_survey
21
+ survey_question = survey.survey_questions.first
22
+ survey_question.text.should be
23
+ survey_question.answer = "foo"
24
+ survey_question.submit.should be
25
+
26
+ survey = user.survey_for_id(survey.id)
27
+ survey.survey_questions.first.answer.should == "foo"
28
+ end
29
+
30
+ it "answers a survey question", record: :all do
31
+ boolean_questions = Poptart::Question.all(type: 'boolean')
32
+ user = Poptart::User.create(42)
33
+ survey = user.create_survey
34
+ survey.add_question(boolean_questions.first).should be
35
+
36
+ survey = user.survey_for_id(survey.id)
37
+ survey.survey_questions.count.should == 1
38
+ survey_question = survey.survey_questions.first
39
+ survey_question.responses.should == ['t', 'f']
40
+ survey_question.type.should == 'boolean'
41
+
42
+ survey_question.answer = 'true'
43
+ survey_question.submit.should be
44
+
45
+ survey = user.survey_for_id(survey.id)
46
+ survey.survey_questions.first.answer.should == 'true'
47
+ end
48
+
49
+ it "finds survey question for id" do
50
+ user = Poptart::User.create(42)
51
+ survey = user.create_random_survey
52
+ first_survey_question = survey.survey_questions.first
53
+ survey_question = survey.survey_question_for_id(first_survey_question.id)
54
+ first_survey_question.should == survey_question
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
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'
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe "User Management", :vcr do
4
+ it "creates a user" do
5
+ user = Poptart::User.create(42)
6
+ user.id.should be
7
+ user.external_user_id.should == 42
8
+ user.token.should be
9
+ end
10
+
11
+ it "returns a user" do
12
+ user = Poptart::User.create(43)
13
+ other_user = Poptart::User.for_id(user.external_user_id)
14
+ user.id.should == other_user.id
15
+ user.external_user_id.should == other_user.external_user_id
16
+ end
17
+ end
@@ -0,0 +1,68 @@
1
+ require_relative "../lib/poptart"
2
+
3
+ require 'mocha/api'
4
+ require 'bourne'
5
+ require 'vcr'
6
+
7
+ require 'active_support/inflector'
8
+ require 'pry/test/helper'
9
+
10
+ RSpec.configure do |c|
11
+ c.mock_with :mocha
12
+ c.treat_symbols_as_metadata_keys_with_true_values = true
13
+
14
+ c.around(:each, :vcr) do |example|
15
+ name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_")
16
+ options = {}
17
+ options[:record] = example.metadata[:record] if example.metadata[:record]
18
+ VCR.use_cassette(name, options) { example.call }
19
+ end
20
+
21
+ include PryTestHelpers
22
+ end
23
+
24
+ VCR.configure do |c|
25
+ c.cassette_library_dir = 'spec/vcr'
26
+ c.hook_into :webmock
27
+
28
+ c.default_cassette_options = {
29
+ :record => :once,
30
+ :decode_compressed_response => true,
31
+ # Because psych is binary encoding response headers marked with ASCII-8BIT
32
+ # https://groups.google.com/forum/?fromgroups#!topic/vcr-ruby/2sKrJa86ktU
33
+ # And syck's output is much easier to read
34
+ :serialize_with => :psych,
35
+ }
36
+
37
+ # Pretty print your json so it's not all on one line
38
+ # From discussion here: https://github.com/myronmarston/vcr/pull/147
39
+ # https://gist.github.com/26edfe7669cc7b85e164
40
+ c.before_record do |i|
41
+ type = Array(i.response.headers['Content-Type']).join(',').split(';').first
42
+ code = i.response.status.code
43
+
44
+ if type =~ /[\/+]json$/ or 'text/javascript' == type
45
+ begin
46
+ data = JSON.parse i.response.body
47
+ rescue
48
+ if code != 404
49
+ puts
50
+ warn "VCR: JSON parse error for Content-type #{type}"
51
+ warn "Your unparseable json is: " + i.response.body.inspect
52
+ puts
53
+ end
54
+ else
55
+ i.response.body = JSON.pretty_generate data
56
+ i.response.update_content_length_header
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ class String
63
+ def unindent
64
+ gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
65
+ end
66
+ end
67
+
68
+ Poptart.api_token = "testing"