Sha256: e543df344bf23e4e4f359e0600f8787e5790b357c291b27c2dabe5ac066e9634

Contents?: true

Size: 1.81 KB

Versions: 39

Compression:

Stored size: 1.81 KB

Contents

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

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
mangopay-3.28.0 lib/generators/mangopay/install_generator.rb
mangopay-3.27.0 lib/generators/mangopay/install_generator.rb
mangopay-3.26.1 lib/generators/mangopay/install_generator.rb
mangopay-3.26.0 lib/generators/mangopay/install_generator.rb
mangopay-3.25.1 lib/generators/mangopay/install_generator.rb
mangopay-3.25.0 lib/generators/mangopay/install_generator.rb
mangopay-3.24.1 lib/generators/mangopay/install_generator.rb
mangopay-3.24.0 lib/generators/mangopay/install_generator.rb
mangopay-3.23.0 lib/generators/mangopay/install_generator.rb
mangopay-3.22.0 lib/generators/mangopay/install_generator.rb
mangopay-3.21.0 lib/generators/mangopay/install_generator.rb
mangopay-3.20.0 lib/generators/mangopay/install_generator.rb
mangopay-3.19.0 lib/generators/mangopay/install_generator.rb
mangopay-3.18.0 lib/generators/mangopay/install_generator.rb
mangopay-3.17.0 lib/generators/mangopay/install_generator.rb
mangopay-3.16.0 lib/generators/mangopay/install_generator.rb
mangopay-3.15.0 lib/generators/mangopay/install_generator.rb
mangopay-3.14.0 lib/generators/mangopay/install_generator.rb
mangopay-3.13.2 lib/generators/mangopay/install_generator.rb
mangopay-3.12.0 lib/generators/mangopay/install_generator.rb