module Alula class ClientConfiguration attr_accessor :api_url, :video_api_url, :debug, :user_agent REQUEST_ATTRIBUTES = %i[api_key video_api_key customer_id internal] # Using RequestStore so we're thread safe even with our non-thread-safe architecture :( REQUEST_ATTRIBUTES.each do |prop| define_method(prop) do RequestStore.store["alula_#{prop}"] end define_method("#{prop}=") do |value| RequestStore.store["alula_#{prop}"] = value end end # Stash the user type as a role for us to infer from later # You can set a symbolized role via role = :user, or provide the string userType # We cast to a symbolized role for ease of use in consumers. def role=(user_type) unless user_type.is_a?(Symbol) || user_type.is_a?(String) raise Alula::InvalidRoleError, 'Role must be symbol or string' end RequestStore.store['alula_role'] = Util.underscore(user_type).to_sym end def role RequestStore.store['alula_role'] end def ensure_api_key_set if api_key.nil? raise Alula::NotConfiguredError, 'Set your API access token before making requests with Alula::Client.config.api_key = {access_token}' end end def ensure_api_url_set if api_url.nil? raise Alula::NotConfiguredError, 'did you forget to set the Alula::Client.config.api_url config option?' end end def ensure_video_api_url_set return unless video_api_url.nil? raise Alula::NotConfiguredError, 'did you forget to set the Alula::Client.config.video_api_url config option?' end def ensure_video_api_key_set return if video_api_key raise Alula::NotConfiguredError, 'Set your video API access token before making requests to the video API' end def ensure_customer_id_set return if internal || customer_id raise Alula::NotConfiguredError, 'Set your customer_id before making requests to the video API' end def ensure_role_set if role.nil? message = 'User role not configured! You must set '\ 'Alula::Client.config.role '\ 'before attempting to save any resources' raise Alula::NotConfiguredError, message end end end end