Created
August 20, 2015 03:16
-
-
Save bchase/a109b3c02412107aed07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Organization class that acts AR-like (::create, ::all, ::find_by) | |
| class Organization | |
| attr_reader :name, :api_key, :api_secret, :list | |
| def initialize(name:, api_key:, api_secret:, list: []) | |
| @name, @api_key, @api_secret, @list= name, api_key, api_secret, list | |
| end | |
| def self.create(*args) | |
| new(*args).tap do |org| | |
| all.push org | |
| end | |
| end | |
| def self.all | |
| @@all ||= [] | |
| end | |
| def self.find_by(api_key:, api_secret:) | |
| all.find {|org| | |
| org.api_key == api_key && | |
| org.api_secret == api_secret | |
| } | |
| end | |
| end | |
| module Service | |
| def self.call(env) | |
| Controller | |
| .new(env) | |
| .response | |
| end | |
| class Controller | |
| def initialize(env) | |
| @env = env | |
| end | |
| def response | |
| authorized? ? list : unauthorized | |
| end | |
| def list | |
| [ 200, {'Content-Type' => 'text/plain'}, [content] ] | |
| end | |
| def unauthorized | |
| [ 401, {'Content-Type' => 'text/plain'}, ['Unauthorized'] ] | |
| end | |
| def content | |
| organization.list.join(', ') | |
| end | |
| def authorized? | |
| !!organization | |
| end | |
| def organization | |
| Organization.find_by \ | |
| api_key: request.params['api_key'], | |
| api_secret: request.params['api_secret'] | |
| end | |
| def request | |
| @request ||= Rack::Request.new(@env) | |
| end | |
| end | |
| end | |
| Organization.create \ | |
| name: 'University of Toledo', | |
| api_key: 'foo', | |
| api_secret: 'bar', | |
| list: [ 'Ben', 'Brad' ] | |
| run Service | |
| # # run the server with... | |
| # $ rackup | |
| # | |
| # | |
| # # req w/ invalid creds | |
| # | |
| # $ curl "localhost:9292?api_key=baz&api_secret=boo" | |
| # Unauthorized% | |
| # | |
| # | |
| # # req w/ valid creds | |
| # | |
| # $ curl "localhost:9292?api_key=foo&api_secret=bar" | |
| # Ben, Brad% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Service | |
| WHITELISTED_DOMAINS = [ | |
| # 'localhost' # TODO uncomment to allow request from `localhost` | |
| ] | |
| def call(env) | |
| @env = env | |
| authorized? ? response : unauthorized | |
| end | |
| def authorized? | |
| WHITELISTED_DOMAINS.include? request.host | |
| end | |
| def request | |
| @request ||= Rack::Request.new(@env) | |
| end | |
| def unauthorized | |
| [ 401, {'Content-Type' => 'text/plain'}, ['Unauthorized'] ] | |
| end | |
| def response | |
| [ 200, {'Content-Type' => 'text/plain'}, ['List of Mentors'] ] | |
| end | |
| end | |
| run Service.new | |
| # # run the server with... | |
| # $ rackup | |
| # | |
| # | |
| # # from another terminal | |
| # | |
| # $ curl localhost:9292 | |
| # Unauthorized% | |
| # $ | |
| # | |
| # # 1 - uncomment the TODO line above | |
| # # 2 - restart the server | |
| # # 3 - rerun the `curl` cmd | |
| # | |
| # $ curl localhost:9292 | |
| # List of Mentors% | |
| # $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment