Class: ElectricProfile::Reward

Inherits:
Object
  • Object
show all
Defined in:
lib/electric_profile_ruby/reward.rb

Constant Summary collapse

TYPES =
['rewardlink', 'amazon', 'paypal', 'bitcoin', 'habitat']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atts) ⇒ Reward

Returns a new instance of Reward.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/electric_profile_ruby/reward.rb', line 7

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

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def amount
  @amount
end

#autoClaimObject

Returns the value of attribute autoClaim.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def autoClaim
  @autoClaim
end

#autoDeliverObject

Returns the value of attribute autoDeliver.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def autoDeliver
  @autoDeliver
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def currency
  @currency
end

#customerIdObject

Returns the value of attribute customerId.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def customerId
  @customerId
end

#deliveredObject

Returns the value of attribute delivered.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def delivered
  @delivered
end

#deliveryErrorsObject

Returns the value of attribute deliveryErrors.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def deliveryErrors
  @deliveryErrors
end

#errorObject

Returns the value of attribute error.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def error
  @error
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def id
  @id
end

#recipientEmailObject

Returns the value of attribute recipientEmail.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def recipientEmail
  @recipientEmail
end

#recipientIdObject

Returns the value of attribute recipientId.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def recipientId
  @recipientId
end

#rewardProfileIdObject

Returns the value of attribute rewardProfileId.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def rewardProfileId
  @rewardProfileId
end

#transactionIdObject

Returns the value of attribute transactionId.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def transactionId
  @transactionId
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/electric_profile_ruby/reward.rb', line 4

def type
  @type
end

Class Method Details

.allObject



99
100
101
102
103
104
105
106
# File 'lib/electric_profile_ruby/reward.rb', line 99

def self.all
  client = Client.new
  if client.fetch_rewards
    client.data["Items"].map { |atts| new atts }
  else
    raise StandardError, client.error
  end
end

.find(id) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/electric_profile_ruby/reward.rb', line 90

def self.find(id)
  client = Client.new
  if client.fetch_reward(id)
    new client.data["data"]
  else
    raise StandardError, client.error
  end
end

Instance Method Details

#initialize_clientObject



108
109
110
# File 'lib/electric_profile_ruby/reward.rb', line 108

def initialize_client
  @client ||= Client.new
end

#queue_deliveryObject



80
81
82
83
84
85
86
87
88
# File 'lib/electric_profile_ruby/reward.rb', line 80

def queue_delivery
  initialize_client
  if @client.queue_reward_delivery(@id)
    true
  else
    @error = @client.error
    false
  end
end

#saveObject



24
25
26
27
28
29
30
# File 'lib/electric_profile_ruby/reward.rb', line 24

def save
  if @id
    save_existing
  else
    save_new
  end
end

#save_existingObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/electric_profile_ruby/reward.rb', line 55

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
  }
  attributes[:transactionId] = @transactionId if @transactionId
  if @client.update_reward(attributes)
    true
  else
    @error = @client.error
    false
  end
end

#save_newObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/electric_profile_ruby/reward.rb', line 32

def save_new
  initialize_client
  attributes = {
    rewardProfileId: @rewardProfileId,
    amount: @amount,
    currency: @currency,
    type: @type,
    recipientEmail: @recipientEmail,
    recipientId: @recipientId,
    autoClaim: @autoClaim,
    autoDeliver: @autoDeliver
  }
  attributes = attributes.delete_if { |k, v| v.nil? }
  if @client.create_reward(attributes)
    @id = @client.data["data"]["id"]
    @customerId = @client.data["data"]["customerId"]
    true
  else
    @error = @client.error
    false
  end
end