Class: Optimizely::Engine
- Inherits:
-
Object
- Object
- Optimizely::Engine
- Defined in:
- lib/optimizely/engine.rb
Constant Summary collapse
- BASE_URL =
"https://www.optimizelyapis.com/experiment/v1/"
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#audience(id) ⇒ Object
Returns the details for a specific audience.
-
#audiences(project_id) ⇒ Object
Returns the list of audiences for a specified project.
- #check_response(code, body) ⇒ Object
- #delete ⇒ Object
-
#experiment(id) ⇒ Object
Returns the details for a specific experiment.
-
#experiments(project_id) ⇒ Object
Returns the list of experiments for a specified project.
-
#get(url) ⇒ Object
Return the parsed JSON data for a request that is done to the Optimizely REST API.
-
#initialize(options = {}) ⇒ Engine
constructor
Initialize Optimizely using an API token.
- #post ⇒ Object
-
#project(id) ⇒ Object
Returns the details for a specific project.
-
#projects ⇒ Object
Returns the list of projects available to the authenticated user.
- #put ⇒ Object
-
#token=(token) ⇒ Object
Initalize the Token so it can be used in every request we’ll do later on.
-
#variation(id) ⇒ Object
Returns the details for a specific variation.
-
#variations(experiment_id) ⇒ Object
Returns the list of variations for a specified experiment.
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 = check_init_auth_requirements end |
Instance Attribute Details
#url ⇒ Object
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.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/optimizely/engine.rb', line 145 def audience(id) @url = "audiences/#{id}" raise OptimizelyError::NoAudienceID, "An Audience ID is required to retrieve the audience." if id.nil? if @audience.nil? response = self.get(@url) @audience = Audience.new(response) end @audience 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.
129 130 131 132 133 134 135 136 137 |
# File 'lib/optimizely/engine.rb', line 129 def audiences(project_id) raise OptimizelyError::NoProjectID, "A Project ID is required to retrieve audiences." if project_id.nil? if @audiences.nil? response = self.get("projects/#{project_id}/audiences") @audiences = response.collect { |audience_json| Audience.new(audience_json) } end @audiences end |
#check_response(code, body) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/optimizely/engine.rb', line 215 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 |
#delete ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/optimizely/engine.rb', line 200 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.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/optimizely/engine.rb', line 79 def experiment(id) @url = "experiments/#{id}" raise OptimizelyError::NoExperimentID, "An Experiment ID is required to retrieve the experiment." if id.nil? if @experiment.nil? response = self.get(@url) @experiment = Experiment.new(response) end @experiment 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.
63 64 65 66 67 68 69 70 71 |
# File 'lib/optimizely/engine.rb', line 63 def experiments(project_id) raise OptimizelyError::NoProjectID, "A Project ID is required to retrieve experiments." if project_id.nil? if @experiments.nil? response = self.get("projects/#{project_id}/experiments") @experiments = response.collect { |response_json| Experiment.new(response_json) } end @experiments end |
#get(url) ⇒ Object
Return the parsed JSON data for a request that is done to the Optimizely REST API.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/optimizely/engine.rb', line 157 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 |
#post ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/optimizely/engine.rb', line 174 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 53 54 55 |
# 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? if @project.nil? response = self.get(@url) @project = Project.new(response) end @project end |
#projects ⇒ Object
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 |
#put ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/optimizely/engine.rb', line 187 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 |
#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.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/optimizely/engine.rb', line 112 def variation(id) @url = "variations/#{id}" raise OptimizelyError::NoVariationID, "A Variation ID is required to retrieve the variation." if id.nil? if @variation.nil? response = self.get(@url) @variation = Variation.new(response) end @variation 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.
96 97 98 99 100 101 102 103 104 |
# File 'lib/optimizely/engine.rb', line 96 def variations(experiment_id) raise OptimizelyError::NoExperimentID, "An Experiment ID is required to retrieve variations." if experiment_id.nil? if @variations.nil? response = self.get("projects/#{experiment_id}/variations") @variations = response.collect { |variation_json| Variation.new(variation_json) } end @variations end |