require 'graphlient' 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| if response.success? response.body["access_token"] end 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 fetch_env_required("USER_EMAIL") end def 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