Class: OptimizelyServerSide::DatafileFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely_server_side/datafile_fetcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content: nil, success: false) ⇒ DatafileFetcher

Returns a new instance of DatafileFetcher.



11
12
13
14
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 11

def initialize(content: nil, success: false)
  @content = content
  @success = success
end

Instance Attribute Details

#contentObject (readonly)

Responsible for fetching the optimizely sdk config from the API source. The API can be optimizely cdn itself or any other source.



9
10
11
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 9

def content
  @content
end

#successObject (readonly)

Responsible for fetching the optimizely sdk config from the API source. The API can be optimizely cdn itself or any other source.



9
10
11
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 9

def success
  @success
end

Class Method Details

.call_optimizely_cdnObject

Gets data from Optimizely cdn



32
33
34
35
36
37
38
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 32

def call_optimizely_cdn
  ActiveSupport::Notifications.instrument "oss.call_optimizely_cdn",this: :data  do
    Net::HTTP.get_response(
      URI(OptimizelyServerSide.configuration.config_endpoint)
    )
  end
end

.fallbackObject



40
41
42
43
44
45
46
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 40

def fallback
  new(
    content: '{"experiments": [],"version": "1","audiences": [],"dimensions": [],"groups": [],"projectId": "5960232316","accountId": "5955320306","events": [],"revision": "30"}',
    success: false
  )

end

.fetchObject Also known as: datafile

Fetch the Config from the specified source. Incase of any error or exception we goto the fallback data



20
21
22
23
24
25
26
27
28
# File 'lib/optimizely_server_side/datafile_fetcher.rb', line 20

def fetch
  begin
    response = call_optimizely_cdn
    return fallback unless response.is_a?(Net::HTTPSuccess)
    new(content: response.body, success: true)
  rescue Exception => e
    fallback
  end
end