endpoint_base 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +1 -2
  4. data/Rakefile +18 -1
  5. data/app/views/application/500.json.jbuilder +2 -0
  6. data/app/views/application/response.json.jbuilder +21 -0
  7. data/endpoint_base.gemspec +17 -6
  8. data/lib/endpoint_base.rb +29 -8
  9. data/lib/endpoint_base/concerns.rb +7 -0
  10. data/lib/endpoint_base/concerns/all.rb +15 -0
  11. data/lib/endpoint_base/concerns/exception_handler.rb +26 -0
  12. data/lib/endpoint_base/concerns/param_processor.rb +54 -0
  13. data/lib/endpoint_base/concerns/rails_responder.rb +20 -0
  14. data/lib/endpoint_base/concerns/response_dsl.rb +44 -0
  15. data/lib/endpoint_base/concerns/sinatra_responder.rb +38 -0
  16. data/lib/endpoint_base/concerns/token_authorization.rb +28 -0
  17. data/lib/endpoint_base/rails.rb +1 -0
  18. data/lib/endpoint_base/rails/engine.rb +5 -0
  19. data/lib/endpoint_base/sinatra.rb +2 -0
  20. data/lib/endpoint_base/sinatra/base.rb +11 -0
  21. data/lib/endpoint_base/sinatra/integrator_utils.rb +17 -0
  22. data/spec/rails/controllers/failing_controller_spec.rb +29 -0
  23. data/spec/rails/controllers/happy_controller_spec.rb +35 -0
  24. data/spec/rails/dummy/.gitignore +16 -0
  25. data/spec/rails/dummy/Gemfile +46 -0
  26. data/spec/rails/dummy/README.rdoc +28 -0
  27. data/spec/rails/dummy/Rakefile +6 -0
  28. data/spec/rails/dummy/app/assets/images/.keep +0 -0
  29. data/spec/rails/dummy/app/assets/javascripts/application.js +16 -0
  30. data/spec/rails/dummy/app/assets/stylesheets/application.css +13 -0
  31. data/spec/rails/dummy/app/controllers/application_controller.rb +5 -0
  32. data/spec/rails/dummy/app/controllers/concerns/.keep +0 -0
  33. data/spec/rails/dummy/app/controllers/failing_controller.rb +7 -0
  34. data/spec/rails/dummy/app/controllers/happy_controller.rb +13 -0
  35. data/spec/rails/dummy/app/helpers/application_helper.rb +2 -0
  36. data/spec/rails/dummy/app/mailers/.keep +0 -0
  37. data/spec/rails/dummy/app/models/.keep +0 -0
  38. data/spec/rails/dummy/app/models/concerns/.keep +0 -0
  39. data/spec/rails/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/spec/rails/dummy/bin/bundle +3 -0
  41. data/spec/rails/dummy/bin/rails +4 -0
  42. data/spec/rails/dummy/bin/rake +4 -0
  43. data/spec/rails/dummy/config.ru +4 -0
  44. data/spec/rails/dummy/config/application.rb +28 -0
  45. data/spec/rails/dummy/config/boot.rb +4 -0
  46. data/spec/rails/dummy/config/database.yml +25 -0
  47. data/spec/rails/dummy/config/environment.rb +5 -0
  48. data/spec/rails/dummy/config/environments/development.rb +29 -0
  49. data/spec/rails/dummy/config/environments/production.rb +80 -0
  50. data/spec/rails/dummy/config/environments/test.rb +36 -0
  51. data/spec/rails/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/rails/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  53. data/spec/rails/dummy/config/initializers/inflections.rb +16 -0
  54. data/spec/rails/dummy/config/initializers/mime_types.rb +5 -0
  55. data/spec/rails/dummy/config/initializers/secret_token.rb +12 -0
  56. data/spec/rails/dummy/config/initializers/session_store.rb +3 -0
  57. data/spec/rails/dummy/config/initializers/wrap_parameters.rb +14 -0
  58. data/spec/rails/dummy/config/locales/en.yml +23 -0
  59. data/spec/rails/dummy/config/routes.rb +4 -0
  60. data/spec/rails/dummy/db/seeds.rb +7 -0
  61. data/spec/rails/dummy/lib/assets/.keep +0 -0
  62. data/spec/rails/dummy/lib/tasks/.keep +0 -0
  63. data/spec/rails/dummy/log/.keep +0 -0
  64. data/spec/rails/dummy/public/404.html +58 -0
  65. data/spec/rails/dummy/public/422.html +58 -0
  66. data/spec/rails/dummy/public/500.html +57 -0
  67. data/spec/rails/dummy/public/favicon.ico +0 -0
  68. data/spec/rails/dummy/public/robots.txt +5 -0
  69. data/spec/rails/dummy/vendor/assets/javascripts/.keep +0 -0
  70. data/spec/rails/dummy/vendor/assets/stylesheets/.keep +0 -0
  71. data/spec/rails/spec_helper.rb +13 -0
  72. data/spec/rails/support/controllers.rb +17 -0
  73. data/spec/sinatra/endpoint_base_spec.rb +55 -0
  74. data/spec/sinatra/spec_helper.rb +55 -0
  75. data/spec/spec_helper.rb +3 -32
  76. metadata +217 -18
  77. data/lib/cross_origin.rb +0 -49
  78. data/lib/integrator_utils.rb +0 -58
  79. data/spec/cross_origin_spec.rb +0 -20
  80. data/spec/integrator_utils_spec.rb +0 -52
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 534fad9f53890503329c7a8da7faa1c4ffa934a9
4
- data.tar.gz: 64588c322e901dead79193836ad83079f64cf3df
3
+ metadata.gz: e259d563e2e585eb98590a759d3f62387f4a62bf
4
+ data.tar.gz: 914e441eca4d033b41d2ec674491d1a42294c803
5
5
  SHA512:
6
- metadata.gz: 6933b9113d0214114c308b7bcc4769107b9fe44088bdd1adab65f82e7efe76719c916d733f6d4a221c0e4fd1e1391854c1c73519e1b23d1b1a55fb6977b415cd
7
- data.tar.gz: 33a94a1b4c1cdaed3369b1e39fe4190e0dfa700ac5fc1f979dc91de441832351483d9d63f4af69d21ebb0b69705bd961215885cde9752b2ed9c77086e6e601dc
6
+ metadata.gz: b9d234ca3a75ee86537058557ec0ca8ddc550d1ab32b010d45f36373343419b234359f66a94e2ea1a001fbef2456b7a35435e8ed0c146bde293b6a06260a2e20
7
+ data.tar.gz: afd3cc84234a6d8212fa594e5b0e56f946d45333b6ece0c3a6634239b8420f3ce01a5910ee8ddccbcc7b64637e06ad5aee8a3fb1b8f7818b265bc992f2f4a972
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ tmp
17
17
  Gemfile.lock
18
18
  *.swo
19
19
  *.swp
20
+ .DS_Store
@@ -1,6 +1,5 @@
1
- script: "bundle exec rspec"
1
+ script: "bundle exec rake spec"
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  notifications:
6
5
  email:
data/Rakefile CHANGED
@@ -1 +1,18 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+
3
+ # Need to run the Rails and Sinatra specs separately so
4
+ # their dependencies won't pollute each other.
5
+
6
+ task :spec do
7
+ exitcode = 0
8
+ puts 'Running Rails specs ---------------------'
9
+ system 'FROM_RAKE=1 bundle exec rspec spec/rails'
10
+ exitcode = 1 unless $?.exitstatus == 0
11
+
12
+ puts 'Running Sinatra specs -------------------'
13
+ system 'FROM_RAKE=1 bundle exec rspec spec/sinatra'
14
+ exitcode = 1 unless $?.exitstatus == 0
15
+
16
+ exit exitcode
17
+ end
18
+
@@ -0,0 +1,2 @@
1
+ json.message_id @message['message_id']
2
+ json.error error
@@ -0,0 +1,21 @@
1
+ json.message_id @message['message_id']
2
+
3
+ @attrs.each do |name, value|
4
+ json.set! name, value
5
+ end if @attrs.present?
6
+
7
+ json.parameters @parameters do |parameter|
8
+ json.name parameter[:name]
9
+ json.value parameter[:value]
10
+ end if @parameters.present?
11
+
12
+ json.messages @messages do |message|
13
+ json.message message[:message]
14
+ json.payload message[:payload]
15
+ end if @messages.present?
16
+
17
+ json.notifications @notifications do |notification|
18
+ json.level notification[:level]
19
+ json.subject notification[:subject]
20
+ json.description notification[:description]
21
+ end if @notifications.present?
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "endpoint_base"
7
- gem.version = '0.0.1'
7
+ gem.version = '0.1.0'
8
8
  gem.authors = ["Andrew Hooker"]
9
9
  gem.email = ["andrew@spreecommerce.com"]
10
- gem.description = %q{Shared functionality for spree professional endpoints}
11
- gem.summary = %q{Spree Endpoints}
10
+ gem.description = %q{Shared functionality for SpreeCommerce hub endpoints}
11
+ gem.summary = %q{SpreeCommerce hub endpoints base library}
12
12
  gem.homepage = "http://www.spreecommerce.com"
13
13
 
14
14
  gem.files = `git ls-files`.split($/)
@@ -16,14 +16,25 @@ Gem::Specification.new do |gem|
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
18
 
19
- gem.add_dependency 'sinatra', '1.4.3'
20
- gem.add_dependency 'sinatra-contrib'
21
19
  gem.add_dependency 'json'
20
+ gem.add_dependency 'jbuilder'
22
21
  gem.add_dependency 'activesupport'
23
22
 
23
+ #sinatra
24
+ gem.add_development_dependency 'sinatra', '1.4.3'
25
+ gem.add_development_dependency 'sinatra-contrib'
26
+ gem.add_development_dependency 'tilt-jbuilder'
27
+ gem.add_development_dependency 'rack-test'
28
+
29
+ #rails
30
+ gem.add_development_dependency 'rails', "~> 4.0.0"
31
+ gem.add_development_dependency 'rspec-rails'
32
+ gem.add_development_dependency 'sqlite3'
33
+
34
+ #dev
24
35
  gem.add_development_dependency 'rake'
25
36
  gem.add_development_dependency 'rspec'
26
- gem.add_development_dependency 'rack-test'
27
37
  gem.add_development_dependency 'vcr'
38
+ gem.add_development_dependency 'simplecov'
28
39
  end
29
40
 
@@ -1,13 +1,34 @@
1
- require 'sinatra'
2
- require 'integrator_utils'
3
- require 'cross_origin'
1
+ module EndpointBase
2
+ def self.framework
3
+ if defined?(Rails)
4
+ :rails
5
+ elsif defined?(Sinatra)
6
+ :sinatra
7
+ end
8
+ end
9
+
10
+ def self.rails?
11
+ self.framework == :rails
12
+ end
4
13
 
5
- class EndpointBase < Sinatra::Base
6
- register Sinatra::IntegratorUtils
7
- register Sinatra::CrossOrigin
14
+ def self.sinatra?
15
+ self.framework == :sinatra
16
+ end
8
17
 
9
- configure do
10
- enable :cross_origin
18
+ def self.path_to_views
19
+ File.expand_path("../app/views", File.dirname(__FILE__))
11
20
  end
12
21
  end
13
22
 
23
+ require 'active_support'
24
+ require 'endpoint_base/concerns'
25
+
26
+ if EndpointBase.rails?
27
+ require 'endpoint_base/rails'
28
+ require 'jbuilder'
29
+ elsif EndpointBase.sinatra?
30
+ require 'endpoint_base/sinatra'
31
+ else
32
+ puts '[Endpoint Base] Neither Rails or Sinatra are defined, you must manually require the relevant endpoint_base files.'
33
+ end
34
+
@@ -0,0 +1,7 @@
1
+ require 'endpoint_base/concerns/param_processor'
2
+ require 'endpoint_base/concerns/response_dsl'
3
+ require 'endpoint_base/concerns/exception_handler'
4
+ require 'endpoint_base/concerns/token_authorization'
5
+ require 'endpoint_base/concerns/sinatra_responder'
6
+ require 'endpoint_base/concerns/rails_responder'
7
+ require 'endpoint_base/concerns/all'
@@ -0,0 +1,15 @@
1
+ module EndpointBase::Concerns
2
+ module All
3
+ extend ActiveSupport::Concern
4
+
5
+ include EndpointBase::Concerns::TokenAuthorization
6
+ include EndpointBase::Concerns::ParamProcessor
7
+ include EndpointBase::Concerns::ResponseDSL
8
+ include EndpointBase::Concerns::ExceptionHandler
9
+
10
+ if EndpointBase.rails?
11
+ include EndpointBase::Concerns::RailsResponder
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,26 @@
1
+ module EndpointBase::Concerns
2
+ module ExceptionHandler
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if EndpointBase.rails?
7
+ rescue_from Exception, :with => :exception_handler
8
+ elsif EndpointBase.sinatra?
9
+ error do
10
+ puts env['sinatra.error'].message
11
+ puts env['sinatra.error'].backtrace
12
+ end
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def exception_handler(exception)
19
+ Rails.logger.error exception.backtrace
20
+ render status: 500, action: '500.json.jbuilder',
21
+ locals: { error: exception.message }
22
+ return false
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ require 'json'
2
+
3
+ module EndpointBase::Concerns
4
+ module ParamProcessor
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ if EndpointBase.rails?
9
+ helper Helpers
10
+
11
+ before_action do
12
+ prepare_message params
13
+ prepare_config params
14
+ end
15
+
16
+ elsif EndpointBase.sinatra?
17
+ helpers Helpers
18
+
19
+ before do
20
+ if request.post?
21
+ begin
22
+ parsed = ::JSON.parse(request.body.read).with_indifferent_access
23
+ rescue Exception => e
24
+ halt 406
25
+ end
26
+
27
+ prepare_message parsed
28
+ prepare_config parsed
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def prepare_message(hsh)
37
+ @message = hsh.slice('message_id', 'payload')
38
+ end
39
+
40
+ def prepare_config(hsh)
41
+ @config = hsh[:payload]['parameters'] || []
42
+ @config = @config.inject({}) do |result, param|
43
+ result[param[:name]] = param[:value]
44
+ result
45
+ end
46
+ end
47
+
48
+ module Helpers
49
+ def store_id
50
+ @config['store_id']
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ module EndpointBase::Concerns
2
+ module RailsResponder
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if EndpointBase.rails?
7
+ include Helpers
8
+ elsif EndpointBase.sinatra?
9
+ raise 'This Concern is only intended to be used with Rails.'
10
+ end
11
+ end
12
+
13
+ module Helpers
14
+ def process_result(code)
15
+ render "#{EndpointBase.path_to_views}/application/response.json", status: code
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,44 @@
1
+ module EndpointBase::Concerns
2
+ module ResponseDSL
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if EndpointBase.rails?
7
+ include Helpers
8
+ elsif EndpointBase.sinatra?
9
+ helpers Helpers
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ module Helpers
16
+ def add_value(name, value)
17
+ @attrs ||= {}
18
+ @attrs[name] = value
19
+ end
20
+
21
+ def add_message(message, payload = {})
22
+ @messages ||= []
23
+
24
+ @messages << { message: message,
25
+ payload: payload }
26
+ end
27
+
28
+ def add_parameter(name, value)
29
+ @parameters ||= []
30
+
31
+ @parameters << { name: name,
32
+ value: value }
33
+ end
34
+
35
+ def add_notification(level, subject, description, options = {})
36
+ @notifications ||= []
37
+
38
+ @notifications << { level: level,
39
+ subject: subject,
40
+ description: description }.merge(options)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,38 @@
1
+ module EndpointBase::Concerns
2
+ module SinatraResponder
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if EndpointBase.rails?
7
+ raise 'This Concern is only intended to be used with Sinatra.'
8
+ elsif EndpointBase.sinatra?
9
+ helpers Helpers
10
+
11
+ before do
12
+ if request.get? && request.path_info == '/'
13
+ redirect '/endpoint.json'
14
+ end
15
+ end
16
+
17
+ get '/auth' do
18
+ status 200
19
+ end
20
+ end
21
+ end
22
+
23
+ module Helpers
24
+ def process_result(code, response = nil)
25
+ status code
26
+ content_type 'application/json', :charset => 'utf-8'
27
+
28
+ if response
29
+ warn '[DEPRECATION WARNING] Passing a hash as a second argument to `process_result` is deprecated, use Response DSL instead.'
30
+ @attrs = response
31
+ end
32
+
33
+ jbuilder :'response.json', views: "#{EndpointBase.path_to_views}/application"
34
+ end
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,28 @@
1
+ module EndpointBase::Concerns
2
+ module TokenAuthorization
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if EndpointBase.rails?
7
+ before_action :authorize_rails
8
+ elsif EndpointBase.sinatra?
9
+ before { authorize_sinatra }
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def authorize_rails
16
+ unless request.headers["HTTP_X_AUGURY_TOKEN"] == ENV['ENDPOINT_KEY']
17
+ render status: 401, json: {text: 'unauthorized'}
18
+ return false
19
+ end
20
+ end
21
+
22
+ def authorize_sinatra
23
+ return unless request.post?
24
+ halt 401 if request.env["HTTP_X_AUGURY_TOKEN"] != ENV['ENDPOINT_KEY']
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1 @@
1
+ require 'endpoint_base/rails/engine'
@@ -0,0 +1,5 @@
1
+ module EndpointBase::Rails
2
+ class Engine < ::Rails::Engine
3
+ include EndpointBase::Concerns::RailsResponder
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'sinatra/jbuilder'
2
+ require 'endpoint_base/sinatra/base'
@@ -0,0 +1,11 @@
1
+ module EndpointBase
2
+ module Sinatra
3
+ class Base < ::Sinatra::Base
4
+ include EndpointBase::Concerns::TokenAuthorization
5
+ include EndpointBase::Concerns::ParamProcessor
6
+ include EndpointBase::Concerns::ResponseDSL
7
+ include EndpointBase::Concerns::SinatraResponder
8
+ include EndpointBase::Concerns::ExceptionHandler
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module EndpointBase::Sinatra
2
+ module IntegratorUtils
3
+
4
+ def self.registered(app)
5
+
6
+ app.before do
7
+ if request.get? && request.path_info == '/'
8
+ redirect '/endpoint.json'
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+
16
+ #register IntegratorUtils
17
+ end