Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More convenient indexing of array members #29

Open
wvengen opened this issue Oct 23, 2014 · 3 comments
Open

More convenient indexing of array members #29

wvengen opened this issue Oct 23, 2014 · 3 comments

Comments

@wvengen
Copy link

wvengen commented Oct 23, 2014

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.

@wvengen wvengen changed the title Return EmbedlyObject(-equivalent) for hash/array members More convenient indexing of array members Oct 23, 2014
@dokipen
Copy link
Contributor

dokipen commented Oct 23, 2014

Please submit a pull request.
On Oct 23, 2014 11:34 AM, "wvengen" [email protected] wrote:

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.

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
endend

but believe it could better be solved here.


Reply to this email directly or view it on GitHub
#29.

@wvengen
Copy link
Author

wvengen commented Oct 23, 2014

Good to hear you're open to the idea. I hope to get to this at some point in the future.

@dokipen
Copy link
Contributor

dokipen commented Oct 24, 2014

No problem, it's a potentially breaking change, so please add something to the ChangeLog describing it. I'll fill in the version number and date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants