Class: Api
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Api
- Defined in:
- lib/correole/api.rb
Constant Summary collapse
- UNSUBSCRIBE_PATH =
'/unsubscribe'
- SUBSCRIBERS_ALLOWED_METHODS =
'PUT, DELETE, OPTIONS'
- SUBSCRIBERS_ALLOWED_ORIGIN =
'*'
- UNSUBSCRIBE_ALLOWED_METHODS =
'GET, OPTIONS'
- UNSUBSCRIBE_ALLOWED_ORIGIN =
'*'
Instance Method Summary collapse
- #subscribe(params) ⇒ Object
- #subscribers_method_not_allowed ⇒ Object
- #unsubscribe(params) ⇒ Object
- #unsubscribe_method_not_allowed ⇒ Object
Instance Method Details
#subscribe(params) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/correole/api.rb', line 21 def subscribe(params) response.headers['Access-Control-Allow-Origin'] = SUBSCRIBERS_ALLOWED_ORIGIN s = Subscriber.new(email: params[:email]) return 400 if not s.valid? begin s.save logger.info("Subscribed #{params[:email]}.") rescue ActiveRecord::RecordNotUnique logger.info("Already subscribed #{params[:email]}.") Subscriber.find_by_email(params[:email]).touch end [201, "#{params[:email]}\n"] end |
#subscribers_method_not_allowed ⇒ Object
49 50 51 52 53 |
# File 'lib/correole/api.rb', line 49 def subscribers_method_not_allowed response.headers['Allow'] = SUBSCRIBERS_ALLOWED_METHODS response.headers['Access-Control-Allow-Methods'] = SUBSCRIBERS_ALLOWED_METHODS 405 end |
#unsubscribe(params) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/correole/api.rb', line 35 def unsubscribe(params) response.headers['Access-Control-Allow-Origin'] = UNSUBSCRIBE_ALLOWED_ORIGIN s = Subscriber.new(email: params[:email]) return 400 if not s.valid? s = Subscriber.find_by_email(params[:email]) if s != nil s.delete logger.info("Unsubscribed #{params[:email]}.") else logger.info("Tried to unsubscribe #{params[:email]} but address is not subscribed.") end "#{params[:email]}\n" end |
#unsubscribe_method_not_allowed ⇒ Object
55 56 57 58 59 |
# File 'lib/correole/api.rb', line 55 def unsubscribe_method_not_allowed response.headers['Allow'] = UNSUBSCRIBE_ALLOWED_METHODS response.headers['Access-Control-Allow-Methods'] = UNSUBSCRIBE_ALLOWED_METHODS 405 end |