cangaroo_endpoint_base 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ecc8e671ea559bacd80dcd6f9443cfb0745979a
4
- data.tar.gz: ab1de70e27da70953e0d837a5758ddef1392b774
3
+ metadata.gz: 538f446a2f03323020baad74065151a667249fbe
4
+ data.tar.gz: 738db85b363d8a829ccca56b012bed61447e059a
5
5
  SHA512:
6
- metadata.gz: 10fdab2caafb5e536c246d5ee4bec3eb81e4919787c288353980f2df7bef6e02088b0f9b6811114355e0b9d46bebfb4b3d1b77b1e57f910570021e6e4a0b041d
7
- data.tar.gz: 2d414757b42ce4a0b5d5b739b9b19c75f70971fa87cc9441e321a24f0d1c797f9f16a0b75344b525b3b7c853c100990c6d9c1fb0eab7585611e802d4d1335d8d
6
+ metadata.gz: 5ff0f984caa8865bea4e07960fd2381413fdbf916656189c4039952e66169b4ba1d6d9622b7bc3ab0d80984bf18efc87273a8619436c84575892d87ea09c4b04
7
+ data.tar.gz: 65a4bbdb6fef996e4d0a7dc217a858fc4f3129a4018b2d021540d86505be016c22b2ac46f8f503170c5e5c35df0938a0f3eb42917078c96aaa5d8146754506fc
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  vendor/bundle
11
+ *.gem
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # CangarooEndpointBase
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cangaroo_endpoint_base`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Simple gem with a application helper to easily build wombat/cangaroo endpoints for system integrations.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
- And then execute:
18
+ In your `.env`:
16
19
 
17
- $ bundle
18
-
19
- Or install it yourself as:
20
+ ```shell
21
+ export ENDPOINT_BASIC_AUTH_TOKEN=123123
22
+ ```
20
23
 
21
- $ gem install cangaroo_endpoint_base
24
+ This token will be used as the password for HTTP Basic authentication against this endpoint.
22
25
 
23
26
  ## Usage
24
27
 
25
- TODO: Write usage instructions here
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
-
@@ -1,5 +1,6 @@
1
1
  require "cangaroo_endpoint_base/version"
2
2
  require 'cangaroo_endpoint_base/application_controller_helper'
3
+ require 'cangaroo_endpoint_base/user_error'
3
4
 
4
5
  require 'simple_structured_logger'
5
6
 
@@ -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 if !Rails.env.test?
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 last_poll_from_params
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
- if Rails.env.production? && last_poll < 2.week.ago
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
@@ -0,0 +1,5 @@
1
+ module CangarooEndpointBase
2
+ class UserError < ::StandardError
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module CangarooEndpointBase
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  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.0
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: 2016-08-19 00:00:00.000000000 Z
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.6.4
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