Class: Shark::RSpec::FakeSurveyService::Request

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shark/rspec/fake_survey_service/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



11
12
13
# File 'lib/shark/rspec/fake_survey_service/request.rb', line 11

def cache
  @cache
end

Class Method Details

.setupObject



13
14
15
16
17
# File 'lib/shark/rspec/fake_survey_service/request.rb', line 13

def self.setup
  instance = self.instance
  instance.stub_requests
  instance.cache = ObjectCache.instance
end

Instance Method Details

#hostObject



56
57
58
# File 'lib/shark/rspec/fake_survey_service/request.rb', line 56

def host
  Shark.configuration.survey_service.site
end

#log(message) ⇒ Object



60
61
62
# File 'lib/shark/rspec/fake_survey_service/request.rb', line 60

def log(message)
  Shark.logger.info "[Shark] #{message}"
end

#stub_requestsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shark/rspec/fake_survey_service/request.rb', line 19

def stub_requests
  WebMock.stub_request(:post, %r{^#{host}/.*$}).to_return do |request|
    log "POST #{request.uri}"
    log "Body #{request.body}"

    payload = JSON.parse(request.body)['data']
    object = cache.add(payload)

    SharkSpec.fake_response(201, data: object)
  end

  WebMock.stub_request(:delete, %r{^#{host}/.+/.+}).to_return do |request|
    log "DELETE #{request.uri}"

    id = request.uri.path.split('/')[2]

    ObjectCache.instance.remove(id)

    SharkSpec.fake_response(204, nil)
  end

  WebMock.stub_request(:get, %r{^#{host}/.+/.+}).to_return do |request|
    log "GET #{request.uri}"

    type = request.uri.path.split('/')[4]
    id = request.uri.path.split('/')[5]

    object = cache.find(id, type)

    if object.present?
      SharkSpec.fake_response(200, data: object)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end
end