Class: Blufin::SiteResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/core/site/site_resolver.rb

Constant Summary collapse

SITE =
'site'
SITE_NAME =
'name'
SITE_TITLE =
'title'
SITE_ALIAS =
'alias'
SITE_PORTS =
'ports'
PORT_RANGE_CRITERIA_ARRAY =
["\xe2\x80\x94 Consist of two 4-digit numbers separated by a '-' hyphen", "\xe2\x80\x94 Have a numerical difference of exactly 19.", "\xe2\x80\x94 The first number in the set must be divisible by 10.", nil, "An example of a valid #{Blufin::Terminal::format_highlight('port-range')} would be \xe2\x86\x92 #{Blufin::Terminal::format_action('6000-6019')}"]

Class Method Summary collapse

Class Method Details

.get_available_apisObject

Get an array of available APIs (for use in Blufin::Terminal messages).

Returns:

  • Array

[View source]

79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/core/site/site_resolver.rb', line 79

def self.get_available_apis
    apis_names = []
    apis_output = []
    apis = Blufin::Projects::get_apis
    apis.each { |api| apis_names << api[1][Blufin::Projects::PROJECT_NAME] }
    api_max_text = apis_names.max_by(&:length).length
    apis.each do |api|
        api = api[1]
        apis_output << "\x1B[38;5;154m#{api[Blufin::Projects::PROJECT_NAME].rjust(api_max_text, ' ')}\x1B[0m\x1B[38;5;240m \xe2\x86\x92 #{get_path_sites}/#{api[Blufin::Projects::PROJECT_NAME]} \x1B[38;5;67m[#{api[Blufin::Projects::PORT_RANGE]}] \x1B[38;5;154m#{api[Blufin::Projects::ALIAS]}"
    end
    apis_output
end

.get_site_details_for_validationObject

Get an Array of Hashes used for validation, listing purposes.

[View source]

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/core/site/site_resolver.rb', line 94

def self.get_site_details_for_validation
    validation_details = {
        :existing_locations => [],
        :existing_titles => [],
        :existing_aliases => [],
        :existing_names => [],
        :existing_ports => []
    }
    get_sitemap(false).each do |key, site|
        nil if key
        validation_details[:existing_locations] << site[:site_location].to_s
        validation_details[:existing_titles] << site[:site_title]
        validation_details[:existing_aliases] << site[:site_alias]
        validation_details[:existing_names] << site[:site_name]
        validation_details[:existing_names_camel_cased] << site[:site_name_camel_cased]
        validation_details[:existing_ports] << site[:site_ports].port_range_raw
    end
    validation_details[:existing_locations].uniq!
    validation_details[:existing_titles].uniq!
    validation_details[:existing_aliases].uniq!
    validation_details[:existing_names].uniq!
    validation_details[:existing_ports].uniq!
    validation_details
end

.get_site_domain(site) ⇒ Object

Gets the site domain.

Returns:

  • String

[View source]

25
26
27
28
# File 'lib/core/site/site_resolver.rb', line 25

def self.get_site_domain(site)
    validate_site(site)
    get_sitemap[site][:site_domain]
end

.get_site_location(site) ⇒ Object

Gets the PATH to the site repository.

Returns:

  • String

[View source]

18
19
20
21
# File 'lib/core/site/site_resolver.rb', line 18

def self.get_site_location(site)
    validate_site(site)
    get_sitemap[site][:site_location]
end

.get_site_name(site) ⇒ Object

Gets the (repository) name of the site.

Returns:

  • String

[View source]

32
33
34
35
# File 'lib/core/site/site_resolver.rb', line 32

def self.get_site_name(site)
    validate_site(site)
    get_sitemap[site][:site_name]
end

.get_site_name_camel_cased(site) ⇒ Object

Gets the (repository) name of the site in camel-cased version.

Returns:

  • String

[View source]

39
40
41
42
# File 'lib/core/site/site_resolver.rb', line 39

def self.get_site_name_camel_cased(site)
    validate_site(site)
    get_sitemap[site][:site_name_camel_cased]
end

.get_site_ports(site) ⇒ Object

Gets an object containing all the site ports.

Returns:

  • Blufin::SitePorts

[View source]

53
54
55
56
# File 'lib/core/site/site_resolver.rb', line 53

def self.get_site_ports(site)
    validate_site(site)
    get_sitemap[site][:site_ports]
end

.get_site_title(site) ⇒ Object

Gets the full name of the site.

Returns:

  • String

[View source]

46
47
48
49
# File 'lib/core/site/site_resolver.rb', line 46

def self.get_site_title(site)
    validate_site(site)
    get_sitemap[site][:site_title]
end

.invalidate_sitemapObject

Invalidates the sitemap so data will have to be re-retrieved.

Returns:

  • void

[View source]

137
138
139
# File 'lib/core/site/site_resolver.rb', line 137

def self.invalidate_sitemap
    @sitemap = nil
end

.validate_ports(site_ports) ⇒ Object

Validates ports. Needs to be in form of -> 6000-6019

Returns:

  • boolean

[View source]

121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/core/site/site_resolver.rb', line 121

def self.validate_ports(site_ports)
    return false unless site_ports.is_a?(String) && site_ports.include?('-')
    valid = true
    site_ports_split = site_ports.split('-')
    if site_ports_split.length == 2
        site_ports_split.each { |site_port| valid = false unless site_port =~ /\d{4}/ }
        valid = false unless site_ports_split[1].to_i - site_ports_split[0].to_i == 19
        valid = false unless site_ports_split[0].to_i % 10 == 0
    else
        valid = false
    end
    valid
end

.validate_site(site, location = true) ⇒ Object

Checks if the site exists and takes ‘aliases’ into account. Throws ERROR if site doesn’ts exist.

Returns:

  • String

[View source]

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/core/site/site_resolver.rb', line 61

def self.validate_site(site, location = true)
    if @site_valid[site].nil?
        sitemap = get_sitemap
        if !sitemap.keys.include?(site) || site.nil?
            Blufin::Terminal::error("An API by the name \"#{Blufin::Terminal::format_highlight(site.nil? ? '[nil]' : site)}\" cannot be found \xe2\x80\x94 available APIs are:", get_available_apis, true)
        else
            validate_location(get_sitemap[site][:site_location]) if location
            validate_ports_internal(get_sitemap[site][:site_ports].port_range_raw)
            @site_valid[site] = true
            site
        end
    else
        site
    end
end