Class: Optimizely::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/engine.rb

Constant Summary collapse

BASE_URL =
"https://www.optimizelyapis.com/experiment/v1/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Engine

Initialize Optimizely using an API token.

Options:

:api_token

Use an API token you received before.

:timeout

Set Net:HTTP timeout in seconds (default is 300).



14
15
16
17
# File 'lib/optimizely/engine.rb', line 14

def initialize(options={})
  @options = options
  check_init_auth_requirements
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/optimizely/engine.rb', line 6

def url
  @url
end

Instance Method Details

#audience(id) ⇒ Object

Returns the details for a specific audience.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
audience = optimizely.audience(12345) # Look up the audience.


141
142
143
144
145
146
147
# File 'lib/optimizely/engine.rb', line 141

def audience(id)
  @url = "audiences/#{id}"
  raise OptimizelyError::NoAudienceID, "An Audience ID is required to retrieve the audience." if id.nil?

  response = self.get(@url)
  Audience.new(response)
end

#audiences(project_id) ⇒ Object

Returns the list of audiences for a specified project.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
audiences = optimizely.audiences(12345) # Look up all audiences for a project.


128
129
130
131
132
133
# File 'lib/optimizely/engine.rb', line 128

def audiences(project_id)
  raise OptimizelyError::NoProjectID, "A Project ID is required to retrieve audiences." if project_id.nil?

  response = self.get("projects/#{project_id}/audiences")
  response.collect { |audience_json| Audience.new(audience_json) }
end

#check_response(code, body) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/optimizely/engine.rb', line 208

def check_response(code, body)
  case code
  when '400'
    raise OptimizelyError::BadRequest, body + "Your request was not sent in valid JSON. (status code: #{code})."
  when '401'
    raise OptimizelyError::Unauthorized, "Your API token was missing or included in the body rather than the header (status code: #{code})."
  when '403'
    raise OptimizelyError::Forbidden, body + "You provided an API token but it was invalid or revoked, or if you don't have read/ write access to the entity you're trying to view/edit (status code: #{code})."
  when '404'
    raise OptimizelyError::NotFound, body + "The id used in the request was inaccurate or you didn't have permission to view/edit it (status code: #{code})."
  else
    raise OptimizelyError::UnknownError, body + " (status code: #{code})."
  end
end

#deleteObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/optimizely/engine.rb', line 193

def delete
  raise OptimizelyError::NoId, "An ID is required to delete data." if @url.nil?

  uri      = URI.parse("#{BASE_URL}#{@url}")
  https    = Net::HTTP.new(uri.host, uri.port)
  https.read_timeout = @options[:timeout] if @options[:timeout]
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.use_ssl = true
  request  = Net::HTTP::Delete.new(uri.request_uri, @headers)
  response = https.request(request)

  # Response code error checking
  check_response(response.code, response.body) if response.code != '204'
end

#experiment(id) ⇒ Object

Returns the details for a specific experiment.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
experiment = optimizely.experiment(12345) # Look up the experiment.


73
74
75
76
77
78
79
# File 'lib/optimizely/engine.rb', line 73

def experiment(id)
  @url = "experiments/#{id}"
  raise OptimizelyError::NoExperimentID, "An Experiment ID is required to retrieve the experiment." if id.nil?

  response = self.get(@url)
  Experiment.new(response)
end

#experiments(project_id) ⇒ Object

Returns the list of experiments for a specified project.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
experiments = optimizely.experiments(12345) # Look up all experiments for a project.


60
61
62
63
64
65
# File 'lib/optimizely/engine.rb', line 60

def experiments(project_id)
  raise OptimizelyError::NoProjectID, "A Project ID is required to retrieve experiments." if project_id.nil?

  response = self.get("projects/#{project_id}/experiments")
  response.collect { |response_json| Experiment.new(response_json) }
end

#get(url) ⇒ Object

Return the parsed JSON data for a request that is done to the Optimizely REST API.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/optimizely/engine.rb', line 150

def get(url)
  uri      = URI.parse("#{BASE_URL}#{url}/")
  https    = Net::HTTP.new(uri.host, uri.port)
  https.read_timeout = @options[:timeout] if @options[:timeout]
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.use_ssl = true
  request  = Net::HTTP::Get.new(uri.request_uri, @headers)
  response = https.request(request)

  # Response code error checking
  if response.code != '200'
    check_response(response.code, response.body)
  else
    parse_json(response.body)
  end
end

#postObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/optimizely/engine.rb', line 167

def post
  uri      = URI.parse("#{BASE_URL}#{url}/")
  https    = Net::HTTP.new(uri.host, uri.port)
  https.read_timeout = @options[:timeout] if @options[:timeout]
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.use_ssl = true
  request  = Net::HTTP::Post.new(uri.request_uri, @headers)
  response = https.request(request)

  # Response code error checking
  check_response(response.code, response.body) if response.code != '201'
end

#project(id) ⇒ Object

Returns the details for a specific project.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
project = optimizely.project(12345) # Look up the project.


46
47
48
49
50
51
52
# File 'lib/optimizely/engine.rb', line 46

def project(id)
  @url = "projects/#{id}"
  raise OptimizelyError::NoProjectID, "A Project ID is required to retrieve the project." if id.nil?

  response = self.get(@url)
  Project.new(response)
end

#projectsObject

Returns the list of projects available to the authenticated user.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
projects = optimizely.projects # Look up all projects.


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

def projects
  if @projects.nil?
    response = self.get("projects")
    @projects = response.collect { |project_json| Project.new(project_json) }
  end
  @projects
end

#putObject



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/optimizely/engine.rb', line 180

def put
  uri      = URI.parse("#{BASE_URL}#{url}/")
  https    = Net::HTTP.new(uri.host, uri.port)
  https.read_timeout = @options[:timeout] if @options[:timeout]
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.use_ssl = true
  request  = Net::HTTP::Put.new(uri.request_uri, @headers)
  response = https.request(request)

  # Response code error checking
  check_response(response.code, response.body) if response.code != '202'
end

#stats(experiment_id) ⇒ Object

Returns the stats for a specific experiment.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
stats = optimizely.stats(12345) # Look up the stats with the specific experiment.


87
88
89
90
91
92
93
# File 'lib/optimizely/engine.rb', line 87

def stats(experiment_id)
  @url = "experiments/#{experiment_id}/stats"
  raise OptimizelyError::NoExperimentID, "An Experiment ID is required to retrieve the stats." if experiment_id.nil?

  response = self.get(@url)
  response.collect { |response_json| Stat.new(response_json) }
end

#token=(token) ⇒ Object

Initalize the Token so it can be used in every request we’ll do later on. Besides that also set the authentication header.



21
22
23
24
# File 'lib/optimizely/engine.rb', line 21

def token=(token)
  @token = token
  set_headers
end

#variation(id) ⇒ Object

Returns the details for a specific variation.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
variation = optimizely.variation(12345) # Look up the variation.


114
115
116
117
118
119
120
# File 'lib/optimizely/engine.rb', line 114

def variation(id)
  @url = "variations/#{id}"
  raise OptimizelyError::NoVariationID, "A Variation ID is required to retrieve the variation." if id.nil?

  response = self.get(@url)
  Variation.new(response)
end

#variations(experiment_id) ⇒ Object

Returns the list of variations for a specified experiment.

Usage

optimizely = Optimizely.new({ api_token: 'oauth2_token' })
variations = optimizely.variations(12345) # Look up all variations for an experiment.


101
102
103
104
105
106
# File 'lib/optimizely/engine.rb', line 101

def variations(experiment_id)
  raise OptimizelyError::NoExperimentID, "An Experiment ID is required to retrieve variations." if experiment_id.nil?

  response = self.get("experiments/#{experiment_id}/variations")
  response.collect { |variation_json| Variation.new(variation_json) }
end