traitify 1.1.0

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.
@@ -0,0 +1,17 @@
1
+ module Traitify
2
+ class Client
3
+ module Result
4
+ def find_results(assessment_id, image_pack = nil)
5
+ image_pack ||= self.image_pack
6
+
7
+ if image_pack
8
+ response = get("/assessments/#{assessment_id}/personality_types?image_pack=#{image_pack}")
9
+ else
10
+ response = get("/assessments/#{assessment_id}/personality_types")
11
+ end
12
+
13
+ Traitify::Result.parse_json(response)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ module Traitify
2
+ class Client
3
+ module Slide
4
+ def find_slides(assessment_id)
5
+ response = get("/assessments/#{assessment_id}/slides")
6
+
7
+ Traitify::Slides.parse_json(assessment_id, response)
8
+ end
9
+
10
+ def update_slides(slides)
11
+ response = put("/assessments/#{slides.assessment_id}/slides", slides.to_update_params)
12
+
13
+ Traitify::Slides.parse_json(slides.assessment_id, response)
14
+ end
15
+
16
+ def update_slide(hash_or_assessment_id, slide = nil)
17
+ if slide
18
+ assessment_id = hash_or_assessment_id
19
+ slide_id = slide.id
20
+ hash = slide.to_update_params.tap { |s| s.delete(:id) }
21
+ else
22
+ assessment_id = hash_or_assessment_id[:assessment_id]
23
+ slide_id = hash_or_assessment_id[:slide_id]
24
+ hash = {
25
+ response: hash_or_assessment_id[:response],
26
+ time_taken: hash_or_assessment_id[:time_taken]
27
+ }
28
+ end
29
+
30
+ response = put("/assessments/#{assessment_id}/slides/#{slide_id}", hash)
31
+
32
+ Traitify::Slide.parse_json(response)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module Traitify
2
+ class Client
3
+ module User
4
+ def create_user(user_params)
5
+ response = post("/users", user: user_params)
6
+
7
+ Traitify::User.parse_json(response)
8
+ end
9
+
10
+ def find_user(user_id)
11
+ response = get("/users/#{user_id}")
12
+
13
+ Traitify::User.parse_json(response)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Traitify
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :secret,
5
+ :api_host,
6
+ :api_version,
7
+ :deck_id,
8
+ :image_pack
9
+ ]
10
+
11
+ attr_accessor(*VALID_OPTIONS_KEYS)
12
+
13
+ def configure
14
+ yield self
15
+ end
16
+
17
+ def options
18
+ VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require "faraday_middleware"
2
+ require "traitify/error"
3
+
4
+ module Traitify
5
+ module Connection
6
+ def connection(options = {})
7
+ connection ||= Faraday.new(options) do |faraday|
8
+ faraday.request :url_encoded
9
+ faraday.request :basic_auth, self.secret, "x"
10
+ faraday.use ContentTypeMiddleware
11
+ faraday.use ErrorMiddleware
12
+ faraday.response :json, :content_type => /\bjson$/
13
+ faraday.adapter Faraday.default_adapter
14
+ end
15
+
16
+ connection
17
+ end
18
+
19
+ private
20
+ class ContentTypeMiddleware < Faraday::Middleware
21
+ def initialize(app)
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ env[:request_headers]["Accept"] = "application/json"
27
+ env[:request_headers]["Content-Type"] = "application/json"
28
+ @app.call(env)
29
+ end
30
+ end
31
+
32
+ class ErrorMiddleware < Faraday::Middleware
33
+ def initialize(app)
34
+ @app = app
35
+ end
36
+
37
+ def call(env)
38
+ @app.call(env).on_complete do |e|
39
+ if error = Traitify::Error.from(e[:response])
40
+ raise error
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
1
+ module Traitify
2
+ class Error < StandardError
3
+ attr_accessor :response
4
+
5
+ def self.from(response)
6
+ status = response.status
7
+
8
+ if klass = case status
9
+ when 400 then Traitify::BadRequest
10
+ when 401 then Traitify::Unauthorized
11
+ when 404 then Traitify::NotFound
12
+ when 422 then Traitify::UnprocessableEntity
13
+ end
14
+
15
+ klass.new(response)
16
+ end
17
+ end
18
+
19
+ def initialize(response)
20
+ @response = response
21
+ super(error_message)
22
+ end
23
+
24
+ def errors
25
+ response.body
26
+ end
27
+
28
+ private
29
+ def error_message
30
+ message = "#{response.env[:method].upcase} "
31
+ message << "#{response.env[:url].to_s} | "
32
+ message << "#{response.status}: "
33
+ if response.body && response.body.respond_to?(map)
34
+ message << "#{response.body.map{|k,v| "#{k}: #{v.first}"}.join(", ")}"
35
+ end
36
+ message
37
+ end
38
+ end
39
+
40
+ class BadRequest < Error; end
41
+ class Unauthorized < Error; end
42
+ class NotFound < Error; end
43
+ class UnprocessableEntity < Error; end
44
+ end
@@ -0,0 +1,55 @@
1
+ module Traitify
2
+ class PersonalityBlend
3
+ attr_accessor :personality_type_1, :personality_type_2, :name, :description, :compliments, :conflicts, :compatible_work_environment_1, :compatible_work_environment_2, :compatible_work_environment_3, :compatible_work_environment_4
4
+
5
+ def initialize(options = {})
6
+ self.personality_type_1 = options[:personality_type_1]
7
+ self.personality_type_2 = options[:personality_type_2]
8
+ self.name = options[:name]
9
+ self.description = options[:description]
10
+ self.compliments = options[:compliments]
11
+ self.conflicts = options[:conflicts]
12
+ self.compatible_work_environment_1 = options[:compatible_work_environment_1]
13
+ self.compatible_work_environment_2 = options[:compatible_work_environment_2]
14
+ self.compatible_work_environment_3 = options[:compatible_work_environment_3]
15
+ self.compatible_work_environment_4 = options[:compatible_work_environment_4]
16
+ end
17
+
18
+ def self.parse_json(personality_blend)
19
+ if personality_blend
20
+ personality_type_1 = PersonalityType.parse_json(personality_blend["personality_type_1"])
21
+ personality_type_2 = PersonalityType.parse_json(personality_blend["personality_type_2"])
22
+
23
+ PersonalityBlend.new(
24
+ personality_type_1: personality_type_1,
25
+ personality_type_2: personality_type_2,
26
+ name: personality_blend["name"],
27
+ description: personality_blend["description"],
28
+ compliments: personality_blend["compliments"],
29
+ conflicts: personality_blend["conflicts"],
30
+ compatible_work_environment_1: personality_blend["compatible_work_environment_1"],
31
+ compatible_work_environment_2: personality_blend["compatible_work_environment_2"],
32
+ compatible_work_environment_3: personality_blend["compatible_work_environment_3"],
33
+ compatible_work_environment_4: personality_blend["compatible_work_environment_4"]
34
+ )
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ def to_hash
41
+ {
42
+ personality_type_1: personality_type_1.to_hash,
43
+ personality_type_2: personality_type_2.to_hash,
44
+ name: name,
45
+ description: description,
46
+ compliments: compliments,
47
+ conflicts: conflicts,
48
+ compatible_work_environment_1: compatible_work_environment_1,
49
+ compatible_work_environment_2: compatible_work_environment_2,
50
+ compatible_work_environment_3: compatible_work_environment_3,
51
+ compatible_work_environment_4: compatible_work_environment_4
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ module Traitify
2
+ class PersonalityType
3
+ attr_accessor :id, :name, :description, :badge, :score
4
+
5
+ def initialize(options = {})
6
+ self.id = options[:id]
7
+ self.name = options[:name]
8
+ self.description = options[:description]
9
+ self.badge = options[:badge]
10
+ self.score = options[:score]
11
+ end
12
+
13
+ def self.parse_json(personality_type)
14
+ if personality_type
15
+ badge = Badge.parse_json(personality_type["badge"])
16
+
17
+ PersonalityType.new(
18
+ id: personality_type["id"],
19
+ name: personality_type["name"],
20
+ description: personality_type["description"],
21
+ badge: badge,
22
+ score: personality_type["score"]
23
+ )
24
+ else
25
+ nil
26
+ end
27
+ end
28
+
29
+ def to_hash
30
+ {
31
+ id: id,
32
+ name: name,
33
+ description: description,
34
+ badge: badge.to_hash,
35
+ score: score
36
+ }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,46 @@
1
+ module Traitify
2
+ module Request
3
+ def get(path, options = {})
4
+ request(:get, path, options)
5
+ end
6
+
7
+ def post(path, options = {})
8
+ request(:post, path, options)
9
+ end
10
+
11
+ def put(path, options = {})
12
+ request(:put, path, options)
13
+ end
14
+
15
+ def delete(path, options = {})
16
+ request(:delete, path, options)
17
+ end
18
+
19
+ def last_response
20
+ @last_response
21
+ end
22
+
23
+ private
24
+ def request(method, path, options = {})
25
+ path = "#{self.api_version}#{path}"
26
+ connection_options = {}.merge!(url: self.api_host)
27
+
28
+ response = connection(connection_options).send(method) do |request|
29
+ request.body = options.to_json if options
30
+ case method
31
+ when :get
32
+ request.url(path)
33
+ when :post
34
+ request.url(path)
35
+ when :put
36
+ request.url(path)
37
+ when :delete
38
+ request.url(path)
39
+ end
40
+ end
41
+
42
+ @last_response = response
43
+ response.body
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,31 @@
1
+ module Traitify
2
+ class Result
3
+ attr_accessor :personality_blend, :personality_types
4
+
5
+ def initialize(options = {})
6
+ self.personality_blend = options[:personality_blend]
7
+ self.personality_types = options[:personality_types]
8
+ end
9
+
10
+ def self.parse_json(result)
11
+ if result
12
+ personality_blend = PersonalityBlend.parse_json(result["personality_blend"])
13
+ personality_types = result["personality_types"].collect { |p| p && p["personality_type"] ? PersonalityType.parse_json(p["personality_type"].merge!("score" => p["score"])) : nil }
14
+
15
+ Result.new(
16
+ personality_blend: personality_blend,
17
+ personality_types: personality_types
18
+ )
19
+ else
20
+ nil
21
+ end
22
+ end
23
+
24
+ def to_hash
25
+ {
26
+ personality_blend: personality_blend.to_hash,
27
+ personality_types: personality_types.collect { |p| p.to_hash }
28
+ }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,95 @@
1
+ module Traitify
2
+ class Slide
3
+ attr_accessor :id, :position, :caption, :image_desktop, :image_desktop_retina, :image_phone_landscape, :image_phone_portrait, :response, :time_taken, :completed_at, :created_at
4
+
5
+ def initialize(options = {})
6
+ self.id = options[:id]
7
+ self.position = options[:position]
8
+ self.caption = options[:caption]
9
+ self.image_desktop = options[:image_desktop]
10
+ self.image_desktop_retina = options[:image_desktop_retina]
11
+ self.image_phone_landscape = options[:image_phone_landscape]
12
+ self.image_phone_portrait = options[:image_phone_portrait]
13
+ self.response = options[:response]
14
+ self.time_taken = options[:time_taken]
15
+ self.completed_at = options[:completed_at]
16
+ self.created_at = options[:created_at]
17
+ end
18
+
19
+ def self.parse_json(slide)
20
+ created_at = slide["created_at"] ? Time.at(slide["created_at"]) : nil
21
+ completed_at = slide["completed_at"] ? Time.at(slide["completed_at"]) : nil
22
+
23
+ Slide.new(
24
+ id: slide["id"],
25
+ position: slide["position"],
26
+ caption: slide["caption"],
27
+ image_desktop: slide["image_desktop"],
28
+ image_desktop_retina: slide["image_desktop_retina"],
29
+ image_phone_landscape: slide["image_phone_landscape"],
30
+ image_phone_portrait: slide["image_phone_portrait"],
31
+ response: slide["response"],
32
+ time_taken: slide["time_taken"],
33
+ completed_at: completed_at,
34
+ created_at: created_at
35
+ )
36
+ end
37
+
38
+ def to_hash
39
+ {
40
+ id: id,
41
+ position: position,
42
+ caption: caption,
43
+ image_desktop: image_desktop,
44
+ image_desktop_retina: image_desktop_retina,
45
+ image_phone_landscape: image_phone_landscape,
46
+ image_phone_portrait: image_phone_portrait,
47
+ response: response,
48
+ time_taken: time_taken,
49
+ completed_at: completed_at,
50
+ created_at: created_at
51
+ }
52
+ end
53
+
54
+ def to_update_params
55
+ {
56
+ id: id,
57
+ response: response,
58
+ time_taken: time_taken
59
+ }
60
+ end
61
+ end
62
+
63
+ class Slides
64
+ attr_accessor :assessment_id, :all
65
+
66
+ def initialize(options = {})
67
+ self.assessment_id = options[:assessment_id]
68
+ self.all = options[:all]
69
+ end
70
+
71
+ def self.parse_json(assessment_id, slides)
72
+ Slides.new(
73
+ assessment_id: assessment_id,
74
+ all: slides.collect { |slide| Traitify::Slide.parse_json(slide) }
75
+ )
76
+ end
77
+
78
+ def to_hash
79
+ {
80
+ assessment_id: assessment_id,
81
+ all: all.collect { |slide| slide.to_hash }
82
+ }
83
+ end
84
+
85
+ def to_update_params
86
+ all.collect { |slide| slide.to_update_params }
87
+ end
88
+
89
+ # Allows slides to forward Array methods to all
90
+ def method_missing(method, *args)
91
+ return all.send(method, *args) if all.respond_to?(method)
92
+ super
93
+ end
94
+ end
95
+ end