traitify 1.8.4 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +11 -0
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +5 -1
  5. data/Gemfile.lock +109 -42
  6. data/README.md +50 -25
  7. data/lib/traitify/client/connection.rb +23 -0
  8. data/lib/traitify/client/model.rb +45 -0
  9. data/lib/traitify/client/overrides.rb +27 -0
  10. data/lib/traitify/client/request.rb +33 -0
  11. data/lib/traitify/client/setup.rb +25 -0
  12. data/lib/traitify/client.rb +20 -27
  13. data/lib/traitify/configuration.rb +4 -12
  14. data/lib/traitify/data.rb +18 -0
  15. data/lib/traitify/error.rb +18 -16
  16. data/lib/traitify/middleware/formatter.rb +52 -0
  17. data/lib/traitify/middleware/raise_error.rb +38 -0
  18. data/lib/traitify/response.rb +44 -0
  19. data/lib/traitify/version.rb +1 -1
  20. data/lib/traitify.rb +29 -1
  21. data/paths.yml +1352 -0
  22. data/spec/spec_helper.rb +14 -6
  23. data/spec/support/mocks/aggregate.json +7 -0
  24. data/spec/support/mocks/assessment.json +1 -1
  25. data/spec/support/mocks/assessment_analytics.json +7 -0
  26. data/spec/support/mocks/blank.json +0 -0
  27. data/spec/support/mocks/career.json +1 -2
  28. data/spec/support/mocks/careers.json +2 -3
  29. data/spec/support/mocks/decks.json +1 -4
  30. data/spec/support/mocks/locale.json +10 -0
  31. data/spec/support/mocks/locales.json +12 -0
  32. data/spec/support/mocks/profile.json +5 -0
  33. data/spec/support/mocks/profiles.json +5 -0
  34. data/spec/support/mocks/trait_analytics.json +7 -0
  35. data/spec/support/mocks/type_analytics.json +7 -0
  36. data/spec/traitify/client/examples/analytics_spec.rb +69 -0
  37. data/spec/{traitify-ruby/client → traitify/client/examples}/assessment_spec.rb +19 -14
  38. data/spec/traitify/client/examples/career_spec.rb +52 -0
  39. data/spec/traitify/client/examples/configuration_spec.rb +29 -0
  40. data/spec/traitify/client/examples/deck_spec.rb +29 -0
  41. data/spec/traitify/client/examples/locale_spec.rb +39 -0
  42. data/spec/{traitify-ruby/client → traitify/client/examples}/major_spec.rb +12 -11
  43. data/spec/traitify/client/examples/profiles_spec.rb +66 -0
  44. data/spec/traitify/client/examples/result_spec.rb +130 -0
  45. data/spec/traitify/client/examples/slide_spec.rb +95 -0
  46. data/spec/traitify/client/model_spec.rb +73 -0
  47. data/spec/traitify/client/request_spec.rb +85 -0
  48. data/spec/traitify/client/setup_spec.rb +62 -0
  49. data/spec/traitify/client_spec.rb +52 -0
  50. data/spec/traitify/data_spec.rb +105 -0
  51. data/spec/traitify/error_spec.rb +117 -0
  52. data/spec/traitify/response_spec.rb +82 -0
  53. data/spec/traitify/version_spec.rb +7 -0
  54. data/traitify.gemspec +14 -12
  55. metadata +115 -53
  56. data/lib/traitify/client/assessments.rb +0 -29
  57. data/lib/traitify/client/careers.rb +0 -25
  58. data/lib/traitify/client/decks.rb +0 -16
  59. data/lib/traitify/client/majors.rb +0 -25
  60. data/lib/traitify/client/results.rb +0 -37
  61. data/lib/traitify/client/slides.rb +0 -34
  62. data/lib/traitify/connection.rb +0 -33
  63. data/lib/traitify/request.rb +0 -18
  64. data/spec/traitify-ruby/client/career_spec.rb +0 -51
  65. data/spec/traitify-ruby/client/configuration_spec.rb +0 -51
  66. data/spec/traitify-ruby/client/deck_spec.rb +0 -28
  67. data/spec/traitify-ruby/client/result_spec.rb +0 -93
  68. data/spec/traitify-ruby/client/slide_spec.rb +0 -82
@@ -3,33 +3,35 @@ module Traitify
3
3
  attr_accessor :response
4
4
 
5
5
  def self.from(response)
6
- if klass = case response.status
7
- when 400 then Traitify::BadRequest
8
- when 401 then Traitify::Unauthorized
9
- when 404 then Traitify::NotFound
10
- when 422 then Traitify::UnprocessableEntity
11
- when 500..505 then Traitify::ServerError
12
- end
13
- klass.new(response)
6
+ klass = case response.status
7
+ when 400 then Traitify::BadRequest
8
+ when 401 then Traitify::Unauthorized
9
+ when 404 then Traitify::NotFound
10
+ when 422 then Traitify::UnprocessableEntity
11
+ when 500..505 then Traitify::ServerError
14
12
  end
13
+
14
+ klass.new(response) if klass
15
15
  end
16
16
 
17
17
  def initialize(response)
18
18
  @response = response
19
- super(error_message)
20
- end
21
19
 
22
- def errors
23
- response.body
20
+ super(error_message)
24
21
  end
25
22
 
26
23
  private
24
+
27
25
  def error_message
28
- message = "#{response.method.upcase} | "
29
- message << "#{response.url.to_s} | "
26
+ message = "#{response.request.http_method.upcase} | "
27
+ message << "#{response.request.url} | "
30
28
  message << "#{response.status} | "
31
- message << (response.body.is_a?(Hash) ?
32
- "#{response.body["message"]}" : "#{response.body.first["message"]}")
29
+
30
+ error = response.body
31
+ error = error.first if error.is_a?(Array)
32
+ error = error.message if error.respond_to?(:message)
33
+
34
+ message << error.to_s if error.present?
33
35
  message
34
36
  end
35
37
  end
@@ -0,0 +1,52 @@
1
+ module Traitify
2
+ module Middleware
3
+ class Formatter
4
+ extend Forwardable
5
+
6
+ attr_accessor :logger, :options
7
+
8
+ def initialize(logger:, options:)
9
+ @logger = logger
10
+ @options = {log_level: :info, prefix: "[traitify-ruby]"}.merge(options)
11
+ end
12
+
13
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
14
+
15
+ def request(env)
16
+ log(env[:method].upcase, env.url.to_s)
17
+ log("AUTH", env.request_headers["Authorization"], level: :debug)
18
+ log("BODY", env.body)
19
+ log("PARAMS", query_params(env))
20
+ end
21
+
22
+ def response(env)
23
+ log("STATUS", env.status)
24
+ end
25
+
26
+ private
27
+
28
+ def log(name, object, level: nil)
29
+ return unless object
30
+
31
+ log = proc {
32
+ "#{name}: #{object.respond_to?(:to_str) ? object.to_str : object.pretty_inspect&.strip}"
33
+ }
34
+
35
+ public_send(level || log_level, options[:prefix], &log)
36
+ end
37
+
38
+ def log_level
39
+ unless [:debug, :info, :warn, :error, :fatal].include?(options[:log_level])
40
+ return :info
41
+ end
42
+
43
+ options[:log_level]
44
+ end
45
+
46
+ def query_params(env)
47
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
48
+ env.params_encoder.decode(env.url.query)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ # NOTE: Traitified version of Faraday error response middleware
2
+ # https://github.com/lostisland/faraday/blob/b3b922949d14f604ed11dc0f749455fc062777e7/lib/faraday/response/raise_error.rb
3
+ module Traitify
4
+ module Middleware
5
+ class RaiseError < Faraday::Middleware
6
+ def on_complete(env)
7
+ error = Traitify::Error.from(error_data(env))
8
+
9
+ raise error if error
10
+ end
11
+
12
+ private
13
+
14
+ def error_data(env)
15
+ Traitify::Data.new(
16
+ body: env.body,
17
+ headers: env.response_headers,
18
+ request: {
19
+ body: env.request_body,
20
+ headers: env.request_headers,
21
+ http_method: env[:method],
22
+ query: query_params(env),
23
+ url: env.url,
24
+ url_path: env.url.path
25
+ },
26
+ status: env.status
27
+ )
28
+ end
29
+
30
+ def query_params(env)
31
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
32
+ env.params_encoder.decode(env.url.query)
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ Faraday::Response.register_middleware(raise_traitify_error: Traitify::Middleware::RaiseError)
@@ -0,0 +1,44 @@
1
+ module Traitify
2
+ class Response
3
+ attr_reader :request
4
+
5
+ def initialize(request)
6
+ @request = request
7
+ end
8
+
9
+ def data
10
+ @data ||= Traitify::Data.new(request.body)
11
+ end
12
+
13
+ def page
14
+ @page ||= begin
15
+ page = {}
16
+
17
+ if request.env.response_headers["link"]
18
+ links = request.env.response_headers["link"].split(",")
19
+ if prev_link = links.find{ |link| link.include?("rel=\"prev\"") }
20
+ prev_link = prev_link.split(/>|</)[1]
21
+ page[:previous] = {
22
+ url: prev_link,
23
+ params: CGI.parse(prev_link.split("?")[1..-1].join("?"))
24
+ }
25
+ end
26
+
27
+ if next_link = links.find{ |link| link.include?("rel=\"next\"") }
28
+ next_link = next_link.split(/>|</)[1]
29
+ page[:next] = {
30
+ url: next_link,
31
+ params: CGI.parse(next_link.split("?")[1..-1].join("?"))
32
+ }
33
+ end
34
+ end
35
+
36
+ Traitify::Data.new page
37
+ end
38
+ end
39
+
40
+ def total
41
+ @total ||= request.env.response_headers["x-total-count"]
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module Traitify
2
- VERSION = "1.8.4"
2
+ VERSION = "2.0.1".freeze
3
3
  end
data/lib/traitify.rb CHANGED
@@ -1,13 +1,41 @@
1
- require "traitify/version"
2
1
  require "traitify/configuration"
3
2
  require "traitify/client"
3
+ require "traitify/data"
4
+ require "traitify/error"
5
+ require "traitify/response"
6
+ require "traitify/version"
7
+ require "logger"
8
+ require "active_support"
9
+ require "active_support/core_ext/object/deep_dup"
4
10
 
5
11
  module Traitify
6
12
  extend Configuration
7
13
 
14
+ self.host = "http://api-sandbox.traitify.com"
15
+ self.version = "v1"
16
+ self.locale_key = "en-us"
17
+
8
18
  class << self
19
+ attr_writer :logger
20
+
9
21
  def new(options = {})
10
22
  Traitify::Client.new(options)
11
23
  end
24
+
25
+ def logger
26
+ @logger ||= Logger.new($stdout).tap do |log|
27
+ log.progname = name
28
+ end
29
+ end
30
+
31
+ def log(level, message)
32
+ message = "[traitify-ruby] #{message}"
33
+ case level
34
+ when :debug
35
+ logger.debug message
36
+ else
37
+ logger.info message
38
+ end
39
+ end
12
40
  end
13
41
  end