endpoint_base 0.1.1 → 0.2
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 +4 -4
- data/README.md +20 -25
- data/app/views/application/500.json.jbuilder +2 -2
- data/app/views/application/response.json.jbuilder +8 -6
- data/lib/endpoint_base/concerns/param_processor.rb +10 -8
- data/lib/endpoint_base/concerns/response_dsl.rb +8 -19
- data/lib/endpoint_base/concerns/sinatra_responder.rb +0 -8
- data/lib/endpoint_base/concerns/token_authorization.rb +2 -2
- data/lib/endpoint_base/sinatra.rb +1 -0
- data/lib/endpoint_base/version.rb +1 -1
- data/lib/spree/testing_support/controllers.rb +1 -1
- data/spec/rails/controllers/failing_controller_spec.rb +5 -5
- data/spec/rails/controllers/happy_controller_spec.rb +9 -17
- data/spec/rails/dummy/app/controllers/happy_controller.rb +2 -3
- data/spec/rails/support/controllers.rb +1 -1
- data/spec/sinatra/endpoint_base_spec.rb +22 -27
- data/spec/sinatra/spec_helper.rb +20 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da206c15c6eb0fb6f90153ea26b4b1b96c8184a8
|
4
|
+
data.tar.gz: 80a8882774dc330244f5211e26fc9cb365e48277
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94780b9489d958c57b438acffe2a58cf4b58b2143c05825e6100d17569aed7eb549441062885845b1af533b06fae3c376d152e9b05e273c51a0ff61c2b6d1ea2
|
7
|
+
data.tar.gz: b713616f278915b1590b161e081f3aab21d59afe706e8beb1135bc516941086770c07ecacc23ac1e7836d8d60220348a9aa79904df40282246bf435157dfcfbc
|
data/README.md
CHANGED
@@ -35,28 +35,27 @@ gem 'endpoint_base'
|
|
35
35
|
```ruby
|
36
36
|
class SampleEndpoint < EndpointBase::Sinatra::Base
|
37
37
|
post '/sample' do
|
38
|
-
# Return
|
39
|
-
|
40
|
-
|
41
|
-
# Return a list of messages of the same type
|
42
|
-
add_messages 'sample:new', [{ sample: { ... } }, { sample: { ... } }]
|
38
|
+
# Return an order object.
|
39
|
+
add_object :order, { id: 1, email: 'test@example.com' }
|
43
40
|
|
44
41
|
# Create or update the parameter sample.new.
|
45
42
|
add_parameter 'sample.new', '...'
|
46
43
|
|
47
|
-
#
|
48
|
-
|
44
|
+
# Set the notification summary.
|
45
|
+
set_summary 'The order was imported correctly'
|
49
46
|
|
50
47
|
# Return a customized key and value.
|
51
48
|
add_value 'my_customized_key', { ... }
|
52
49
|
|
50
|
+
#return the relevant HTTP status code
|
53
51
|
process_result 200
|
54
52
|
end
|
55
53
|
|
56
54
|
post '/fail' do
|
57
|
-
#
|
58
|
-
|
55
|
+
# Set the notification summary.
|
56
|
+
set_summary 'The order failed to imported'
|
59
57
|
|
58
|
+
#return the relevant HTTP status code
|
60
59
|
process_result 500
|
61
60
|
end
|
62
61
|
end
|
@@ -81,28 +80,27 @@ class SampleController < ApplicationController
|
|
81
80
|
skip_before_filter :verify_authenticity_token
|
82
81
|
|
83
82
|
def sample
|
84
|
-
# Return
|
85
|
-
|
86
|
-
|
87
|
-
# Return a list of messages of the same type
|
88
|
-
add_messages 'sample:new', [{ sample: { ... } }, { sample: { ... } }]
|
83
|
+
# Return an order object.
|
84
|
+
add_object :order, { id: 1, email: 'test@example.com' }
|
89
85
|
|
90
86
|
# Create or update the parameter sample.new.
|
91
87
|
add_parameter 'sample.new', '...'
|
92
88
|
|
93
|
-
#
|
94
|
-
|
89
|
+
# Set the notification summary.
|
90
|
+
set_summary 'The order was imported correctly'
|
95
91
|
|
96
92
|
# Return a customized key and value.
|
97
93
|
add_value 'my_customized_key', { ... }
|
98
94
|
|
95
|
+
#return the relevant HTTP status code
|
99
96
|
process_result 200
|
100
97
|
end
|
101
98
|
|
102
99
|
def fail
|
103
|
-
#
|
104
|
-
|
100
|
+
# Set the notification summary.
|
101
|
+
set_summary 'The order failed to imported'
|
105
102
|
|
103
|
+
#return the relevant HTTP status code
|
106
104
|
process_result 500
|
107
105
|
end
|
108
106
|
end
|
@@ -131,9 +129,9 @@ end
|
|
131
129
|
require 'spec_helper'
|
132
130
|
|
133
131
|
describe SampleEndpoint do
|
134
|
-
let(:request) { {
|
135
|
-
|
136
|
-
|
132
|
+
let(:request) { { request_id: '1234567',
|
133
|
+
order: {},
|
134
|
+
parameters: [] } }
|
137
135
|
|
138
136
|
describe '/sample' do
|
139
137
|
it 'notifies a new sample' do
|
@@ -141,10 +139,7 @@ describe SampleEndpoint do
|
|
141
139
|
|
142
140
|
expect(last_response).to be_ok
|
143
141
|
|
144
|
-
expect(json_response['
|
145
|
-
expect(json_response['notifications'].first).to eq ({ 'level' => 'info',
|
146
|
-
'subject' => 'Info subject',
|
147
|
-
'description' => 'Info description' })
|
142
|
+
expect(json_response['summary']).to eq "Order was successfully imported"
|
148
143
|
end
|
149
144
|
end
|
150
145
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
json.
|
2
|
-
json.
|
1
|
+
json.request_id @payload['request_id']
|
2
|
+
json.summary error
|
@@ -1,13 +1,15 @@
|
|
1
|
-
json.
|
1
|
+
json.request_id @payload['request_id']
|
2
2
|
|
3
3
|
@attrs.each do |name, value|
|
4
4
|
json.set! name, value
|
5
5
|
end if @attrs.present?
|
6
6
|
|
7
|
-
json.
|
8
|
-
|
9
|
-
|
7
|
+
json.set! :parameters do
|
8
|
+
@parameters.each do |key, value|
|
9
|
+
json.set! key.to_sym, value
|
10
|
+
end
|
10
11
|
end if @parameters.present?
|
11
12
|
|
12
|
-
|
13
|
-
json.
|
13
|
+
@objects.each do |klass, objects|
|
14
|
+
json.set! klass, objects
|
15
|
+
end if @objects.present?
|
@@ -9,7 +9,7 @@ module EndpointBase::Concerns
|
|
9
9
|
helper Helpers
|
10
10
|
|
11
11
|
before_action do
|
12
|
-
|
12
|
+
prepare_payload params
|
13
13
|
prepare_config params
|
14
14
|
end
|
15
15
|
|
@@ -24,7 +24,7 @@ module EndpointBase::Concerns
|
|
24
24
|
halt 406
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
prepare_payload parsed
|
28
28
|
prepare_config parsed
|
29
29
|
end
|
30
30
|
end
|
@@ -33,16 +33,18 @@ module EndpointBase::Concerns
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
-
def
|
37
|
-
@
|
36
|
+
def prepare_payload(hsh)
|
37
|
+
@payload = hsh
|
38
38
|
end
|
39
39
|
|
40
40
|
def prepare_config(hsh)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
if hsh.key? 'parameters'
|
42
|
+
if hsh['parameters'].is_a? Hash
|
43
|
+
@config = hsh['parameters']
|
44
|
+
end
|
45
45
|
end
|
46
|
+
|
47
|
+
@config || {}
|
46
48
|
end
|
47
49
|
|
48
50
|
module Helpers
|
@@ -18,30 +18,19 @@ module EndpointBase::Concerns
|
|
18
18
|
@attrs[name] = value
|
19
19
|
end
|
20
20
|
|
21
|
-
def add_message(message, payload = {}, extra = {})
|
22
|
-
@messages ||= []
|
23
|
-
|
24
|
-
@messages << { message: message,
|
25
|
-
payload: payload }.merge(extra)
|
26
|
-
end
|
27
|
-
|
28
|
-
def add_messages(message, collection, extra = {})
|
29
|
-
collection.each { |payload| add_message(message, payload, extra) }
|
30
|
-
end
|
31
|
-
|
32
21
|
def add_parameter(name, value)
|
33
|
-
@parameters ||=
|
22
|
+
@parameters ||= {}
|
34
23
|
|
35
|
-
@parameters
|
36
|
-
value: value }
|
24
|
+
@parameters[name] = value
|
37
25
|
end
|
38
26
|
|
39
|
-
def
|
40
|
-
|
27
|
+
def set_summary(summary)
|
28
|
+
add_value(:summary, summary)
|
29
|
+
end
|
41
30
|
|
42
|
-
|
43
|
-
|
44
|
-
|
31
|
+
def add_object(klass, object)
|
32
|
+
@objects ||= Hash.new {|h,k| h[k] = []}
|
33
|
+
@objects[klass.to_s.pluralize] << object
|
45
34
|
end
|
46
35
|
end
|
47
36
|
end
|
@@ -8,14 +8,6 @@ module EndpointBase::Concerns
|
|
8
8
|
elsif EndpointBase.sinatra?
|
9
9
|
helpers Helpers
|
10
10
|
|
11
|
-
set :public_folder, './public'
|
12
|
-
|
13
|
-
before do
|
14
|
-
if request.get? && request.path_info == '/'
|
15
|
-
redirect "#{request.script_name}/endpoint.json"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
11
|
get '/auth' do
|
20
12
|
status 200
|
21
13
|
end
|
@@ -13,7 +13,7 @@ module EndpointBase::Concerns
|
|
13
13
|
private
|
14
14
|
|
15
15
|
def authorize_rails
|
16
|
-
unless request.headers["
|
16
|
+
unless request.headers["HTTP_X_HUB_TOKEN"] == ENV['ENDPOINT_KEY']
|
17
17
|
render status: 401, json: {text: 'unauthorized'}
|
18
18
|
return false
|
19
19
|
end
|
@@ -21,7 +21,7 @@ module EndpointBase::Concerns
|
|
21
21
|
|
22
22
|
def authorize_sinatra
|
23
23
|
return unless request.post?
|
24
|
-
halt 401 if request.env["
|
24
|
+
halt 401 if request.env["HTTP_X_HUB_TOKEN"] != ENV['ENDPOINT_KEY']
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
@@ -7,20 +7,20 @@ describe FailingController, type: 'controller' do
|
|
7
7
|
{ 'name' => 'spree.api_version', 'value' => '2.0' }] }
|
8
8
|
|
9
9
|
let(:message) {{ 'store_id' => '123229227575e4645c000001',
|
10
|
-
'
|
11
|
-
'
|
10
|
+
'request_id' => 'abc',
|
11
|
+
'parameters' => config }}
|
12
12
|
|
13
13
|
|
14
14
|
it "renders the 500.json page on exceptions" do
|
15
15
|
post :index, message
|
16
16
|
|
17
17
|
response.code.should eq '500'
|
18
|
-
json_response['
|
19
|
-
json_response['
|
18
|
+
json_response['summary'].should eq 'I see dead people'
|
19
|
+
json_response['request_id'].should eq 'abc'
|
20
20
|
end
|
21
21
|
|
22
22
|
it "return 401 on authorization failues" do
|
23
|
-
request.env['
|
23
|
+
request.env['HTTP_X_HUB_TOKEN'] = 'wrong'
|
24
24
|
post :index, message
|
25
25
|
|
26
26
|
response.code.should eq '401'
|
@@ -7,8 +7,8 @@ describe HappyController, type: 'controller' do
|
|
7
7
|
{ 'name' => 'spree.api_version', 'value' => '2.0' }] }
|
8
8
|
|
9
9
|
let(:message) {{ 'store_id' => '123229227575e4645c000001',
|
10
|
-
'
|
11
|
-
'
|
10
|
+
'requet_id' => 'abc',
|
11
|
+
'parameters' => config }}
|
12
12
|
|
13
13
|
|
14
14
|
it 'render populated response' do
|
@@ -16,20 +16,12 @@ describe HappyController, type: 'controller' do
|
|
16
16
|
|
17
17
|
response.code.should eq '200'
|
18
18
|
|
19
|
-
json_response['
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
param['name'].should == 'spree.order_poll.last_updated_at'
|
27
|
-
param['value'].should == 'today'
|
28
|
-
|
29
|
-
json_response['notifications'].size.should == 1
|
30
|
-
note = json_response['notifications'].first
|
31
|
-
note['level'].should == 'info'
|
32
|
-
note['subject'].should == 'hello world'
|
33
|
-
note['description'].should == 'today is a good day to ...'
|
19
|
+
json_response['products'].size.should == 1
|
20
|
+
product = json_response['products'].first
|
21
|
+
product['id'].should == 1
|
22
|
+
|
23
|
+
json_response['parameters']['spree.order_poll.last_updated_at'].should == 'today'
|
24
|
+
|
25
|
+
json_response['summary'].should == 'today is a good day to ...'
|
34
26
|
end
|
35
27
|
end
|
@@ -2,12 +2,11 @@ class HappyController < ApplicationController
|
|
2
2
|
include EndpointBase::Concerns::All
|
3
3
|
|
4
4
|
def index
|
5
|
-
|
5
|
+
add_object :product, { id: 1, name: 'Test Product' }
|
6
6
|
add_parameter 'spree.order_poll.last_updated_at', 'today'
|
7
7
|
add_parameter 'spree.order_poll.first_updated_at', 'yesterday'
|
8
|
-
|
8
|
+
set_summary 'today is a good day to ...'
|
9
9
|
|
10
10
|
process_result 200
|
11
11
|
end
|
12
12
|
end
|
13
|
-
|
@@ -2,8 +2,8 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
2
|
|
3
3
|
module EndpointBase::Sinatra
|
4
4
|
describe Base do
|
5
|
-
let(:payload) { { :
|
6
|
-
let(:headers) { {'
|
5
|
+
let(:payload) { { :request_id => 'abc456', :order => {id: 'R123'} }.to_json }
|
6
|
+
let(:headers) { {'HTTP_X_HUB_TOKEN' => 'x123', "CONTENT_TYPE" => "application/json"} }
|
7
7
|
|
8
8
|
it 'rejects request without auth' do
|
9
9
|
post '/', payload
|
@@ -14,20 +14,10 @@ module EndpointBase::Sinatra
|
|
14
14
|
post '/', payload, headers
|
15
15
|
expect(last_response).to be_ok
|
16
16
|
|
17
|
-
|
18
|
-
expect(::JSON.parse(last_response.body)['messages']).to be_nil
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'redirects to endpoint.json for GET on root url' do
|
22
|
-
get '/', nil, {}
|
23
|
-
expect(last_response.status).to eq 302
|
24
|
-
expect(last_response.location).to eq 'http://example.org/endpoint.json'
|
25
|
-
end
|
17
|
+
response = ::JSON.parse(last_response.body)
|
26
18
|
|
27
|
-
|
28
|
-
|
29
|
-
expect(last_response.status).to eq 302
|
30
|
-
expect(last_response.location).to eq 'http://example.org/myapp/endpoint.json'
|
19
|
+
expect(response['orders']).to be_nil
|
20
|
+
expect(response['request_id']).to eq 'abc456'
|
31
21
|
end
|
32
22
|
|
33
23
|
it 'returns a 200 for /auth check' do
|
@@ -47,7 +37,7 @@ module EndpointBase::Sinatra
|
|
47
37
|
end
|
48
38
|
|
49
39
|
it 'finds parameters' do
|
50
|
-
body = { '
|
40
|
+
body = { 'parameters' => {'x' => 1, '2' => 3} }.to_json
|
51
41
|
post '/config', body, headers
|
52
42
|
|
53
43
|
response = ::JSON.parse(last_response.body)['params']
|
@@ -61,25 +51,30 @@ module EndpointBase::Sinatra
|
|
61
51
|
expect(last_response.content_type).to eq 'application/json;charset=utf-8'
|
62
52
|
end
|
63
53
|
|
64
|
-
describe '#
|
65
|
-
it 'adds
|
66
|
-
post '/
|
54
|
+
describe '#add_objects' do
|
55
|
+
it 'adds objects' do
|
56
|
+
post '/add_objects', payload, headers
|
67
57
|
|
68
58
|
response = ::JSON.parse(last_response.body)
|
69
59
|
|
70
|
-
expect(response['
|
71
|
-
|
60
|
+
expect(response['orders']).to eq([{ 'id' => 1, 'email' => 'test@example.com' },
|
61
|
+
{ 'id' => 2, 'email' => 'spree@example.com' }])
|
62
|
+
expect(response['products']).to eq([{ 'id' => 1, 'sku' => 'ROR-123' }])
|
72
63
|
end
|
73
64
|
end
|
74
65
|
|
75
|
-
|
76
|
-
|
77
|
-
post '/add_notifications', payload, headers
|
66
|
+
it 'returns parameters set' do
|
67
|
+
post '/add_parameter', payload, headers
|
78
68
|
|
79
|
-
|
69
|
+
response = ::JSON.parse(last_response.body)
|
70
|
+
expect(response['parameters']['some.param']).to eq 123
|
71
|
+
end
|
80
72
|
|
81
|
-
|
82
|
-
|
73
|
+
it 'sets the summary' do
|
74
|
+
post '/set_summary', payload, headers
|
75
|
+
|
76
|
+
response = ::JSON.parse(last_response.body)
|
77
|
+
expect(response['summary']).to eq 'everything is ok'
|
83
78
|
end
|
84
79
|
end
|
85
80
|
end
|
data/spec/sinatra/spec_helper.rb
CHANGED
@@ -45,30 +45,41 @@ class TestEndpoint < EndpointBase::Sinatra::Base
|
|
45
45
|
process_result 200
|
46
46
|
end
|
47
47
|
|
48
|
+
#used to verify JSON parsing in param_processor
|
48
49
|
post '/payload' do
|
49
|
-
|
50
|
+
@payload.to_json
|
51
|
+
end
|
52
|
+
|
53
|
+
#used to verify parameters are correctly stored in @config
|
54
|
+
post '/config' do
|
55
|
+
add_value :params, @config
|
50
56
|
|
51
57
|
process_result 200
|
52
58
|
end
|
53
59
|
|
54
|
-
post '/
|
55
|
-
|
60
|
+
post '/add_value' do
|
61
|
+
add_value :priates, [ { id: 5, name: 'Blue Beard'},
|
62
|
+
{ id: 7, name: 'Peg Eye' } ]
|
63
|
+
|
64
|
+
process_result 200
|
56
65
|
end
|
57
66
|
|
58
|
-
post '/
|
59
|
-
|
67
|
+
post '/add_objects' do
|
68
|
+
add_object :order, { id: 1, email: 'test@example.com' }
|
69
|
+
add_object :order, { id: 2, email: 'spree@example.com' }
|
70
|
+
add_object :product, { id: 1, sku: 'ROR-123' }
|
60
71
|
|
61
72
|
process_result 200
|
62
73
|
end
|
63
74
|
|
64
|
-
post '/
|
65
|
-
|
75
|
+
post '/add_parameter' do
|
76
|
+
add_parameter 'some.param', 123
|
66
77
|
|
67
78
|
process_result 200
|
68
79
|
end
|
69
80
|
|
70
|
-
post '/
|
71
|
-
|
81
|
+
post '/set_summary' do
|
82
|
+
set_summary 'everything is ok'
|
72
83
|
|
73
84
|
process_result 200
|
74
85
|
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.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Hooker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|