lib/recurly.rb in recurly-0.4.16 vs lib/recurly.rb in recurly-2.0.0

- old
+ new

@@ -1,154 +1,79 @@ -require 'active_resource' -require 'active_support/deprecation' -require 'cgi' -require 'openssl' -require 'addressable/uri' - -# load ActiveResource patches -if ::ActiveResource::VERSION::MAJOR == 3 - if ::ActiveResource::VERSION::MINOR == 0 && - ::ActiveResource::VERSION::TINY < 20 - require 'patches/rails3/active_model/serializers/xml' - end - require 'patches/rails3/active_resource/connection' -elsif ::ActiveResource::VERSION::MAJOR == 2 - require 'patches/rails2/active_resource/connection' -end - -if ::ActiveResource::VERSION::MAJOR == 2 || - (::ActiveResource::VERSION::MAJOR == 3 && - ::ActiveResource::VERSION::MINOR == 0) - require 'patches/rails2/active_resource/base' -end - -require 'recurly/version' -require 'recurly/exceptions' -require 'recurly/formats/xml_with_pagination' -require 'recurly/formats/xml_with_errors' -require 'recurly/config_parser' -require 'recurly/rails3/railtie' if defined?(::Rails::Railtie) -require 'recurly/base' - -# load rails2 fixes -if ::ActiveResource::VERSION::MAJOR == 2 - require 'recurly/rails2/compatibility' -end - -# configuration +# Recurly is a Ruby client for Recurly's REST API. module Recurly + autoload :Account, 'recurly/account' + autoload :AddOn, 'recurly/add_on' + 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 :Transaction, 'recurly/transaction' + autoload :Version, 'recurly/version' + autoload :XML, 'recurly/xml' - autoload :Account, 'recurly/account' - autoload :AccountBase, 'recurly/account_base' - autoload :BillingInfo, 'recurly/billing_info' - autoload :Charge, 'recurly/charge' - autoload :Coupon, 'recurly/coupon' - autoload :Credit, 'recurly/credit' - autoload :Invoice, 'recurly/invoice' - autoload :Plan, 'recurly/plan' - autoload :Subscription, 'recurly/subscription' - autoload :Transaction, 'recurly/transaction' - autoload :Transparent, 'recurly/transparent' - autoload :Verification, 'recurly/verification' - - class << self - attr_accessor :api_key, :environment, :subdomain, :private_key - - # default Recurly.settings_path to config/recurly.yml - unless respond_to?(:settings_path) - def settings_path - @settings_path || "config/recurly.yml" - end - - def settings_path=(new_settings_path) - @settings_path = new_settings_path - end + # The exception class from which all Recurly exceptions inherit. + class Error < StandardError + def set_message message + @message = message end - def configured? - Base.user && Base.site + # @return [String] + def to_s + defined? @message and @message or super end + end - # Alias password to api_key - def password - self.api_key - end - def password=(password) - self.api_key = password - end + # This exception is raised if Recurly has not been configured. + class ConfigurationError < Error + end - # Ignore username - def username - nil + class << self + # @return [String] An API key. + # @raise [ConfigurationError] If not configured. + def api_key + defined? @api_key and @api_key or raise( + ConfigurationError, "Recurly.api_key not configured" + ) end - def username=(_) - end + attr_writer :api_key - def configure - if block_given? - yield self - - Base.user = api_key - Base.site = site_for_environment(environment) - - return true - else - if ENV["RECURLY_CONFIG"] - Recurly.configure_from_json(ENV["RECURLY_CONFIG"]) - else - Recurly.configure_from_yaml - end - end + # @return [String, nil] A default currency. + def default_currency + return @default_currency if defined? @default_currency + @default_currency = 'USD' end + attr_writer :default_currency - def site_for_environment(environment) - if environment == :development - "http://api.lvh.me:3000" - else - "https://api.recurly.com" - end + # @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 - # allows configuration from a yml file that contains the fields: - # api_key,site,private_key - def configure_from_yaml(path = nil) - configure do |c| - # parse configuration from yml - recurly_config = ConfigParser.parse(path) - - if recurly_config.present? - - # check for environment specific config - recurly_env = Rails.env if defined?(Rails) and Rails.respond_to?(:env) - recurly_env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" - recurly_config = recurly_config[recurly_env] if recurly_config.has_key?(recurly_env) - - c.api_key = recurly_config["api_key"] || recurly_config["password"] - c.subdomain = recurly_config["subdomain"] - c.private_key = recurly_config["private_key"] - c.environment = recurly_config["environment"] - end - end + # Convenience logging method includes a Logger#progname dynamically. + # @return [true, nil] + def log level, message + logger and logger.send(level, name) { message } end - - # allows configuration from a json string that contains the fields: - # api_key,site,private_key - def configure_from_json(json_string) - config_data = ActiveSupport::JSON.decode(json_string) - configure do |c| - c.api_key = config_data['api_key'] || config_data['password'] - c.subdomain = config_data['subdomain'] - c.private_key = config_data['private_key'] - c.environment = config_data['environment'] - end - end - - def current_accept_language - Thread.current[:recurly_accept_language] - end - - def current_accept_language=(accept_language) - Thread.current[:recurly_accept_language] = accept_language - end end end + +require 'rails/recurly' if defined? Rails::Railtie