Class: PagerJudy::API::FakeApp
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- PagerJudy::API::FakeApp
- Defined in:
- lib/pager_judy/api/fake_api_app.rb
Overview
This little Rack app is designed to fake the PagerDuty REST API.
It’s pretty much just CRUD:
GET /COLLECTION
POST /COLLECTION
GET /COLLECTION/ID
PUT /COLLECTION/ID
Instance Attribute Summary collapse
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_type ⇒ Object
- #item ⇒ Object
- #item_data ⇒ Object
- #item_id ⇒ Object
- #item_type ⇒ Object
- #sub_collection ⇒ Object
- #sub_collection_type ⇒ Object
- #sub_item ⇒ Object
- #sub_item_data ⇒ Object
- #sub_item_exists? ⇒ Boolean
- #sub_item_id ⇒ Object
- #sub_item_type ⇒ Object
Instance Attribute Details
#db ⇒ Object
19 20 21 22 23 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 19 def db # The "database" is just a big Hash. We make it an instance variable, # so it sticks around between requests. @db ||= {} end |
Instance Method Details
#collection ⇒ Object
31 32 33 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 31 def collection db[collection_type] ||= {} end |
#collection_type ⇒ Object
27 28 29 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 27 def collection_type params["collection_type"] end |
#item ⇒ Object
43 44 45 46 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 43 def item return_error(404, "#{item_type} #{item_id} not found") unless collection.key?(item_id) collection.fetch(item_id) end |
#item_data ⇒ Object
48 49 50 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 48 def item_data item.merge("id" => item_id) end |
#item_id ⇒ Object
39 40 41 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 39 def item_id @item_id ||= params["item_id"] end |
#item_type ⇒ Object
35 36 37 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 35 def item_type collection_type.chomp("s") end |
#sub_collection ⇒ Object
56 57 58 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 56 def sub_collection item[sub_collection_type] ||= {} end |
#sub_collection_type ⇒ Object
52 53 54 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 52 def sub_collection_type params["sub_collection_type"] end |
#sub_item ⇒ Object
72 73 74 75 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 72 def sub_item return_error(404, "#{sub_item_type} #{sub_item_id} not found") unless sub_collection.key?(sub_item_id) sub_collection.fetch(sub_item_id) end |
#sub_item_data ⇒ Object
77 78 79 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 77 def sub_item_data sub_collection.fetch(sub_item_id).merge("id" => sub_item_id) end |
#sub_item_exists? ⇒ Boolean
68 69 70 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 68 def sub_item_exists? sub_collection.key?(sub_item_id) end |
#sub_item_id ⇒ Object
64 65 66 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 64 def sub_item_id @sub_item_id ||= params["sub_item_id"] end |
#sub_item_type ⇒ Object
60 61 62 |
# File 'lib/pager_judy/api/fake_api_app.rb', line 60 def sub_item_type sub_collection_type.chomp("s") end |