require 'base64' module Ecoportal module API # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. # @attr_reader logger [Logger] the logger. class V2 extend Common::BaseClass include Common::Logging VERSION = "v2" class << self def v2key (ukey, gkey) Base64.urlsafe_encode64({ organization: gkey, user: ukey }.to_json) end end class_resolver :people_class, "Ecoportal::API::V2::People" class_resolver :registers_class, "Ecoportal::API::V2::Registers" class_resolver :pages_class, "Ecoportal::API::V2::Pages" attr_reader :client, :logger # Creates an `V2` object to scope version specific api requests. # @note # - You should use either `api_key` or `user_key` and `org_key` # - The const `VERSION` determineds the api version that client will query against. # - This means that each subclass of `V2` should define their own `VERSION` constant. # @param api_key [String] the key version to stablish the api connection. # @param user_key [String] the user key used for the api connection (requires `org_key`). # @param org_key [String] the org key used for the api connection (requires `user_key`). # @param host [String] api server domain. # @param logger [Logger] an object with `Logger` interface to generate logs. def initialize(api_key = nil, user_key: nil, org_key: nil, host: "live.ecoportal.com", logger: default_logger) v2key = get_key(api_key: api_key, user_key: user_key, org_key: org_key) @logger = logger @client = Common::Content::Client.new( api_key: v2key, host: host, version: self.class::VERSION, logger: @logger ) end # Obtain specific object for people api requests. # @return [People] an instance object ready to make people api requests. def people people_class.new(client) end # Obtain specific object for schema api requests. # @return [Registers] an instance object ready to make registers api requests. def registers registers_class.new(client) end # Obtain specific object for pages api requests. # @return [Pages] an instance object ready to make pages api requests. def pages pages_class.new(client) end private def get_key(api_key: nil, user_key: nil, org_key: nil) return self.class.v2key(user_key, org_key) if user_key && org_key return api_key if api_key #|| ENV['X_ECOPORTAL_API_KEY'] raise "You need to provide either an api_key or user_key" unless user_key raise "You need to provide an org_key as well (not just a user_key)" unless org_key end end end end require 'ecoportal/api/v2/people' require 'ecoportal/api/v2/pages' require 'ecoportal/api/v2/registers'