Sha256: e19ffeac1e2ff0e4d16bb1a8f2ba4a9e0ca4273ee71d5be4dfe9d940268d4d08

Contents?: true

Size: 1.31 KB

Versions: 14

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true
require 'http'

module Skyscanner
  # Service for all SkyScanner API calls
  class SkyscannerApi
    SKY_URL = 'http://partners.api.skyscanner.net/apiservices/browseroutes/'
    API_VER = 'v1.0'
    SKY_API_URL = URI.join(SKY_URL, "#{API_VER}/")

    def self.config=(credentials)
      @config = {} unless @config
      @config.update(credentials)
    end

    def self.config
      return @config if @config
      @config = {
        api_key: ENV['SKY_API_KEY']
      }
    end

    def self.routes_info(data)
      skyscanner_routes_response = HTTP.get(
        route_info_url(data),
        params: {
          apiKey: config['api_key']
        }
      )
      JSON.load(skyscanner_routes_response.to_s)
    end

    private

    def self.route_info_url(data)
      URI.join(
        SKY_API_URL,
        route_env_param(data[:market], data[:currency], data[:locale]),
        route_place_param(data[:origin], data[:destination]),
        route_date_param(data[:outbound], data[:inbound])
      )
    end

    def self.route_env_param(market, currency, locale)
      "#{market}/#{currency}/#{locale}/"
    end

    def self.route_place_param(origin, destination)
      "#{origin}/#{destination}/"
    end

    def self.route_date_param(outbound, inbound)
      "#{outbound}/#{inbound}"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
movlog-0.2.12 lib/movlog/skyscanner_api.rb
movlog-0.2.11 lib/movlog/skyscanner_api.rb
movlog-0.2.10 lib/movlog/skyscanner_api.rb
movlog-0.2.9 lib/movlog/skyscanner_api.rb
movlog-0.2.8 lib/movlog/skyscanner_api.rb
movlog-0.2.7 lib/movlog/skyscanner_api.rb
movlog-0.2.6 lib/movlog/skyscanner_api.rb
movlog-0.2.5 lib/movlog/skyscanner_api.rb
movlog-0.2.4 lib/movlog/skyscanner_api.rb
movlog-0.2.3 lib/movlog/skyscanner_api.rb
movlog-0.2.2 lib/movlog/skyscanner_api.rb
movlog-0.2.1 lib/movlog/skyscanner_api.rb
movlog-0.2.0 lib/movlog/skyscanner_api.rb
movlog-0.1.0 lib/movlog/skyscanner_api.rb