promisepay-ruby icon indicating copy to clipboard operation
promisepay-ruby copied to clipboard

Resource refactor

Open ngelx opened this issue 8 years ago • 0 comments

As the promisepay API resources are standard REST, almost all the classes *Resource do pretty much the same. There is no need of that such amount of code repetition that degrade the code quality and increase complexity. What i'm talking is doing something like:

module Promisepay
  class RestResource < BaseResource
        def find_all(options = {})
          response = JSON.parse(@client.get(resource_key_name, options).body)
          resources = response.key?(resource_key_name) ? response[resource_key_name] : []
          resources.map { |attributes| model.new(@client, attributes) }
         end
         # So on for all the others rest actions.
  end
end

module Promisepay
  # Resource for the Users API
  class UserResource < RestResource
    def model
      Promisepay::User
    end
    def resource_key_name
      "users"
    end
    # any extra method needed.
  end
end

ngelx avatar Jan 19 '17 10:01 ngelx