cangaroo_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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 538f446a2f03323020baad74065151a667249fbe
|
4
|
+
data.tar.gz: 738db85b363d8a829ccca56b012bed61447e059a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ff0f984caa8865bea4e07960fd2381413fdbf916656189c4039952e66169b4ba1d6d9622b7bc3ab0d80984bf18efc87273a8619436c84575892d87ea09c4b04
|
7
|
+
data.tar.gz: 65a4bbdb6fef996e4d0a7dc217a858fc4f3129a4018b2d021540d86505be016c22b2ac46f8f503170c5e5c35df0938a0f3eb42917078c96aaa5d8146754506fc
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# CangarooEndpointBase
|
2
2
|
|
3
|
-
|
3
|
+
Simple gem with a application helper to easily build wombat/cangaroo endpoints for system integrations.
|
4
4
|
|
5
|
-
|
5
|
+
* Easily extract polling ranges (after, and time range) from params
|
6
|
+
* Throw errors to Rollbar
|
7
|
+
* Protect endpoint via HTTP basic auth
|
8
|
+
* Reset logging context before processing action
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -12,17 +15,36 @@ Add this line to your application's Gemfile:
|
|
12
15
|
gem 'cangaroo_endpoint_base'
|
13
16
|
```
|
14
17
|
|
15
|
-
|
18
|
+
In your `.env`:
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
```shell
|
21
|
+
export ENDPOINT_BASIC_AUTH_TOKEN=123123
|
22
|
+
```
|
20
23
|
|
21
|
-
|
24
|
+
This token will be used as the password for HTTP Basic authentication against this endpoint.
|
22
25
|
|
23
26
|
## Usage
|
24
27
|
|
25
|
-
|
28
|
+
```ruby
|
29
|
+
# application_controller.rb
|
30
|
+
class ApplicationController < ActionController::API
|
31
|
+
include CangarooEndpointBase::ApplicationControllerHelper
|
32
|
+
end
|
33
|
+
|
34
|
+
# gift_cards_controller.rb
|
35
|
+
class GiftCardsController < ApplicationController
|
36
|
+
def index
|
37
|
+
updated_after, updated_before = poll_timestamps_from_params
|
38
|
+
|
39
|
+
gift_card_poll_manager = GiftCardPollManager.new
|
40
|
+
gift_card_poll_manager.poll(GiftCard, last_poll: updated_after, updated_before: updated_before)
|
41
|
+
|
42
|
+
render json: { gift_cards: gift_card_poll_manager.updated_gift_cards }
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
```
|
26
48
|
|
27
49
|
## Development
|
28
50
|
|
@@ -33,4 +55,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
33
55
|
## Contributing
|
34
56
|
|
35
57
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cangaroo_endpoint_base.
|
36
|
-
|
@@ -8,16 +8,22 @@ module CangarooEndpointBase
|
|
8
8
|
|
9
9
|
http_basic_authenticate_with(
|
10
10
|
name: '',
|
11
|
+
# TODO this should be a gem-level config
|
11
12
|
password: ENV['ENDPOINT_BASIC_AUTH_TOKEN']
|
12
13
|
)
|
13
14
|
|
14
15
|
before_action :reset_context
|
15
16
|
|
16
17
|
rescue_from StandardError do |exception|
|
18
|
+
# NOTE throw exceptions in test, otherwise failing tests won't fail loudly
|
19
|
+
if Rails.env.test? && exception.class != CangarooEndpointBase::UserError
|
20
|
+
raise(exception)
|
21
|
+
end
|
22
|
+
|
17
23
|
Rollbar.error(exception) if defined?(::Rollbar)
|
18
24
|
|
19
25
|
render json: { :error => exception.message }, status: :internal_server_error
|
20
|
-
end
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
protected
|
@@ -26,7 +32,28 @@ module CangarooEndpointBase
|
|
26
32
|
log.reset_context!
|
27
33
|
end
|
28
34
|
|
29
|
-
def
|
35
|
+
def poll_timestamps_from_params
|
36
|
+
updated_before = updated_before_from_params_or_now
|
37
|
+
|
38
|
+
updated_after = last_poll_from_params(allow_any: updated_before.present?)
|
39
|
+
|
40
|
+
return if updated_after.nil?
|
41
|
+
|
42
|
+
if updated_after >= updated_before
|
43
|
+
render json: "updated_since later than updated_before", status: :bad_request
|
44
|
+
return
|
45
|
+
end
|
46
|
+
|
47
|
+
[updated_after, updated_before]
|
48
|
+
end
|
49
|
+
|
50
|
+
def updated_before_from_params_or_now
|
51
|
+
return DateTime.now if params[:updated_before].nil?
|
52
|
+
|
53
|
+
Time.at(params[:updated_before].to_i).to_datetime
|
54
|
+
end
|
55
|
+
|
56
|
+
def last_poll_from_params(allow_any: false)
|
30
57
|
last_poll = Time.at(params[:last_poll].to_i).to_datetime rescue nil
|
31
58
|
|
32
59
|
if last_poll.nil?
|
@@ -34,7 +61,8 @@ module CangarooEndpointBase
|
|
34
61
|
return
|
35
62
|
end
|
36
63
|
|
37
|
-
|
64
|
+
# TODO the env based limitation should be optional
|
65
|
+
if !allow_any && Rails.env.production? && last_poll < 2.week.ago
|
38
66
|
render json: "last poll should never be more than two weeks in the past", status: :bad_request
|
39
67
|
return
|
40
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cangaroo_endpoint_base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bianco
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_structured_logger
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/cangaroo_endpoint_base.rb
|
98
98
|
- lib/cangaroo_endpoint_base/application_controller_helper.rb
|
99
99
|
- lib/cangaroo_endpoint_base/spec/spec_helper.rb
|
100
|
+
- lib/cangaroo_endpoint_base/user_error.rb
|
100
101
|
- lib/cangaroo_endpoint_base/version.rb
|
101
102
|
homepage: https://github.com/iloveitaly/cangaroo_endpoint_base
|
102
103
|
licenses: []
|
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
118
|
version: '0'
|
118
119
|
requirements: []
|
119
120
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.5.1
|
121
122
|
signing_key:
|
122
123
|
specification_version: 4
|
123
124
|
summary: Helpers for developing a Cangaroo (Wombat replacement) endpoint
|