Class: SgPostcode::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/sg_postcode/services/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, postcode, cache: true) ⇒ Proxy

Returns a new instance of Proxy.



5
6
7
8
9
10
11
12
13
14
# File 'lib/sg_postcode/services/proxy.rb', line 5

def initialize(service, postcode, cache: true)
  @postcode = postcode

  case service
  when :Google
    @service = Google.new(postcode)
  end

  @cache_adapter = CacheAdapter.new(postcode) if cache
end

Instance Attribute Details

#cache_adapterObject (readonly)

Returns the value of attribute cache_adapter.



3
4
5
# File 'lib/sg_postcode/services/proxy.rb', line 3

def cache_adapter
  @cache_adapter
end

#serviceObject (readonly)

Returns the value of attribute service.



3
4
5
# File 'lib/sg_postcode/services/proxy.rb', line 3

def service
  @service
end

Instance Method Details

#requestObject



16
17
18
19
20
21
22
23
24
# File 'lib/sg_postcode/services/proxy.rb', line 16

def request
  return nil unless service

  if cache_adapter
    cache_adapter.fetch || cache_adapter.store(@postcode, service.request)
  else
    service.request
  end
end