Skip to content

More convenient indexing of array members #29

Description

@wvengen

I find EmbedlyObject's behaviour to return nil for non-existent members convenient. It would be even more convenient if (hash and) array members would return an object that behaves in a similar way, so that for example response.images.first.name doesn't give an error when there are no associated images. I would have expected response.images to be something like an EmbedlyObject.

Right now I'm wrapping the response in an object containing something like

class MyEmbedly < SimpleDelegator
  def initialize(url)
    super(url)
  end

  def images
    (super || []).map &OpenStruct.method(:new)
  end

  private

  def data(url)
    @embedly ||= Embedly::API.new key: ENV['EMBEDLY_KEY']
    @data ||= @embedly.extract(url: url).first
  end
end

but believe it could better be solved here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions