Sha256: 59e29e48df3a26a5fc8cf1495b7a9f9deb3dcf49c2ca74d31ef3c98ddaa08acf

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 KB

Contents

module Ecoportal
  module API
    module Common
      module GraphQL
        module AuthService
          DEFAULT_SERVER = "live.ecoportal.com"

          module InstanceMethods
            def session_token(host: server, version: nil)
              http_client(host: host, version: version).post("/oauth/token", data: {
                "grant_type" => "password",
                "email"      => user_email,
                "password"   => user_pass
              }).yield_self do |response|
                response.body["access_token"] if response.success?
              end
            end

            private

            def http_client(host: server, version: nil)
              @http_client ||= Ecoportal::API::Common::GraphQL::HttpClient.new(host: host, version: version)
            end

            def user_email
              @user_email || fetch_env_required("USER_EMAIL")
            end

            def user_pass
              @user_pass || fetch_env_required("USER_PASS")
            end

            def server(default = DEFAULT_SERVER)
              ENV['SERVER'] || default
            end

            def fetch_env_required(name)
              abort("Missing ENV '#{name}'") unless ENV.key?(name)
              ENV[name]
            end

            def abort(msg)
              puts msg
              exit(1)
            end
          end

          module ClassMethods
          end

          class << self
            include InstanceMethods

            def included(base)
              base.send(:include, InstanceMethods)
              base.extend(ClassMethods)
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.3 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.3.1 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.2.3 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.2.2 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.2.1 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.11 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.10 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.9 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.8 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.7 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.6 lib/ecoportal/api/common/graphql/auth_service.rb
ecoportal-api-graphql-0.1.5 lib/ecoportal/api/common/graphql/auth_service.rb