Sha256: a8a469e476d671598c5a76c1a9ee4bf7f073cd7aee5a66e81e4d81dcd8aec6de

Contents?: true

Size: 1.51 KB

Versions: 44

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module PlatformSdk
  module IdMapper
    # IdMapper::Client
    class Client
      attr_reader :token, :domain, :conn

      def initialize(secret)
        @token = secret[:token]
        @domain = secret[:domain]
        @token ||= PlatformSdk::IdMapper.token
        @domain ||= PlatformSdk::IdMapper.domain

        @conn = Faraday.new("https://#{@domain}") do |faraday|
          faraday.headers = headers
          faraday.adapter Faraday.default_adapter
          faraday.response :json, content_type: /\bjson$/
          faraday.response :raise_error
        end
      end

      private
      def models
        PlatformSdk::IdMapper::Models
      end

      def get(path, params = nil)
        response = @conn.get(path, params)
        response.body
      end

      def headers
        { "Authorization" => "Token #{@token}" }
      end

      def list_of_models(model, list)
        list.map do |item|
          model.new(item)
        end
      end

      public

      def pairs(guid)
        path = "api/v1/pairs/#{guid}"
        begin
          get(path)
        rescue Faraday::ResourceNotFound
          raise PlatformSdk::IdMapper::PairNotFoundError, msg = guid
        end
      end

      def domains(**params)
        path = 'api/v1/domains/'
        body = get(path, params)
        list_of_models(models::Domain, body)
      end

      def domain_by_name(name)
        path = "api/v1/domains/#{name}/"
        body = get(path)
        models::Domain.new(body)
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.19.17 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.16 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.15 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.14 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.13 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.12 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.11 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.19.10 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.14.2 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.14.1 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.14.0 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.13.0 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.12.0 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.11.2 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.11.1 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.11.0 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.10.3 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.10.2 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.10.1 lib/platform_sdk/id_mapper/client.rb
strongmind-platform-sdk-3.10.0 lib/platform_sdk/id_mapper/client.rb