# Recurly is a Ruby client for Recurly's REST API. module Recurly autoload :Account, 'recurly/account' autoload :AddOn, 'recurly/add_on' autoload :Address, 'recurly/address' autoload :TaxDetail, 'recurly/tax_detail' autoload :Adjustment, 'recurly/adjustment' autoload :API, 'recurly/api' autoload :BillingInfo, 'recurly/billing_info' autoload :Coupon, 'recurly/coupon' autoload :Helper, 'recurly/helper' autoload :Invoice, 'recurly/invoice' autoload :JS, 'recurly/js' autoload :Money, 'recurly/money' autoload :Plan, 'recurly/plan' autoload :Redemption, 'recurly/redemption' autoload :Resource, 'recurly/resource' autoload :Subscription, 'recurly/subscription' autoload :SubscriptionAddOn, 'recurly/subscription_add_on' autoload :Transaction, 'recurly/transaction' autoload :Version, 'recurly/version' autoload :XML, 'recurly/xml' autoload :Webhook, 'recurly/webhook' @subdomain = nil # The exception class from which all Recurly exceptions inherit. class Error < StandardError def set_message message @message = message end # @return [String] def to_s defined? @message and @message or super end end # This exception is raised if Recurly has not been configured. class ConfigurationError < Error end class << self # Set a config based on current thread context. # Any default set will say in effect unless overwritten in the config_params. # Call this method with out any arguments to have it unset the thread context config values. # @param config_params - Hash with the following keys: subdomain, api_key, default_currency def config(config_params = nil) Thread.current[:recurly_config] = config_params end # @return [String] A subdomain. def subdomain if Thread.current[:recurly_config] && Thread.current[:recurly_config][:subdomain] return Thread.current[:recurly_config][:subdomain] end @subdomain || 'api' end attr_writer :subdomain # @return [String] An API key. # @raise [ConfigurationError] If not configured. def api_key if Thread.current[:recurly_config] && Thread.current[:recurly_config][:api_key] return Thread.current[:recurly_config][:api_key] end defined? @api_key and @api_key or raise( ConfigurationError, "Recurly.api_key not configured" ) end attr_writer :api_key # @return [String, nil] A default currency. def default_currency if Thread.current[:recurly_config] && Thread.current[:recurly_config][:default_currency] return Thread.current[:recurly_config][:default_currency] end return @default_currency if defined? @default_currency @default_currency = 'USD' end attr_writer :default_currency # @return [JS] The Recurly.js module. def js JS end # Assigns a logger to log requests/responses and more. # # @return [Logger, nil] # @example # require 'logger' # Recurly.logger = Logger.new STDOUT # @example Rails applications automatically log to the Rails log: # Recurly.logger = Rails.logger # @example Turn off logging entirely: # Recurly.logger = nil # Or Recurly.logger = Logger.new nil attr_accessor :logger # Convenience logging method includes a Logger#progname dynamically. # @return [true, nil] def log level, message logger.send(level, name) { message } end if RUBY_VERSION <= "1.9.0" def const_defined? sym, inherit = false raise ArgumentError, "inherit must be false" if inherit super sym end def const_get sym, inherit = false raise ArgumentError, "inherit must be false" if inherit super sym end end end end require 'rails/recurly' if defined? Rails::Railtie