module ElectricProfile class Question attr_accessor :id, :customerId, :name, :prompt, :type, :answerOptions, :answerOptionOrder, :allowMultiple, :categories, :error, :exclusiveAnswerOptions, :regExValidation, :regExValidationMessage, :baseLogic, :derived, :derivationLogic TYPES = ['multi_choice', 'number', 'short_text', 'long_text'] def initialize(atts) atts = atts.inject({}){ |memo, (k, v) | memo[k.to_sym] = v; memo } @id = atts[:id] @customerId = atts[:customerId] @name = atts[:name] @prompt = atts[:prompt] @type = atts[:type] @answerOptions = atts[:answerOptions] || {} @allowMultiple = atts[:allowMultiple] || false @categories = atts[:categories] || [] @exclusiveAnswerOptions = atts[:exclusiveAnswerOptions] @answerOptionOrder = atts[:answerOptionOrder] @regExValidation = atts[:regExValidation] @regExValidationMessage = atts[:regExValidationMessage] || {} @baseLogic = atts[:baseLogic] @derived = atts[:derived] || false @derivationLogic = atts[:derivationLogic] || [] end def save if @id save_existing else save_new end end def save_new initialize_client attributes = { name: @name, prompt: @prompt, type: @type, answerOptions: @answerOptions, allowMultiple: @allowMultiple, categories: @categories, exclusiveAnswerOptions: @exclusiveAnswerOptions, answerOptionOrder: @answerOptionOrder, regExValidation: @regExValidation, regExValidationMessage: @regExValidationMessage, baseLogic: @baseLogic, derived: @derived, derivationLogic: @derivationLogic } if @client.create_question(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, prompt: @prompt, type: @type, answerOptions: @answerOptions, allowMultiple: @allowMultiple, categories: @categories, exclusiveAnswerOptions: @exclusiveAnswerOptions, answerOptionOrder: @answerOptionOrder, regExValidation: @regExValidation, regExValidationMessage: @regExValidationMessage, baseLogic: @baseLogic, derived: @derived, derivationLogic: @derivationLogic } if @client.update_question(attributes) true else @error = @client.error false end end def self.find(id) client = Client.new if client.fetch_question(id) new client.data else raise StandardError, client.error end end def self.all client = Client.new if client.fetch_questions client.data["Items"].map { |atts| new atts } else raise StandardError, client.error end end def initialize_client @client ||= Client.new end end end