Class: Kentico::Kontent::Delivery::Tests::FakeResponder

Inherits:
Object
  • Object
show all
Defined in:
lib/delivery/tests/fake_responder.rb

Constant Summary collapse

PROJECT_ID =
ENV['PROJECT_ID']
IS_SECURE =
ENV['TEST_SECURE_API_ENABLED']
SECURE_KEY =
ENV['SECURE_KEY']
PREVIEW_KEY =
ENV['PREVIEW_KEY']
BASE_URL =
"https://deliver.kontent.ai/#{PROJECT_ID}".freeze
PREVIEW_URL =
"https://preview-deliver.kontent.ai/#{PROJECT_ID}".freeze

Class Method Summary collapse

Class Method Details

.get_response(query, url, headers) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/delivery/tests/fake_responder.rb', line 18

def get_response(query, url, headers)
  @query = query
  if IS_SECURE && !(
    headers['Authorization'] == "Bearer #{SECURE_KEY}" ||
    headers['Authorization'] == "Bearer #{PREVIEW_KEY}"
  )
    return respond_401
  end

  url =
    if @query.should_preview
      url[PREVIEW_URL.length...url.length]
    else
      url[BASE_URL.length...url.length]
    end

  qs = url.contains('?') ? url.split('?')[1] : nil
  return respond_filtering qs unless qs.nil? # e.g. /items/about_us?skip=0&limit=5

  respond_generic url # Didn't match other clauses, so response should be located in corresponding filepath
end

.respond_401Object



58
59
60
61
# File 'lib/delivery/tests/fake_responder.rb', line 58

def respond_401
  path = Pathname.new(File.dirname(__FILE__) + '/401.json')
  Kentico::Kontent::Delivery::Responses::ResponseBase.new 401, '', path.read if path.exist?
end

.respond_filtering(query) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/delivery/tests/fake_responder.rb', line 45

def respond_filtering(query)
  path =
    case CGI.unescape query
    when 'skip=0&limit=5'
      Pathname.new(File.dirname(__FILE__) + '/filtering/pagination_about_us.json')
    when 'elements.price[gt]=20'
      Pathname.new(File.dirname(__FILE__) + '/filtering/items_gt.json')
    when 'elements.price[gt]=20&system.type=grinder'
      Pathname.new(File.dirname(__FILE__) + '/filtering/multiple.json')
    end
  path.read unless path.nil? && !path.exist?
end

.respond_generic(url) ⇒ Object



40
41
42
43
# File 'lib/delivery/tests/fake_responder.rb', line 40

def respond_generic(url)
  path = Pathname.new(File.dirname(__FILE__) + "/generic#{url}.json")
  path.read if path.exist?
end