Class: PagerJudy::API::FakeApp

Inherits:
Sinatra::Base
  • Object
show all
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

Instance Attribute Details

#dbObject



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

#collectionObject



31
32
33
# File 'lib/pager_judy/api/fake_api_app.rb', line 31

def collection
  db[collection_type] ||= {}
end

#collection_typeObject



27
28
29
# File 'lib/pager_judy/api/fake_api_app.rb', line 27

def collection_type
  params["collection_type"]
end

#itemObject



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_dataObject



48
49
50
# File 'lib/pager_judy/api/fake_api_app.rb', line 48

def item_data
  item.merge("id" => item_id)
end

#item_idObject



39
40
41
# File 'lib/pager_judy/api/fake_api_app.rb', line 39

def item_id
  @item_id ||= params["item_id"]
end

#item_typeObject



35
36
37
# File 'lib/pager_judy/api/fake_api_app.rb', line 35

def item_type
  collection_type.chomp("s")
end

#sub_collectionObject



56
57
58
# File 'lib/pager_judy/api/fake_api_app.rb', line 56

def sub_collection
  item[sub_collection_type] ||= {}
end

#sub_collection_typeObject



52
53
54
# File 'lib/pager_judy/api/fake_api_app.rb', line 52

def sub_collection_type
  params["sub_collection_type"]
end

#sub_itemObject



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_dataObject



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

Returns:

  • (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_idObject



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_typeObject



60
61
62
# File 'lib/pager_judy/api/fake_api_app.rb', line 60

def sub_item_type
  sub_collection_type.chomp("s")
end