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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e46e098f78366bc2bcb2e830388d57708c4bd56
4
- data.tar.gz: c42a5477374d83dec19f10575233b1c0fc495347
3
+ metadata.gz: da206c15c6eb0fb6f90153ea26b4b1b96c8184a8
4
+ data.tar.gz: 80a8882774dc330244f5211e26fc9cb365e48277
5
5
  SHA512:
6
- metadata.gz: 2f5b94c42940558f12c45afc4f456280e45597c02f9a3f95fb45651ea94de61986763b6a30881133e59da5c1363baf7b18793a658637d62b6203880588f0d3b7
7
- data.tar.gz: c5385d32a88555300c7e4eec61027dd514ea26cf1c29215f4421f0a7aad29de49c2f049c60f6e9404020a80d7e68e8725377da8228688ebe1048627513064629
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 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: { ... } }]
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
- # Return a notification info. The three levels available are: info, warn and error.
48
- add_notification 'info', 'Info subject', 'Info description'
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
- # Return a notification error.
58
- add_notification 'error', 'Error subject', '...'
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 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: { ... } }]
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
- # Return a notification info. The three levels available are: info, warn and error.
94
- add_notification 'info', 'Info subject', 'Info description'
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
- # Return a notification error.
104
- add_notification 'error', 'Error subject', '...'
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) { { message: 'sample:new',
135
- message_id: '1234567',
136
- payload: { parameters: [] } } }
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['notifications']).to have(1).item
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.message_id @message['message_id']
2
- json.error error
1
+ json.request_id @payload['request_id']
2
+ json.summary error
@@ -1,13 +1,15 @@
1
- json.message_id @message['message_id']
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.parameters @parameters do |parameter|
8
- json.name parameter[:name]
9
- json.value parameter[:value]
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
- json.messages @messages if @messages.present?
13
- json.notifications @notifications if @notifications.present?
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
- prepare_message params
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
- prepare_message parsed
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 prepare_message(hsh)
37
- @message = hsh.slice('message_id', 'message', 'payload')
36
+ def prepare_payload(hsh)
37
+ @payload = hsh
38
38
  end
39
39
 
40
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
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 << { name: name,
36
- value: value }
24
+ @parameters[name] = value
37
25
  end
38
26
 
39
- def add_notification(level, subject, description = nil, options = {})
40
- @notifications ||= []
27
+ def set_summary(summary)
28
+ add_value(:summary, summary)
29
+ end
41
30
 
42
- @notifications << { level: level,
43
- subject: subject,
44
- description: description || subject }.merge(options)
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["HTTP_X_AUGURY_TOKEN"] == ENV['ENDPOINT_KEY']
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["HTTP_X_AUGURY_TOKEN"] != ENV['ENDPOINT_KEY']
24
+ halt 401 if request.env["HTTP_X_HUB_TOKEN"] != ENV['ENDPOINT_KEY']
25
25
  end
26
26
 
27
27
  end
@@ -1,2 +1,3 @@
1
1
  require 'sinatra/jbuilder'
2
2
  require 'endpoint_base/sinatra/base'
3
+ #require 'active_support/core_ext/string/conversions'
@@ -1,3 +1,3 @@
1
1
  module EndpointBase
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2'
3
3
  end
@@ -4,7 +4,7 @@ module Spree
4
4
  ENV['ENDPOINT_KEY'] ||= '123'
5
5
 
6
6
  def auth
7
- { 'HTTP_X_AUGURY_TOKEN' => ENV['ENDPOINT_KEY'], 'CONTENT_TYPE' => 'application/json' }
7
+ { 'HTTP_X_HUB_TOKEN' => ENV['ENDPOINT_KEY'], 'CONTENT_TYPE' => 'application/json' }
8
8
  end
9
9
 
10
10
  def json_response
@@ -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
- 'message_id' => 'abc',
11
- 'payload' => { 'parameters' => config } }}
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['error'].should eq 'I see dead people'
19
- json_response['message_id'].should eq 'abc'
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['HTTP_X_AUGURY_TOKEN'] = 'wrong'
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
- 'message_id' => 'abc',
11
- 'payload' => { 'parameters' => config } }}
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['messages'].size.should == 1
20
- msg = json_response['messages'].first
21
- msg['message'].should == 'spree:order:poll'
22
- msg['payload'].should == {}
23
-
24
- json_response['parameters'].size.should == 2
25
- param = json_response['parameters'].first
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
- add_message 'spree:order:poll'
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
- add_notification 'info', 'hello world', 'today is a good day to ...'
8
+ set_summary 'today is a good day to ...'
9
9
 
10
10
  process_result 200
11
11
  end
12
12
  end
13
-
@@ -3,7 +3,7 @@ module Controllers
3
3
  mod.before do
4
4
  request.env["HTTP_ACCEPT"] = "application/json"
5
5
  request.env["CONTENT_TYPE"] = "application/json"
6
- request.env["HTTP_X_AUGURY_TOKEN"] = "x123"
6
+ request.env["HTTP_X_HUB_TOKEN"] = "x123"
7
7
  end
8
8
  end
9
9
 
@@ -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) { { :message_id => 'abc456', :payload => {} }.to_json }
6
- let(:headers) { {'HTTP_X_AUGURY_TOKEN' => 'x123', "CONTENT_TYPE" => "application/json"} }
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
- expect(::JSON.parse(last_response.body)['notifications']).to be_nil
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
- 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'
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 = { 'payload' => { 'parameters' => [{'name' => 'x', 'value' => 1}, {'name' => '2', 'value' => 3}] } }.to_json
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 '#add_messages' do
65
- it 'adds messages' do
66
- post '/add_messages', payload, headers
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['messages']).to eq([{ 'message' => 'order:new', 'payload' => { 'number' => 1 } },
71
- { 'message' => 'order:new', 'payload' => { 'number' => 2 } }])
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
- describe '#add_notifications'do
76
- it 'adds messages' do
77
- post '/add_notifications', payload, headers
66
+ it 'returns parameters set' do
67
+ post '/add_parameter', payload, headers
78
68
 
79
- response = ::JSON.parse(last_response.body)
69
+ response = ::JSON.parse(last_response.body)
70
+ expect(response['parameters']['some.param']).to eq 123
71
+ end
80
72
 
81
- expect(response['notifications']).to eq([{ 'level' => 'error', 'subject' => 'subject', 'description' => 'description', 'backtrace' => 'backtrace' }])
82
- end
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
@@ -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
- add_value :payload, @message[:payload]
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 '/deprecated' do
55
- process_result 200, @message
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 '/config' do
59
- add_value :params, @config
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 '/add_messages' do
65
- add_messages 'order:new', [ { number: 1 }, { number: 2 } ]
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 '/add_notifications' do
71
- add_notification 'error', 'subject', 'description', { backtrace: 'backtrace' }
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.1.1
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-02-06 00:00:00.000000000 Z
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json