-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.thor
139 lines (125 loc) · 3.98 KB
/
app.thor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require "./environment.rb"
class Codes < Thor
desc "generate", "Generates random codes."
method_option :code_lengh,
desc: "The length of the generated code",
type: :numeric,
aliases: "-L",
default: "8"
method_option :number_of_codes,
desc: "The number of codes to generate",
type: :numeric,
aliases: "-N",
default: "10"
method_option :to_file,
desc: "Prints the generated codes to a file",
type: :boolean,
aliases: "-f",
default: true
method_option :debug,
desc: "If set to true, the codes will be printed to the console",
type: :boolean,
aliases: "-D",
default: true
def generate
number_of_codes = options.number_of_codes.to_i
length = options.code_lengh.to_i
bar = ProgressBar.new(number_of_codes)
puts "Generating #{number_of_codes} code(s) with a length of #{length} character(s)"
codes = []
(1..number_of_codes).to_a.each do |n|
new_code = Devise.friendly_token.first(length)
while codes.include?(new_code) == true
new_code = Devise.friendly_token.first(length)
end
p new_code if options.debug
codes << new_code
bar.increment!
end
if options.to_file
CSV.open("codes_#{Time.now.to_s}.csv", "w") do |csv|
codes.each do |code|
csv << [code]
end
end
end
end
desc "test", "uniquenes of codes"
method_option :code_lengh,
desc: "The length of the generated codes",
type: :numeric,
aliases: "-L",
default: "8"
method_option :number_of_codes,
desc: "The number of codes to generate",
type: :numeric,
aliases: "-N",
default: "10"
def test
number_of_codes = options.number_of_codes.to_i
length = options.code_lengh.to_i
a = invoke :generate, ["-N=#{number_of_codes}", "-L=#{length}"]
puts a.count
puts a.uniq.count
end
end
module Recurly
class Coupons < Thor
desc "generate API_KEY", "generate coupons in recurly"
method_option :code_lengh,
desc: "The length of the generated codes",
type: :numeric,
aliases: "-L",
default: "8"
method_option :number_of_codes,
desc: "The number of codes to generate",
type: :numeric,
aliases: "-N",
default: "10"
method_option :debug,
desc: "If set to true, the generated coupon will not be saved",
type: :boolean,
aliases: "-D",
default: true
def generate(api_key)
# Setting the api key for recurly
::Recurly.api_key = api_key
# Getting the values from the options attributes
number_of_codes = options.number_of_codes.to_i
length = options.code_lengh.to_i
bar = ProgressBar.new(number_of_codes)
# Setting default coupon attributes
new_coupon_attributes = {
name: "FIL 2013",
discount_in_cents: 50_00,
# redeem_by_date: Date.new(2013, 7, 1),
max_redemptions: 1,
single_use: true
}
# Generating random coupon codes
_codes = invoke "codes:generate", ["-N=#{number_of_codes}", "-L=#{length}"]
_codes.each do |code|
# upload coupon to recurly
new_coupon_attributes[:coupon_code] = code
recurly_coupon = ::Recurly::Coupon.new(new_coupon_attributes)
if options.debug == false
# p "uploading coupon #{recurly_coupon.coupon_code} to recurly"
recurly_coupon.save
else
# p "coupon code #{recurly_coupon.coupon_code} not saved in recurly."
end
bar.increment!
end
end
desc "list API_KEY", "List the coupons you have in recurly"
def list(api_key)
# Setting the api key for recurly
::Recurly.api_key = api_key
::Recurly::Coupon.find_each do |coupon|
if coupon.state == "redeemable"
puts "name: #{coupon.name} code: #{coupon.coupon_code} discount: #{coupon.discount_in_cents.to_s} single_use: #{coupon.single_use} Redeem-By: #{coupon.redeem_by_date.strftime('%a %d %b %Y')}"
end
end
end
end
end