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:, success:) ⇒ DatafileFetcher

Returns a new instance of DatafileFetcher.



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

def initialize(content:, success:)
  @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



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

def call_optimizely_cdn
  Net::HTTP.get_response(
    URI(OptimizelyServerSide.configuration.config_endpoint)
  )
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.



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

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