Class: Dscf::Marketplace::GebetaService

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
app/services/dscf/marketplace/gebeta_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV["GEBETA_MAPS_API_KEY"]) ⇒ GebetaService

Returns a new instance of GebetaService.



11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/dscf/marketplace/gebeta_service.rb', line 11

def initialize(api_key: ENV["GEBETA_MAPS_API_KEY"])
  # Sanitize the key: strip whitespace and remove surrounding quotes if any
  @api_key = api_key.to_s.strip.gsub(/\A['"]+|['"]+\z/, "")

  if @api_key.blank?
    Rails.logger.error "[GebetaService] GEBETA_MAPS_API_KEY is missing or blank"
  else
    masked_key = "#{@api_key[0...4]}...#{@api_key[-4..-1]}" if @api_key.length > 8
    Rails.logger.info "[GebetaService] Initialized with key: #{masked_key || "too short"}, length: #{@api_key.length}"
  end
end

Instance Method Details

#geocode(place_name) ⇒ Array

Forward Geocoding

Parameters:

  • place_name (String)

    The name of the place to search for

Returns:

  • (Array)

    Array of matching location objects



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/dscf/marketplace/gebeta_service.rb', line 26

def geocode(place_name)
  response = self.class.get("/v1/route/geocoding", query: {
    name: place_name,
    apiKey: @api_key
  })

  if response.success?
    response.parsed_response
  else
    raise StandardError, "Gebeta API Error: #{response.code} - #{response.body}"
  end
end

#tsp(locations) ⇒ Object

Traveling Salesperson Solver Start point is assumed to be the first element.

Parameters:

  • locations (Array<Array>)

    Array of coordinate pairs [[lat, lng], [lat, lng], …]



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/dscf/marketplace/gebeta_service.rb', line 42

def tsp(locations)
  # Gebeta TSS API expects a custom string format: [{lat,lon},{lat,lon},...]
  # Example: [{8.9,38.7},{9.0,38.8}] - Literal curly braces, comma sep lat/lon

  custom_payload = "[" + locations.map { |loc| "{#{loc[0]},#{loc[1]}}" }.join(",") + "]"

  # We pass this string directly to the json query param.
  # HTTParty will encode the characters (e.g., { becomes %7B), which is expected.
  response = self.class.get("/route/tss", query: {
    apiKey: @api_key,
    json: custom_payload
  })

  if response.success?
    response.parsed_response
  else
    raise StandardError, "Gebeta API Error: #{response.code} - #{response.body}"
  end
end