Skip to content

Open to PR creating a links method set #53

Description

@itsdanferrellnow

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions