Rich-text Reply

Ruby SDK - Undefined method `activate` for Optimizely Project

lloudermilk 02-23-17
Accepted Solution

Ruby SDK - Undefined method `activate` for Optimizely Project

[ Edited ]

Hi all,

I'm using the Ruby SDK and can't get optimizely_client.activate to work. I can successfully create a Project object in a class I've made (I'm also new to Ruby, so apologies if anything below is incorrect):

module WWOptimizely

  class << self

    PROJECT_ID = 8252151177
    DATAFILE_REMOTE_PATH = "https://cdn.optimizely.com/json/#{PROJECT_ID}.json"
    DATAFILE_LOCAL_PATH = './config/optimizely_datafile.json'

    def client
      return @client if @client

      datafile = File.read(DATAFILE_LOCAL_PATH)
      datafile ||= update_datafile

      @client = Optimizely::Project.new(datafile)
    end

    def update_datafile
      File.open(DATAFILE_LOCAL_PATH, 'w') do |file|
        file.write(get_datafile_json)
      end
    end

    def get_datafile_json
      HTTParty.get(DATAFILE_REMOTE_PATH).body
    end
  end
end

This gives me the following object:

 

#<Optimizely::Project:0x007f843c3a91f8 @json="{\n  \"version\": \"2\",\n  \"projectId\": \"8252151177\",\n  \"experiments\": [\n    {\n      \"status\": \"Not started\",\n      \"audienceIds\": [\n        \"8249051205\"\n      ],\n      \"variations\": [\n        {\n          \"id\": \"8252133685\",\n          \"key\": \"key1\"\n        },\n        {\n          \"id\": \"8248971715\",\n          \"key\": \"key2\"\n        }\n      ],\n      \"id\": \"8250701338\",\n      \"key\": \"initial-experiment\",\n      \"layerId\": \"8250614457\",\n      \"trafficAllocation\": [\n        {\n          \"entityId\": \"8252133685\",\n          \"endOfRange\": 5000\n        },\n        {\n          \"entityId\": \"8248971715\",\n          \"endOfRange\": 10000\n        }\n      ],\n      \"forcedVariations\": {}\n    }\n  ],\n  \"audiences\": [\n    {\n      \"conditions\": \"[\\\"and\\\", [\\\"or\\\", [\\\"or\\\", {\\\"name\\\": \\\"user_attr\\\", \\\"type\\\": \\\"custom_attribute\\\"}]]]\",\n      \"id\": \"8249051205\",\n      \"name\": \"Everyone\"\n    }\n  ],\n  \"groups\": [],\n  \"attributes\": [\n    {\n      \"id\": \"8253167187\",\n      \"key\": \"user_attr\"\n    }\n  ],\n  \"revision\": \"5\",\n  \"events\": [],\n  \"accountId\": \"8252151177\"\n}", @project_name=nil, @project_status=nil, @id="id", @account_id=nil>

 

Afterwards from another file I attempt to activate an experiement using the following code:

 

WWOptimizely.client.activate('SHOW_HIDE_VIDEO_POSTS', @current_user.id)

On executing the line above i recieve the following error: 

 

 

NoMethodError: undefined method `activate' for #<Optimizely::Project:0x007f843c3a91f8>

 

 

This page on github and this rubydoc page, as well as the documentation all lead me to believe my approach here is not wrong - activate should be a method on the Project object. Looking at the lib/optimizely/project.rb file I see that the only method defined is the initialize method.

As I'm 100% new to Optimizely, I was hoping someone here could help me figure out what I've done wrong.

Any help would be greatly appreciated. 

Thanks!

Lauryn

 

--
Lauryn Loudermilk
Software Engineer - Weight Watchers
lloudermilk 02-24-17
 

Re: Ruby SDK - Undefined method `activate` for Optimizely Project

Found the issue – in my Gemfile I was using ‘gem optimizely’, which was installing this gem: https://github.com/MartijnSch/optimizely-gem
Whoops!
Lauryn
--
Lauryn Loudermilk
Software Engineer - Weight Watchers