Class: Latta::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/latta/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
15
# File 'lib/latta/configuration.rb', line 11

def initialize
  @api_key = nil
  @relation_id = SecureRandom.uuid
  @instance_id = nil
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/latta/configuration.rb', line 9

def api_key
  @api_key
end

#instance_idObject

Returns the value of attribute instance_id.



9
10
11
# File 'lib/latta/configuration.rb', line 9

def instance_id
  @instance_id
end

#relation_idObject

Returns the value of attribute relation_id.



9
10
11
# File 'lib/latta/configuration.rb', line 9

def relation_id
  @relation_id
end

Instance Method Details

#fetch_instance_idObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/latta/configuration.rb', line 17

def fetch_instance_id
  raise "API key must be set before fetching ID" if @api_key.nil?
  url = "#{LattaProperties::LATTA_API_URI}/#{LattaEndpoints::LATTA_PUT_INSTANCE}"
  uname = Sys::Uname.uname

  locale = I18n.locale.to_s
  locale = locale.split('-').first if locale.include?('-')
  language = ISO_639.find(locale)
  lang = 'en'

  if language
    lang = language.alpha2
  end


  response = HTTParty.put(url,body:{
      framework: "rails",
        framework_version: Rails.version,
        device: 'server',
        lang: lang,
        os: uname.sysname, 
        os_version: uname.release
  }.to_json, headers: {
      "Content-Type" => "application/json",
        "Authorization" => "Bearer #{@api_key}"
    })

  if response.success?
    @instance_id = JSON.parse(response.body)["id"]
  else
    raise "Failed to fetch ID from API: #{response.code} - #{response.body}"
  end
rescue => e
  Rails.logger.error "Error fetching API provided ID: #{e.message}"
  raise
end