Class: Inegi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/inegi_ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_indexes(hash) ⇒ Object

From a hash with Inegifacil’s structure, return a more readable one

Examples:

hash = { "header": { "INDICADOR": "XXXXXXXXXX" },
         "indices": [{ "TIME_PERIOD": "YYYY", "OBS_VALUE": "Z" }] }

format_indexes(hash) # { indicator: "XXXXXXXXXX", values: [..] }
                     # +values+ contains +period+, +value+ and +units+

Parameters:

  • hash (Hash)

    A hash containing results from service

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/inegi_ruby.rb', line 61

def self.format_indexes(hash)
  result = { indicator: hash["header"]["INDICADOR"] }
  values = hash["indices"].map do |v|
    {
      period: v["TIME_PERIOD"],
      value: v["OBS_VALUE"],
      units: v["OBS_UNIT"]
    }
  end
  values = { values: values }
  result.merge values
end

.validate_indicator(indicator) ⇒ Object

Validates that an indicator adheres to INEGI’s format It looks like indicators have a format of 10 digits

Examples:

validate_indicator("5300000041")  # true
validate_indicator("53000000412") # false
validate_indicator("")            # false
validate_indicator("a")           # false

Parameters:

  • indicator (String)

    Indicator to be evaluated



19
20
21
22
# File 'lib/inegi_ruby.rb', line 19

def self.validate_indicator(indicator)
  i_regex = /\A\d{10}\z/
  indicator =~ i_regex || raise(ArgumentError.new("Given indicator is not valid"))
end

.validate_location(location) ⇒ Object

Validates that a location adheres to INEGI’s format It looks like locations have a format of 5 digits

Examples:

validate_indicator("53000")       # true
validate_indicator("53000000412") # false
validate_indicator("")            # false
validate_indicator("a")           # false

Parameters:

  • location (String)

    Location to be evaluated



32
33
34
35
# File 'lib/inegi_ruby.rb', line 32

def self.validate_location(location)
  l_regex = /\A\d{5}\z/
  location =~ l_regex || raise(ArgumentError.new("Given location is not valid"))
end

Instance Method Details

#indexes(indicator, location = nil) ⇒ Object

After validating indicator, call Inegifacil’s service and return indexes.

Examples:

indexes("5300000041") # HTTParty::Response[...]

Parameters:

  • indicator (String)

    10 digits indicator

  • location (String) (defaults to: nil)

    5 digits location indicator



43
44
45
46
47
48
49
# File 'lib/inegi_ruby.rb', line 43

def indexes(indicator, location = nil)
  self.class.validate_indicator indicator
  self.class.validate_location location if location
  path = "/#{indicator}/#{location}"
  indexes = self.class.get path
  self.class.format_indexes indexes
end