Module: LatoCore::Interface::General

Included in:
LatoCore::Interface
Defined in:
lib/lato_core/interfaces/general.rb

Overview

This module contains a list of general functions used as helpers on the system.

Instance Method Summary collapse

Instance Method Details

#core__add_param_to_url(url, param_name, param_value) ⇒ Object

This function add a new GET param to an url string.



30
31
32
33
34
35
# File 'lib/lato_core/interfaces/general.rb', line 30

def core__add_param_to_url(url, param_name, param_value)
  uri = URI(url)
  params = URI.decode_www_form(uri.query || "") << [param_name, param_value]
  uri.query = URI.encode_www_form(params)
  uri.to_s
end

#core__get_string_inside_strings(string, marker1, marker2) ⇒ Object

This function return the substring inside two strings.



19
20
21
# File 'lib/lato_core/interfaces/general.rb', line 19

def core__get_string_inside_strings(string, marker1, marker2)
  string[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
end

#core__paginate_array(array, per_page, page) ⇒ Object

This function paginate an array and return the requested page.



24
25
26
27
# File 'lib/lato_core/interfaces/general.rb', line 24

def core__paginate_array(array, per_page, page)
  start = (page - 1) * per_page
  array[start, per_page]
end

#core__read_yaml(file_path) ⇒ Object

This function takes a path to a yaml file and return the hash with yaml data or nil if file not exist.



8
9
10
11
12
13
14
15
16
# File 'lib/lato_core/interfaces/general.rb', line 8

def core__read_yaml(file_path)
  # return nil if file not exist
  return unless File.exist?(file_path)
  config_file = File.read(file_path)
  # return yaml data
  return YAML.safe_load(config_file).with_indifferent_access
rescue
  nil
end