module ElectricProfile class Reward attr_accessor :id, :customerId, :rewardProfileId, :amount, :currency, :type, :recipientEmail, :recipientId, :autoClaim, :autoDeliver, :delivered, :deliveryErrors, :transactionId, :error TYPES = ['rewardlink', 'amazon', 'paypal', 'bitcoin', 'habitat'] def initialize(atts) atts = atts.inject({}){ |memo, (k, v) | memo[k.to_sym] = v; memo } @id = atts[:id] @customerId = atts[:customerId] @rewardProfileId = atts[:rewardProfileId] @amount = atts[:amount] @currency = atts[:currency] @type = atts[:type] @recipientEmail = atts[:recipientEmail] @recipientId = atts[:recipientId] @autoClaim = atts[:autoClaim] @autoDeliver = atts[:autoDeliver] @delivered = atts[:delivered] @deliveryErrors = atts[:deliveryErrors] @transactionId = atts[:transactionId] end def save if @id save_existing else save_new end end def save_new initialize_client attributes = { rewardProfileId: @rewardProfileId, amount: @amount, currency: @currency, type: @type, recipientEmail: @recipientEmail, recipientId: @recipientId, autoClaim: @autoClaim, autoDeliver: @autoDeliver } if @client.create_reward(attributes) @id = @client.data["data"]["id"] @customerId = @client.data["data"]["customerId"] true else @error = @client.error false end end def save_existing initialize_client attributes = { id: @id, customerId: @customerId, rewardProfileId: @rewardProfileId, amount: @amount, currency: @currency, type: @type, recipientEmail: @recipientEmail, recipientId: @recipientId, autoClaim: @autoClaim, autoDeliver: @autoDeliver, delivered: @delivered, deliveryErrors: @deliveryErrors, transactionId: @transactionId } if @client.update_reward(attributes) true else @error = @client.error false end end def self.find(id) client = Client.new if client.fetch_reward(id) new client.data else raise StandardError, client.error end end def self.all client = Client.new if client.fetch_rewards client.data["Items"].map { |atts| new atts } else raise StandardError, client.error end end def initialize_client @client ||= Client.new end end end