shift-api-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 22e90c061afc5f050f592daba4e6aa3cd40a5e7b
4
+ data.tar.gz: b230e1daa6dc660f80290c7c945799e5d0b50e1d
5
+ SHA512:
6
+ metadata.gz: 0dbd4ed6ed783184ec2794f82f8d3105ed41bf285b7e1418eed2dc9e841256f7f934247ee52a876ded667bedf14aa7f102bed6784041ad6f8c66b609a7aa4447
7
+ data.tar.gz: 333d829672f168a424139bdfa700761aa73a53731dd2b8d5e006f09eeae70fba77c4421500a969b40c84564ecb356ac865cc27358fc067c7c6284f61f6b21809
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at team@shiftcommerce.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shift-api-core.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Shift Commerce Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # Shift::Api::Core
2
+
3
+ Welcome to Shift API Core - the core gem for all api access gems for the shift
4
+ commerce platform.
5
+
6
+ ## Installation
7
+
8
+ Generally, as this gem is to be used by another gem and not directly, you should
9
+ add
10
+
11
+ ```ruby
12
+ spec.add_runtime_dependency "shift-api-core"
13
+ ```
14
+
15
+ to your gem's gemspec file
16
+
17
+ ## Usage
18
+
19
+ The gem depending on this gem will generally define models to access a
20
+ shift commerce service.
21
+
22
+ A model simply looks like this :-
23
+
24
+ ```ruby
25
+ module ShiftCommerce
26
+ module Inventory
27
+ class StockLevel < ::Shift::Api::Core::Model
28
+
29
+ end
30
+ end
31
+ end
32
+
33
+ ```
34
+
35
+ This model can then be used in a similar (but not the same !) manner as an
36
+ active record model.
37
+
38
+ For example,
39
+
40
+ ```ruby
41
+ ShiftCommerce::Inventory::StockLevel.find(1)
42
+ ```
43
+
44
+ will request a "StockLevel" with an id of 1
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shiftcommerce/shift-api-core-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shift/api/core"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ require "shift/api/core/version"
2
+ require "shift/api/core/config"
3
+ require "shift/api/core/model"
4
+ require "shift/api/core/middleware"
5
+ require "shift/api/core/request_id"
6
+ require "shift/api/core/errors"
7
+ module Shift
8
+ module Api
9
+ #
10
+ # Shift::Api::Core
11
+ #
12
+ # This gem is intended for "Shift API" gem authors to use. It provides the
13
+ # base methods etc.. in order to define models.
14
+ #
15
+ module Core
16
+ ROOT_THREAD_VARS = :"shift-api-core"
17
+ # The global configuration object
18
+ # If a block is passed into this method, it is yielded with
19
+ # the config object and all actions are performed from within the
20
+ # block as a batch - any action(s) that then need performing after
21
+ # a reconfigure are done once only.
22
+ def self.config
23
+ return config_instance unless block_given?
24
+ config_instance.batch_configure do |config|
25
+ yield(config)
26
+ end
27
+ end
28
+
29
+ # Global storage per thread for the gems to use where required.
30
+ # @return [Hash] A hash which the caller is free to modify at will
31
+ def self.root_thread_vars
32
+ Thread.current[ROOT_THREAD_VARS] ||= {}
33
+ end
34
+
35
+ def self.config_instance
36
+ root_thread_vars[:config_instance] ||= Shift::Api::Core::Config.new
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,150 @@
1
+ require "singleton"
2
+ module Shift
3
+ module Api
4
+ module Core
5
+ #
6
+ # Global configuration
7
+ #
8
+ #
9
+ # A simple config might look like this:-
10
+ #
11
+ # Shift::Api::Core.config do
12
+ # config.shift_root_url = ENV["SHIFT_ROOT_URL"]
13
+ # config.logger = Rails.logger
14
+ # config.timeout = ENV["SHIFT_TIMEOUT"]
15
+ # config.open_timeout = ENV["SHIFT_OPEN_TIMEOUT"]
16
+ # end
17
+ #
18
+ #
19
+ #
20
+ # But, for testing, where the test suite wants to see data going both ways,
21
+ # and have no timeouts,
22
+ # we may have something like this :-
23
+ #
24
+ # Shift::Api::Core.config do
25
+ # config.shift_root_url = test_root_url
26
+ # config.logger = Rails.logger
27
+ # config.before_request -> (request) { do_something_with_request }
28
+ # config.after_response -> (request, response) { do_something_with_request_or_response }
29
+ # config.adapter = [:rack, application_under_test]
30
+ # config.timeout = 0
31
+ # config.open_timeout = 0
32
+ # end
33
+ #
34
+ # See detailed documentation below for more ...
35
+
36
+ class Config
37
+ # @!attribute [rw] shift_root_url
38
+ # The root url for the shift server
39
+ # @!attribute [rw] logger
40
+ # The logger to use or nil for none. Must be compatible
41
+ # with an ActiveSupport logger
42
+ # @!attribute [rw] adapter
43
+ # The adapter to use for faraday or :default for the default
44
+ # @!attribute [r] before_request_handlers
45
+ # An array of callable objects (lambda etc..) that are called with
46
+ # the request before it goes out
47
+ # defaults to []
48
+ # @!attribute [r] after_response_handlers
49
+ # An array of callable objects (lambda etc..) that are called with the
50
+ # request and response after it has been receive from the server
51
+ # defaults to []
52
+ # @!attribute [rw] headers
53
+ # Extra headers to add to every request
54
+ # defaults to {}
55
+ # Can also be an object responding to :call (i.e. a proc or lambda etc..)
56
+ # which must return a hash of headers to add
57
+ # @!attribute [rw] timeout
58
+ # The connection read timeout in seconds. If data is not received in
59
+ # this time, an error is raised.
60
+ # defaults to :default (15 seconds)
61
+ # @!attribute [rw] open_timeout
62
+ # The connection open timeout in seconds - i.e. if it takes longer than
63
+ # this to open connection, an error is raised
64
+ # defaults to :default (15 seconds)
65
+ attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout
66
+
67
+ def initialize
68
+ @before_request_handlers = []
69
+ @after_response_handlers = []
70
+ @allow_reconfigure = true
71
+ @adapter = :default
72
+ @logger = :disabled
73
+ @headers = {}
74
+ @timeout = :default
75
+ @open_timeout = :default
76
+ end
77
+
78
+ def shift_root_url=(url)
79
+ @shift_root_url = url.tap { reconfigure }
80
+ end
81
+
82
+ def logger=(logger)
83
+ @logger = logger.tap { reconfigure }
84
+ end
85
+
86
+ def adapter=(adapter)
87
+ @adapter = adapter.tap { reconfigure }
88
+ end
89
+
90
+ def headers=(headers)
91
+ @headers = headers.deep_dup.tap { reconfigure }
92
+ end
93
+
94
+ def timeout=(timeout)
95
+ @timeout = timeout.tap { reconfigure }
96
+ end
97
+
98
+ def open_timeout=(open_timeout)
99
+ @open_timeout = open_timeout.tap { reconfigure }
100
+ end
101
+
102
+ # Registers a handler that is to be called before the request is made
103
+ # to the server.
104
+ # Multiple handlers get called in the sequence they were registered
105
+ # The handlers are called with the request as the parameter
106
+ # @param handler [#call] A handler that responds to call
107
+ def before_request(handler)
108
+ @before_request_handlers << handler
109
+ reconfigure
110
+ end
111
+
112
+ # Registers a handler that is to be called after the response is returned
113
+ # from the server.
114
+ # Multiple handlers get called in the sequence they were registered
115
+ # The handlers are called with the request and the response as the parameters
116
+ # @param handler [#call] A handler that responds to call
117
+ def after_response(handler)
118
+ @after_response_handlers << handler
119
+ reconfigure
120
+ end
121
+
122
+ # As every change to the config forces all models to reconfigure, this method
123
+ # allows for batch changes where the reconfigure is done at the end of the
124
+ # passed block.
125
+ def batch_configure
126
+ disable_reconfigure
127
+ yield self
128
+ ensure
129
+ enable_reconfigure
130
+ reconfigure
131
+ end
132
+
133
+ private
134
+
135
+ def disable_reconfigure
136
+ @allow_reconfigure = false
137
+ end
138
+
139
+ def enable_reconfigure
140
+ @allow_reconfigure = true
141
+ end
142
+
143
+ def reconfigure
144
+ return unless @allow_reconfigure
145
+ Shift::Api::Core::Model.reconfigure(self)
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,76 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ # Shift::Api::Core version of the original JsonApiClient Errors
5
+ # purely so we are not exposing jsonapi client exceptions to the outside
6
+ # world in case we ever migrate from it.
7
+ module Errors
8
+ class ApiError < StandardError
9
+ attr_reader :env
10
+ def initialize(env)
11
+ @env = env
12
+ end
13
+
14
+ def self.from_jsonapi_client(ex)
15
+ new(ex.env)
16
+ end
17
+ end
18
+
19
+ class ClientError < ApiError
20
+ end
21
+
22
+ class AccessDenied < ClientError
23
+ end
24
+
25
+ class NotAuthorized < ClientError
26
+ end
27
+
28
+ class ConnectionError < ApiError
29
+ end
30
+
31
+ class ServerError < ApiError
32
+ def message
33
+ "Internal server error"
34
+ end
35
+ end
36
+
37
+ class Conflict < ServerError
38
+ def message
39
+ "Resource already exists"
40
+ end
41
+ end
42
+
43
+ class NotFound < ServerError
44
+ attr_reader :uri
45
+ def initialize(uri)
46
+ @uri = uri
47
+ end
48
+
49
+ def message
50
+ "Couldn't find resource at: #{ uri }"
51
+ end
52
+
53
+ def self.from_jsonapi_client(ex)
54
+ new(ex.uri)
55
+ end
56
+ end
57
+
58
+ class UnexpectedStatus < ServerError
59
+ attr_reader :code, :uri
60
+ def initialize(code, uri)
61
+ @code = code
62
+ @uri = uri
63
+ end
64
+
65
+ def message
66
+ "Unexpected response status: #{ code } from: #{ uri }"
67
+ end
68
+
69
+ def self.from_jsonapi_client(ex)
70
+ new(ex.code, ex.uri)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,20 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ module Formatters
5
+ module Logger
6
+ def self.message_from_request_body(env, request_id)
7
+ msg = "Shift Request (#{ request_id }): #{ env.method.to_s.upcase } to #{ env.url }"
8
+ body = env.body
9
+ msg << " with body \"#{ body }\"" unless body.empty?
10
+ msg
11
+ end
12
+
13
+ def self.message_from_response_body(env, request_id)
14
+ "Shift Response (#{ request_id }): #{ env[:raw_body] }"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ require "shift/api/core/middleware/logger"
2
+ require "shift/api/core/middleware/inspector"
3
+ require "shift/api/core/middleware/custom_headers"
4
+ require "shift/api/core/middleware/error_handler"
@@ -0,0 +1,30 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ module Middleware
5
+ #
6
+ # Faraday middleware to add extra headers to the request
7
+ #
8
+ class CustomHeaders < ::Faraday::Middleware
9
+ def initialize(app, headers:)
10
+ self.app = app
11
+ self.headers = headers
12
+ end
13
+
14
+ # Adds the custom headers to the passed in environment
15
+ # @param [Faraday::Env] env The environment from faraday
16
+ def call(env)
17
+ extra_headers = headers
18
+ extra_headers = headers.call(env) if headers.respond_to?(:call)
19
+ env.request_headers.merge!(extra_headers)
20
+ app.call(env)
21
+ end
22
+
23
+ private
24
+
25
+ attr_accessor :app, :headers
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ module Middleware
5
+ #
6
+ # Middleware to translate json api client errors to Shift::Api::Core errors
7
+ #
8
+ # This is so as to not expose json api client stuff to the outside world
9
+ #
10
+ class ErrorHandler < Faraday::Middleware
11
+ def initialize(app)
12
+ self.app = app
13
+ end
14
+
15
+ # Executes as normal but catches jsonapi client errors and translates them
16
+ # @param [Faraday::Env] env The environment from faraday
17
+ def call(env)
18
+ app.call(env)
19
+ rescue JsonApiClient::Errors::ApiError => ex
20
+ raise ex.class.name.gsub(/^JsonApiClient::/, "Shift::Api::Core::").constantize.from_jsonapi_client(ex)
21
+ end
22
+
23
+ private
24
+
25
+ attr_accessor :app
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ module Middleware
5
+ #
6
+ # Faraday middleware to add the ability to register to receive the
7
+ # request environment and/or the response environment from faraday.
8
+ # Useful for recording or even changing requests / responses
9
+ #
10
+ class Inspector < ::Faraday::Middleware
11
+ def initialize(app, before_request_handlers:, after_response_handlers:)
12
+ self.app = app
13
+ self.before_request_handlers = before_request_handlers
14
+ self.after_response_handlers = after_response_handlers
15
+ end
16
+
17
+ # Calls the provided handlers before each request and after the response
18
+ # @param [Faraday::Env] env The environment from faraday
19
+ def call(env)
20
+ notify_request_handlers(env)
21
+ request = env.dup
22
+ app.call(env).on_complete do |env|
23
+ notify_response_handlers(request, env)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def notify_request_handlers(env)
30
+ before_request_handlers.each do |handler|
31
+ handler.call(env)
32
+ request = env.dup
33
+ end
34
+ end
35
+
36
+ def notify_response_handlers(request, env)
37
+ after_response_handlers.each do |handler|
38
+ handler.call(request, env)
39
+ end
40
+ end
41
+
42
+ attr_accessor :app, :before_request_handlers, :after_response_handlers
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ require "shift/api/core/formatters/logger"
2
+ module Shift
3
+ module Api
4
+ module Core
5
+ module Middleware
6
+ #
7
+ # Faraday middleware to log requests and responses with request ids
8
+ # to a given logger.
9
+ # The logger must support the "info" method such as active support logger
10
+ #
11
+ class Logger < ::Faraday::Middleware
12
+ def initialize(app, logger:, id_generator: ::Shift::Api::Core::RequestId, formatter: ::Shift::Api::Core::Formatters::Logger)
13
+ self.app = app
14
+ self.logger = logger
15
+ self.id_generator = id_generator
16
+ self.formatter = formatter
17
+ end
18
+
19
+ # Logs the request and response to the logger
20
+ # @param [Faraday::Env] env The environment from faraday
21
+ def call(env)
22
+ request_id = id_generator.call
23
+ logger.info formatter.message_from_request_body(env, request_id)
24
+ app.call(env).on_complete do |env|
25
+ logger.info formatter.message_from_response_body(env, request_id)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ attr_accessor :app, :logger, :id_generator, :formatter
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,93 @@
1
+ require "json_api_client"
2
+ require "shift/api/core/middleware/logger"
3
+ require "shift/api/core/middleware/custom_headers"
4
+ require "shift/api/core/middleware/inspector"
5
+ require "shift/api/core/middleware/error_handler"
6
+ module Shift
7
+ module Api
8
+ module Core
9
+ # The base class for all shift api models.
10
+ #
11
+ # Defining a new model is as simple as extending this.
12
+ class Model < JsonApiClient::Resource
13
+ # Reconfigures the model and all subclasses
14
+ # this is generally called whenever the client
15
+ # is reconfigured on the fly such as in test and
16
+ # development environments.
17
+ # @param [Shift::Api::Core::Config] config The config instance to use
18
+ def self.reconfigure(config)
19
+ configure_site(config)
20
+ configure_adapter(config)
21
+ remove_connection_cache
22
+ configure_error_handler
23
+ configure_logger(config)
24
+ configure_inspector(config)
25
+ configure_timeout(config)
26
+ configure_open_timeout(config)
27
+ configure_headers(config)
28
+ reconfigure_subclasses(config)
29
+ end
30
+
31
+ private
32
+
33
+ def self.configure_site(config)
34
+ self.site = config.shift_root_url
35
+ end
36
+
37
+ def self.configure_error_handler
38
+ connection.faraday.builder.insert_before(JsonApiClient::Middleware::Status, ::Shift::Api::Core::Middleware::ErrorHandler)
39
+ end
40
+
41
+ def self.configure_adapter(config)
42
+ adapter = config.adapter
43
+ if adapter == :default
44
+ connection_options.delete(:adapter)
45
+ else
46
+ connection_options.merge!(adapter: adapter)
47
+ end
48
+ end
49
+
50
+ def self.configure_headers(config)
51
+ headers = config.headers
52
+ connection.use(::Shift::Api::Core::Middleware::CustomHeaders, headers: headers) unless headers.empty?
53
+ end
54
+
55
+ def self.configure_logger(config)
56
+ logger = config.logger
57
+ connection.use(::Shift::Api::Core::Middleware::Logger, logger: logger) unless logger == :disabled
58
+ end
59
+
60
+ def self.configure_inspector(config)
61
+ before_request_handlers = config.before_request_handlers
62
+ after_response_handlers = config.after_response_handlers
63
+ return if before_request_handlers.empty? && after_response_handlers.empty?
64
+ connection.use ::Shift::Api::Core::Middleware::Inspector,
65
+ before_request_handlers: before_request_handlers,
66
+ after_response_handlers: after_response_handlers
67
+ end
68
+
69
+ def self.configure_timeout(config)
70
+ timeout = config.timeout.to_s
71
+ timeout = "0" if timeout == "disabled"
72
+ connection.faraday.options.merge!(timeout: timeout.to_i) unless timeout == "default"
73
+ end
74
+
75
+ def self.configure_open_timeout(config)
76
+ open_timeout = config.open_timeout.to_s
77
+ open_timeout = "0" if open_timeout == "disabled"
78
+ connection.faraday.options.merge!(open_timeout: open_timeout.to_i) unless open_timeout == "default"
79
+ end
80
+
81
+ def self.reconfigure_subclasses(config)
82
+ subclasses.each do |klass|
83
+ klass.reconfigure(config)
84
+ end
85
+ end
86
+
87
+ def self.remove_connection_cache
88
+ self.connection_object = nil
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,21 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ # A utility class to generate request id numbers for logging purposes
5
+ class RequestId
6
+ # Generates the next request id
7
+ # @return [Integer] request_id
8
+ def self.call
9
+ thread_vars = Shift::Api::Core.root_thread_vars
10
+ thread_vars[:request_id] ||= 0
11
+ thread_vars[:request_id] += 1
12
+ end
13
+
14
+ # Resets the request id back to zero
15
+ def self.reset
16
+ Shift::Api::Core.root_thread_vars[:request_id] = 0
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Shift
2
+ module Api
3
+ module Core
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'shift/api/core/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'shift-api-core'
9
+ spec.version = Shift::Api::Core::VERSION
10
+ spec.authors = ['Shift Commerce Ltd']
11
+ spec.email = ['team@shiftcommerce.com']
12
+
13
+ spec.summary = 'Core gem for all shift commerce API gems'
14
+ spec.description = 'Core gem for all shift commerce API gems'
15
+ spec.homepage = 'https://github.com/shiftcommerce'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.14'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'byebug'
29
+ spec.add_development_dependency 'webmock', '~> 2.3'
30
+ spec.add_runtime_dependency 'json_api_client', '~> 1.5'
31
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shift-api-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shift Commerce Ltd
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json_api_client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.5'
97
+ description: Core gem for all shift commerce API gems
98
+ email:
99
+ - team@shiftcommerce.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/shift/api/core.rb
115
+ - lib/shift/api/core/config.rb
116
+ - lib/shift/api/core/errors.rb
117
+ - lib/shift/api/core/formatters/logger.rb
118
+ - lib/shift/api/core/middleware.rb
119
+ - lib/shift/api/core/middleware/custom_headers.rb
120
+ - lib/shift/api/core/middleware/error_handler.rb
121
+ - lib/shift/api/core/middleware/inspector.rb
122
+ - lib/shift/api/core/middleware/logger.rb
123
+ - lib/shift/api/core/model.rb
124
+ - lib/shift/api/core/request_id.rb
125
+ - lib/shift/api/core/version.rb
126
+ - shift-api-core.gemspec
127
+ homepage: https://github.com/shiftcommerce
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.6.10
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Core gem for all shift commerce API gems
151
+ test_files: []