Class: Senec::Local::State
- Inherits:
-
Object
- Object
- Senec::Local::State
- Defined in:
- lib/senec/local/state.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#initialize(connection:) ⇒ State
constructor
A new instance of State.
-
#names(language: :de) ⇒ Object
Extract state names from JavaScript file, which is formatted like this:.
Constructor Details
#initialize(connection:) ⇒ State
Returns a new instance of State.
4 5 6 |
# File 'lib/senec/local/state.rb', line 4 def initialize(connection:) @connection = connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/senec/local/state.rb', line 8 def connection @connection end |
Instance Method Details
#names(language: :de) ⇒ Object
Extract state names from JavaScript file, which is formatted like this:
var system_state_name = {
0: "FIRST STATE",
1: "SECOND STATE",
...
};
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/senec/local/state.rb', line 17 def names(language: :de) js_content = response(language:) # Extract the matched content match = js_content.match(FILE_REGEX) return unless match # Extract the JSON-like part json_like = "{#{match[1]}}" # The keys are numbers, which is not valid JSON, so we need to convert them to strings json = json_like.gsub(/(\d+)\s*:/, '"\1":') # Convert the JSON string to a Ruby hash hash = JSON.parse(json) # Convert keys from strings to integers hash.transform_keys(&:to_i) end |