Sha256: 4a8041db3392f07ab9d4ea2917574051f3a53fe7aef979c0019934008e56fda8

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Chargify
  class Migration < Base
    self.prefix = "/subscriptions/:subscription_id/"
    
    def self.preview(attrs = {})
      Chargify::Migration::Preview.create(attrs) 
    end

    def subscription
      self.attributes["id"].present? ? Chargify::Subscription.new(self.attributes) : nil
    end

    private

    # Chargify returns a non-standard XML error response for Migration.  Since the API wants to maintain
    # backwards compatibility, we will work around that when we parse errors here by overriding the
    # framework's `load_remote_errors` with our own for just XML
    def load_remote_errors(remote_errors, save_cache = false)
      case self.class.format
      when ActiveResource::Formats[:xml]
        array = [Hash.from_xml(remote_errors.response.body)["errors"]] rescue []
        errors.from_array array, save_cache
      else
        super
      end
    end

    class Preview < Base
      self.prefix = "/subscriptions/:subscription_id/migrations/"

      def create
        response = post(:preview, {}, attributes.send("to_#{self.class.format.extension}", :root => :migration, :dasherize => false))
        load_attributes_from_response(response)
      end

      private

      def custom_method_new_element_url(method_name, options = {})
        "#{self.class.prefix(prefix_options)}#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chargify_api_ares-1.0.1 lib/chargify_api_ares/resources/migration.rb
chargify_api_ares-1.0.0 lib/chargify_api_ares/resources/migration.rb