endpoint_base 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e259d563e2e585eb98590a759d3f62387f4a62bf
4
- data.tar.gz: 914e441eca4d033b41d2ec674491d1a42294c803
3
+ metadata.gz: 4e46e098f78366bc2bcb2e830388d57708c4bd56
4
+ data.tar.gz: c42a5477374d83dec19f10575233b1c0fc495347
5
5
  SHA512:
6
- metadata.gz: b9d234ca3a75ee86537058557ec0ca8ddc550d1ab32b010d45f36373343419b234359f66a94e2ea1a001fbef2456b7a35435e8ed0c146bde293b6a06260a2e20
7
- data.tar.gz: afd3cc84234a6d8212fa594e5b0e56f946d45333b6ece0c3a6634239b8420f3ce01a5910ee8ddccbcc7b64637e06ad5aee8a3fb1b8f7818b265bc992f2f4a972
6
+ metadata.gz: 2f5b94c42940558f12c45afc4f456280e45597c02f9a3f95fb45651ea94de61986763b6a30881133e59da5c1363baf7b18793a658637d62b6203880588f0d3b7
7
+ data.tar.gz: c5385d32a88555300c7e4eec61027dd514ea26cf1c29215f4421f0a7aad29de49c2f049c60f6e9404020a80d7e68e8725377da8228688ebe1048627513064629
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # EndpointBase
2
2
 
3
- TODO: Write a gem description
3
+ Shared functionality for SpreeCommerce Hub Endpoints.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,25 +18,100 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ ### Sinatra
22
+
23
+ #### Gemfile
24
+
21
25
  ```ruby
22
- require 'endpoint_base'
26
+ gem 'sinatra'
27
+ gem 'tilt', '~> 1.4.1'
28
+ gem 'tilt-jbuilder', require: 'sinatra/jbuilder'
29
+ # ...
30
+ gem 'endpoint_base'
31
+ ```
23
32
 
24
- class SampleEndpoint < EndpointBase
33
+ #### Endpoint
25
34
 
35
+ ```ruby
36
+ class SampleEndpoint < EndpointBase::Sinatra::Base
26
37
  post '/sample' do
27
- result = { 'message_id' => @message[:message_id],
28
- 'notifications' => [ { 'level' => 'info',
29
- 'subject' => 'New sample',
30
- 'description' => '...' } ] }
31
- process_result 200, result
38
+ # Return a message sample:new.
39
+ add_message 'sample:new', { sample: { ... } }
40
+
41
+ # Return a list of messages of the same type
42
+ add_messages 'sample:new', [{ sample: { ... } }, { sample: { ... } }]
43
+
44
+ # Create or update the parameter sample.new.
45
+ add_parameter 'sample.new', '...'
46
+
47
+ # Return a notification info. The three levels available are: info, warn and error.
48
+ add_notification 'info', 'Info subject', 'Info description'
49
+
50
+ # Return a customized key and value.
51
+ add_value 'my_customized_key', { ... }
52
+
53
+ process_result 200
54
+ end
55
+
56
+ post '/fail' do
57
+ # Return a notification error.
58
+ add_notification 'error', 'Error subject', '...'
59
+
60
+ process_result 500
61
+ end
62
+ end
63
+
64
+ ```
65
+
66
+ ### Rails
67
+
68
+ #### Gemfile
69
+
70
+ ```ruby
71
+ gem 'rails'
72
+ # ...
73
+ gem 'endpoint_base'
74
+ ```
75
+
76
+ #### Endpoint
77
+
78
+ ```ruby
79
+ class SampleController < ApplicationController
80
+ include EndpointBase::Concerns::All
81
+ skip_before_filter :verify_authenticity_token
82
+
83
+ def sample
84
+ # Return a message sample:new.
85
+ add_message 'sample:new', { sample: { ... } }
86
+
87
+ # Return a list of messages of the same type
88
+ add_messages 'sample:new', [{ sample: { ... } }, { sample: { ... } }]
89
+
90
+ # Create or update the parameter sample.new.
91
+ add_parameter 'sample.new', '...'
92
+
93
+ # Return a notification info. The three levels available are: info, warn and error.
94
+ add_notification 'info', 'Info subject', 'Info description'
95
+
96
+ # Return a customized key and value.
97
+ add_value 'my_customized_key', { ... }
98
+
99
+ process_result 200
100
+ end
101
+
102
+ def fail
103
+ # Return a notification error.
104
+ add_notification 'error', 'Error subject', '...'
105
+
106
+ process_result 500
32
107
  end
33
108
  end
34
109
 
35
110
  ```
36
111
 
37
- ## Testing
112
+ ### Testing
38
113
 
39
- EndpointBase provides a `Controllers` testing support.
114
+ ##### spec_helper.rb
40
115
 
41
116
  ```ruby
42
117
  # ...
@@ -50,7 +125,7 @@ RSpec.configure do |config|
50
125
  end
51
126
  ```
52
127
 
53
- Which enables you to use `json_response` and `auth` in your Endpoint tests. It also sets `ENV['ENDPOINT_KEY'] ||= '123'`.
128
+ `Spree:TestingSupport::Controllers` enables you to use `json_response` and `auth` in your Endpoint tests. It also sets `ENV['ENDPOINT_KEY'] ||= '123'`.
54
129
 
55
130
  ```ruby
56
131
  require 'spec_helper'
@@ -68,8 +143,8 @@ describe SampleEndpoint do
68
143
 
69
144
  expect(json_response['notifications']).to have(1).item
70
145
  expect(json_response['notifications'].first).to eq ({ 'level' => 'info',
71
- 'subject' => 'New Sample',
72
- 'description' => '...' })
146
+ 'subject' => 'Info subject',
147
+ 'description' => 'Info description' })
73
148
  end
74
149
  end
75
150
  end
data/Rakefile CHANGED
@@ -3,16 +3,16 @@ require 'bundler/gem_tasks'
3
3
  # Need to run the Rails and Sinatra specs separately so
4
4
  # their dependencies won't pollute each other.
5
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
6
+ task spec: [:spec_sinatra, :spec_rails]
11
7
 
8
+ task :spec_sinatra do
12
9
  puts 'Running Sinatra specs -------------------'
13
10
  system 'FROM_RAKE=1 bundle exec rspec spec/sinatra'
14
- exitcode = 1 unless $?.exitstatus == 0
15
-
16
- exit exitcode
11
+ exit exitcode unless exitcode = $?.exitstatus == 0
17
12
  end
18
13
 
14
+ task :spec_rails do
15
+ puts 'Running Rails specs ---------------------'
16
+ system 'FROM_RAKE=1 bundle exec rspec spec/rails'
17
+ exit exitcode unless exitcode = $?.exitstatus == 0
18
+ end
@@ -9,13 +9,5 @@ json.parameters @parameters do |parameter|
9
9
  json.value parameter[:value]
10
10
  end if @parameters.present?
11
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?
12
+ json.messages @messages if @messages.present?
13
+ json.notifications @notifications if @notifications.present?
@@ -1,15 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
+
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
 
6
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[lib endpoint_base version]))
7
+
5
8
  Gem::Specification.new do |gem|
6
- gem.name = "endpoint_base"
7
- gem.version = '0.1.0'
8
- gem.authors = ["Andrew Hooker"]
9
- gem.email = ["andrew@spreecommerce.com"]
9
+ gem.name = 'endpoint_base'
10
+ gem.version = EndpointBase::VERSION
11
+ gem.authors = ['Andrew Hooker']
12
+ gem.email = ['andrew@spreecommerce.com']
10
13
  gem.description = %q{Shared functionality for SpreeCommerce hub endpoints}
11
14
  gem.summary = %q{SpreeCommerce hub endpoints base library}
12
- gem.homepage = "http://www.spreecommerce.com"
15
+ gem.homepage = 'http://www.spreecommerce.com'
13
16
 
14
17
  gem.files = `git ls-files`.split($/)
15
18
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -34,7 +34,7 @@ module EndpointBase::Concerns
34
34
  private
35
35
 
36
36
  def prepare_message(hsh)
37
- @message = hsh.slice('message_id', 'payload')
37
+ @message = hsh.slice('message_id', 'message', 'payload')
38
38
  end
39
39
 
40
40
  def prepare_config(hsh)
@@ -18,11 +18,15 @@ module EndpointBase::Concerns
18
18
  @attrs[name] = value
19
19
  end
20
20
 
21
- def add_message(message, payload = {})
21
+ def add_message(message, payload = {}, extra = {})
22
22
  @messages ||= []
23
23
 
24
24
  @messages << { message: message,
25
- payload: payload }
25
+ payload: payload }.merge(extra)
26
+ end
27
+
28
+ def add_messages(message, collection, extra = {})
29
+ collection.each { |payload| add_message(message, payload, extra) }
26
30
  end
27
31
 
28
32
  def add_parameter(name, value)
@@ -32,12 +36,12 @@ module EndpointBase::Concerns
32
36
  value: value }
33
37
  end
34
38
 
35
- def add_notification(level, subject, description, options = {})
39
+ def add_notification(level, subject, description = nil, options = {})
36
40
  @notifications ||= []
37
41
 
38
42
  @notifications << { level: level,
39
43
  subject: subject,
40
- description: description }.merge(options)
44
+ description: description || subject }.merge(options)
41
45
  end
42
46
  end
43
47
  end
@@ -8,9 +8,11 @@ module EndpointBase::Concerns
8
8
  elsif EndpointBase.sinatra?
9
9
  helpers Helpers
10
10
 
11
+ set :public_folder, './public'
12
+
11
13
  before do
12
14
  if request.get? && request.path_info == '/'
13
- redirect '/endpoint.json'
15
+ redirect "#{request.script_name}/endpoint.json"
14
16
  end
15
17
  end
16
18
 
@@ -0,0 +1,3 @@
1
+ module EndpointBase
2
+ VERSION = '0.1.1'
3
+ end
@@ -13,6 +13,9 @@ module EndpointBase::Sinatra
13
13
  it 'accepts POST with auth' do
14
14
  post '/', payload, headers
15
15
  expect(last_response).to be_ok
16
+
17
+ expect(::JSON.parse(last_response.body)['notifications']).to be_nil
18
+ expect(::JSON.parse(last_response.body)['messages']).to be_nil
16
19
  end
17
20
 
18
21
  it 'redirects to endpoint.json for GET on root url' do
@@ -21,6 +24,12 @@ module EndpointBase::Sinatra
21
24
  expect(last_response.location).to eq 'http://example.org/endpoint.json'
22
25
  end
23
26
 
27
+ it 'redirect to relative root endpoint.json for GET on mapped root url' do
28
+ get '/myapp/', nil, {}
29
+ expect(last_response.status).to eq 302
30
+ expect(last_response.location).to eq 'http://example.org/myapp/endpoint.json'
31
+ end
32
+
24
33
  it 'returns a 200 for /auth check' do
25
34
  get '/auth', {}, headers
26
35
  expect(last_response).to be_ok
@@ -51,5 +60,26 @@ module EndpointBase::Sinatra
51
60
 
52
61
  expect(last_response.content_type).to eq 'application/json;charset=utf-8'
53
62
  end
63
+
64
+ describe '#add_messages' do
65
+ it 'adds messages' do
66
+ post '/add_messages', payload, headers
67
+
68
+ response = ::JSON.parse(last_response.body)
69
+
70
+ expect(response['messages']).to eq([{ 'message' => 'order:new', 'payload' => { 'number' => 1 } },
71
+ { 'message' => 'order:new', 'payload' => { 'number' => 2 } }])
72
+ end
73
+ end
74
+
75
+ describe '#add_notifications'do
76
+ it 'adds messages' do
77
+ post '/add_notifications', payload, headers
78
+
79
+ response = ::JSON.parse(last_response.body)
80
+
81
+ expect(response['notifications']).to eq([{ 'level' => 'error', 'subject' => 'subject', 'description' => 'description', 'backtrace' => 'backtrace' }])
82
+ end
83
+ end
54
84
  end
55
85
  end
@@ -1,4 +1,5 @@
1
1
  require 'simplecov'
2
+
2
3
  SimpleCov.start do
3
4
  add_filter 'spec/spec_helper'
4
5
  add_filter 'lib/endpoint_base.rb'
@@ -20,7 +21,15 @@ require File.join(File.dirname(__FILE__), '../..', 'lib', 'endpoint_base', 'sina
20
21
  Sinatra::Base.environment = 'test'
21
22
 
22
23
  def app
23
- TestEndpoint
24
+
25
+ Rack::Builder.new do
26
+ map '/' do
27
+ run TestEndpoint
28
+ end
29
+ map '/myapp' do
30
+ run TestEndpoint
31
+ end
32
+ end.to_app
24
33
  end
25
34
 
26
35
  RSpec.configure do |config|
@@ -30,7 +39,6 @@ end
30
39
  ENV['ENDPOINT_KEY'] = 'x123'
31
40
 
32
41
  class TestEndpoint < EndpointBase::Sinatra::Base
33
-
34
42
  set :logging, true
35
43
 
36
44
  post '/' do
@@ -52,4 +60,16 @@ class TestEndpoint < EndpointBase::Sinatra::Base
52
60
 
53
61
  process_result 200
54
62
  end
63
+
64
+ post '/add_messages' do
65
+ add_messages 'order:new', [ { number: 1 }, { number: 2 } ]
66
+
67
+ process_result 200
68
+ end
69
+
70
+ post '/add_notifications' do
71
+ add_notification 'error', 'subject', 'description', { backtrace: 'backtrace' }
72
+
73
+ process_result 200
74
+ end
55
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: endpoint_base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Hooker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-28 00:00:00.000000000 Z
11
+ date: 2014-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -237,7 +237,7 @@ files:
237
237
  - lib/endpoint_base/rails/engine.rb
238
238
  - lib/endpoint_base/sinatra.rb
239
239
  - lib/endpoint_base/sinatra/base.rb
240
- - lib/endpoint_base/sinatra/integrator_utils.rb
240
+ - lib/endpoint_base/version.rb
241
241
  - lib/spree/testing_support/controllers.rb
242
242
  - spec/rails/controllers/failing_controller_spec.rb
243
243
  - spec/rails/controllers/happy_controller_spec.rb
@@ -1,17 +0,0 @@
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