Class: FakeApi::FakeApiData

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_api/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFakeApiData

Returns a new instance of FakeApiData.



10
11
12
13
# File 'lib/fake_api/data.rb', line 10

def initialize
  @responses = {}
  @routes    = {}
end

Instance Attribute Details

#responsesObject (readonly)

Returns the value of attribute responses.



4
5
6
# File 'lib/fake_api/data.rb', line 4

def responses
  @responses
end

#routesObject (readonly)

Returns the value of attribute routes.



4
5
6
# File 'lib/fake_api/data.rb', line 4

def routes
  @routes
end

Class Method Details

.instanceObject



6
7
8
# File 'lib/fake_api/data.rb', line 6

def FakeApiData.instance
  @@instance ||= FakeApiData.new
end

Instance Method Details

#create_list(something, amount) ⇒ Object



52
53
54
55
56
# File 'lib/fake_api/data.rb', line 52

def create_list(something, amount)
  result = []
  amount.times { result << response_or_value(something) }
  result
end

#delete(path) ⇒ Object



42
43
44
# File 'lib/fake_api/data.rb', line 42

def delete(path)
  route("DELETE", route: path)
end

#factory(name, &block) ⇒ Object



15
16
17
18
# File 'lib/fake_api/data.rb', line 15

def factory(name, &block)
  response = Factory.new(name: name, value: block)
  @responses[name] = response
end

#get(path) ⇒ Object



26
27
28
# File 'lib/fake_api/data.rb', line 26

def get(path)
  route("GET", route: path)
end

#object(something) ⇒ Object Also known as: create, build



46
47
48
# File 'lib/fake_api/data.rb', line 46

def object(something)
  response_or_value(something)
end

#patch(path) ⇒ Object



38
39
40
# File 'lib/fake_api/data.rb', line 38

def patch(path)
  route("PATCH", route: path)
end

#post(path) ⇒ Object



30
31
32
# File 'lib/fake_api/data.rb', line 30

def post(path)
  route("POST", route: path)
end

#put(path) ⇒ Object



34
35
36
# File 'lib/fake_api/data.rb', line 34

def put(path)
  route("PUT", route: path)
end

#response_or_value(e) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fake_api/data.rb', line 58

def response_or_value(e)
  if e.is_a?(Symbol)
    if response = FakeApiData.instance.responses[e]
      response.value.call
    else
      nil
    end
  else
    e
  end
end

#route(request_method, route:, &block) ⇒ Object



20
21
22
23
24
# File 'lib/fake_api/data.rb', line 20

def route(request_method, route:, &block)
  e = Route.new(route: route)
  @routes[request_method.upcase] ||= {}
  @routes[request_method.upcase][route] = e
end