Class: Clever::FakeServer

Inherits:
Object
  • Object
show all
Defined in:
lib/clever/fake_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ FakeServer

Returns a new instance of FakeServer.



6
7
8
# File 'lib/clever/fake_server.rb', line 6

def initialize opts
  @api_key = opts[:api_key]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/clever/fake_server.rb', line 30

def method_missing(sym, *args, &block)
  if Clever::FakeJson.const_defined? sym.to_s.upcase
    return get_json(args[0], Clever::FakeJson.const_get(sym.to_s.upcase))
  end

  super
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



4
5
6
# File 'lib/clever/fake_server.rb', line 4

def api_key
  @api_key
end

Instance Method Details

#get_json(verb, json) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/clever/fake_server.rb', line 38

def get_json verb, json
  unless verb == "GET"
    return Clever::FakeResponse.new("405", "")
  end

  Clever::FakeResponse.new("200", json)
end

#request(req, body = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clever/fake_server.rb', line 10

def request(req, body=nil)
  return Clever::FakeResponse.new("404", "") unless req.path.start_with? "/v1.1/"

  path, params = req.path.split('?')
  resource, id, action = path.gsub(/(\/v1\.1\/|\?$)/, "").split('/')

  if action.nil? && id.nil?
    path = resource
  elsif id.nil?
    path = resource.gsub(/s$/, '')
  else
    path = resource.gsub(/s$/, '') + (action.nil? ? '' : "_#{action}")
  end

  self.send(path, req.method)

  #rescue => exception
  #  return Clever::FakeResponse.new("500", exception.message)
end