module ElectricProfile class Profiler attr_accessor :id, :customerId, :name, :listQuestionsId, :introMessage, :exitMessage, :hideAnsweredQuestions, :callbackType, :error def initialize(atts) atts = atts.inject({}){ |memo, (k, v) | memo[k.to_sym] = v; memo } @id = atts[:id] @customerId = atts[:customerId] @name = atts[:name] @listQuestionsId = atts[:listQuestionsId] || [] @introMessage = atts[:introMessage] @exitMessage = atts[:exitMessage] @hideAnsweredQuestions = atts[:hideAnsweredQuestions] || false @callbackType = atts[:callbackType] end def save if @id save_existing else save_new end end def save_new initialize_client attributes = { name: @name, listQuestionsId: @listQuestionsId, introMessage: @introMessage, exitMessage: @exitMessage, hideAnsweredQuestions: @hideAnsweredQuestions, callbackType: @callbackType } if @client.create_profiler(attributes) @id = @client.data["id"] @customerId = @client.data["customerId"] true else @error = @client.error false end end def save_existing initialize_client attributes = { id: @id, customerId: @customerId, name: @name, listQuestionsId: @listQuestionsId, introMessage: @introMessage, exitMessage: @exitMessage, hideAnsweredQuestions: @hideAnsweredQuestions, callbackType: @callbackType } if @client.update_profiler(attributes) true else @error = @client.error false end end def self.find(id) client = Client.new if client.fetch_profiler(id) new client.data["Items"].first else raise StandardError, client.error end end def self.all client = Client.new if client.fetch_profilers client.data["Items"].map { |atts| new atts } else raise StandardError, client.error end end def initialize_client @client ||= Client.new end end end