Dwolla / dwolla-v2-ruby

Official Ruby Wrapper for Dwolla's API v2
https://developers.dwolla.com/
MIT License
22 stars 17 forks source link

Open to PR creating a links method set #53

Open tadiou opened 4 years ago

tadiou commented 4 years ago

One thing I've been thinking about is how there should be an easier way to access the responses of links.

Example

DWOLLA_CLIENT = DwollaV2::Client.new(params)

funding_sources_link  =
  DWOLLA_CLIENT
    .get('customers', limit: 1)
    ._embedded.dig('customers', 0 '_links', 'funding-sources', 'href')

DWOLLA_CLIENT.get("#{funding_source_link}", params)

So, granted I understand the HAL schema y'all are working off of, the fundamental basis of the schema will remain the same, but ideally my end goal would be to go

DWOLLA_CLIENT = DwollaV2::Client.new(params)

DWOLLA_CLIENT
  .get('customers', limit: 1)
  .links[0].get.funding_sources(params) # or funding_sources('get', params)

to accomplish roughly the same thing, where .links dynamcially dispatches the link information to methods to then re-call the client.

So, codewise, I'd imagine that the https://github.com/Dwolla/dwolla-v2-ruby/blob/main/lib/dwolla_v2/response.rb would contain a method that would look directly into the response maps, pull out the direct child _links and _embedded links object and create new methods based on that. The only caveat that would be tricky to work through is carrying over the same client request for both, which I think given the way token works, by adding the token to the initializer of the Response, we should (:crossed_fingers:) be able to easily pass over the request structure to the links by going

class Response
  def initialize response, token
    @response = response
    @token = token
  end

  def links
    @response.body["_links"].keys.each do |link| 
      # ideally we'd snakecase link here
      define_method(method, link, params=nil, headers=nil) do |path, params, headers|
        full_url = self.class.full_url client, path
        @token.public_send(method, full_url, params_headers)
      end
    end
  end
end

Granted I haven't tested this, but just thought it up over lunch, but if this is something amenable, I'd be happy to fork and set up a PR.

sausman commented 4 years ago

Thanks @tadiou,

Feel free to open a pull request! I can't promise it's something we'll merge in, but we'd definitely like to make some improvements along those lines. At the very least it'd be great to see how you'd go about things.

I need to give this some more thought, but what do you think of something like:

dwolla = DwollaV2::Client.new(params)
customer = dwolla.get(customer_url)
funding_sources = dwolla.get(customer, :funding_sources, limit: 2)