Sha256: 8cb3fb07e67c07698c1fe8eea64a679ec75fa99a7b485e02cc0689955b33c685

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module IronBank
  # Base class for Zuora actions, e.g., subscribe
  #
  class Action
    private_class_method :new

    def self.call(args)
      new(args).call
    end

    def call
      @body = IronBank.client.connection.post(endpoint, params).body
      return body if success?

      raise ::IronBank::UnprocessableEntity, errors
    end

    private

    attr_reader :args, :body

    def initialize(args)
      @args = args
    end

    def endpoint
      "v1/action/#{name.downcase}"
    end

    def name
      self.class.name.split('::').last
    end

    def success?
      response_object.fetch(:success, true)
    end

    def response_object
      @response_object ||= begin
        return {} unless body.is_a?(Array)

        ::IronBank::Object.new(body.first).deep_underscore
      end
    end

    def errors
      { errors: response_object.fetch(:errors, []) }
    end

    def requests(type: :upper)
      args.fetch(:objects).map do |object|
        IronBank::Object.new(object).deep_camelize(type: type)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
iron_bank-1.0.4 lib/iron_bank/action.rb
iron_bank-1.0.3 lib/iron_bank/action.rb
iron_bank-1.0.2 lib/iron_bank/action.rb