Module: Modulorails

Defined in:
lib/modulorails.rb,
lib/modulorails/data.rb,
lib/modulorails/railtie.rb,
lib/modulorails/version.rb,
lib/modulorails/configuration.rb,
lib/modulorails/validators/database_configuration.rb

Overview

Author: Matthieu 'ciappa_m' Ciappara The entry point of the gem. It exposes the configurator, the gathered data and the method to send those data to the intranet.

Defined Under Namespace

Modules: ApplicationHelper, Validators Classes: BaseError, BaseService, Configuration, Data, DockerGenerator, Error, ErrorData, GitlabciGenerator, HealthCheckGenerator, InvalidFormatError, InvalidValueError, LogsForMethodService, Railtie, RubocopGenerator, SelfUpdateGenerator, ServiceGenerator, SuccessData

Constant Summary collapse

VERSION =
'1.0.0'
COMPARABLE_RUBY_VERSION =

Useful to compare the current Ruby version

Gem::Version.new(RUBY_VERSION)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationModulorails::Configuration

A configuration getter.

Returns:

Author:

  • Matthieu 'ciappa_m' Ciappara



53
54
55
# File 'lib/modulorails.rb', line 53

def configuration
  @configuration ||= Modulorails::Configuration.new
end

Class Method Details

.check_database_configObject

Check the database configuration respects Modulotech's norms

Author:

  • Matthieu 'ciappa_m' Ciappara



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/modulorails.rb', line 123

def check_database_config
  invalid_rules = Modulorails::Validators::DatabaseConfiguration.call
  return true if invalid_rules.empty?

  puts('[Modulorails] The database configuration (config/database.yml) has warnings:')
  invalid_rules.each do |rule|
    t_rule = I18n.t(rule, scope: :modulorails, locale: :en)
    puts("[Modulorails]    Invalid database configuration: #{t_rule}")
  end

  false
end

.configure {|configuration| ... } ⇒ Modulorails::Configuration

When a block is given, it allows to define or update the current configuration. Without a block, this methods is just a configuration getter.

Yields:

  • (configuration)

    Block with the current configuration; optional

Yield Parameters:

Returns:

Author:

  • Matthieu 'ciappa_m' Ciappara



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/modulorails.rb', line 36

def configure
  # Get the current configuration if no block is given
  return configuration unless block_given?

  # Pass the configuration to the block and let the block do what it wants (probably update the
  # configuration)
  yield configuration

  # Return the (probably updated) current configuration
  configuration
end

.dataModulorails::Data

A data getter.

Returns:

Author:

  • Matthieu 'ciappa_m' Ciappara



62
63
64
# File 'lib/modulorails.rb', line 62

def data
  @data ||= Modulorails::Data.new
end

.generate_ci_templateObject

Generate a CI/CD template unless it was already done. The check is done using a 'keepfile'.

Author:

  • Matthieu 'ciappa_m' Ciappara



114
115
116
117
118
# File 'lib/modulorails.rb', line 114

def generate_ci_template
  return if File.exists?(Rails.root.join('.modulorails-gitlab-ci'))

  Modulorails::GitlabciGenerator.new([], {}, {}).invoke_all
end

.generate_healthcheck_templateObject

Generate a health_check configuration unless it was already done. The check is done using a 'keepfile'.

Author:

  • Matthieu 'ciappa_m' Ciappara



150
151
152
153
154
# File 'lib/modulorails.rb', line 150

def generate_healthcheck_template
  return if File.exists?(Rails.root.join('.modulorails-health_check'))

  Modulorails::HealthCheckGenerator.new([], {}, {}).invoke_all
end

.generate_rubocop_templateObject

Generate a rubocop configuration.

Author:

  • Matthieu 'ciappa_m' Ciappara



159
160
161
# File 'lib/modulorails.rb', line 159

def generate_rubocop_template
  Modulorails::RubocopGenerator.new([], {}, {}).invoke_all
end

.self_updateObject

Check the last version of Modulorails available on rubygems and update if there was a publication

Author:

  • Matthieu 'ciappa_m' Ciappara



140
141
142
143
144
# File 'lib/modulorails.rb', line 140

def self_update
  Modulorails::SelfUpdateGenerator.new([], {}, {}).invoke_all unless configuration.no_auto_update
rescue StandardError => e
  puts("[Modulorails] An error occured: #{e.class} - #{e.message}")
end

.send_dataHTTParty::Response

Send the `#data` to the Intranet as JSON. HTTParty is used to send the POST request.

Returns:

  • (HTTParty::Response)

    The response of the intranet

Raises:

  • (Modulorails::Error)

    If the endpoint or the API key of the intranet were not configured

Author:

  • Matthieu 'ciappa_m' Ciappara



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
# File 'lib/modulorails.rb', line 72

def send_data
  # If no endpoint and/or no API key is configured, it is impossible to send the data to the
  # intranet and thus we raise an error: it is the only error we want to raise since it goes
  # against one of the main goals of the gem and the gem's user is responsible.
  if configuration.endpoint && configuration.api_key
    # Define the headers of the request ; sending JSON and API key to authenticate the gem on
    # the intranet
    headers = {
      'Content-Type' => 'application/json', 'X-MODULORAILS-TOKEN' => configuration.api_key
    }

    # Define the JSON body of the request
    body = data.to_params.to_json

    # Prevent HTTParty to raise error and crash the server in dev
    begin
      # Post to the configured endpoint on the Intranet
      response = HTTParty.post(configuration.endpoint, headers: headers, body: body)

      # According to the API specification, on a "Bad request" response, the server explicits what
      # went wrong with an `errors` field. We do not want to raise since the gem's user is not
      # (necessarily) responsible for the error but we still need to display it somewhere to warn
      # the user something went wrong.
      puts("[Modulorails] Error: #{response['errors'].join(', ')}") if response.code == 400

      # Return the response to allow users to do some more
      response
    rescue StandardError => e
      # Still need to notify the user
      puts("[Modulorails] Error: Could not post to #{configuration.endpoint}")
      puts e.message
      nil
    end
  else
    raise Error.new('No endpoint or api key')
  end
end