-
Notifications
You must be signed in to change notification settings - Fork 38
/
install_generator.rb
60 lines (53 loc) · 1.81 KB
/
install_generator.rb
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
require 'rails/generators/base'
require 'mangopay'
module Mangopay
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../../templates', __FILE__)
argument :client_id, type: :string,
desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)'
argument :client_name, type: :string, desc: "Full name of you're organization"
argument :client_email, type: :string, desc: "An email for future contacts"
class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment'
desc 'Installs all the basic configuration of the mangopay gem'
def setup
begin
client = client_id_valid?
remove_file 'config/initializers/mangopay.rb'
@client_id = client_id
@client_apiKey = client['APIKey']
template 'mangopay.rb.erb', 'config/initializers/mangopay.rb'
rescue => e
puts e.message
end
end
protected
def client_id_valid?
check_client_id_validity
check_client_id_availablility
end
def check_client_id_validity
if (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil?
raise "The client_id must match the regexp ^[a-z0-9_-]{4,20}$"
end
end
def check_client_id_availablility
client = create_client
if client['Type'] == 'ClientID_already_exist'
raise client['Message']
end
client
end
def create_client
MangoPay.configure do |c|
c.preproduction = options[:preproduction]
end
MangoPay::Client.create({
ClientID: client_id,
Name: client_name,
Email: client_email
})
end
end
end
end