hanklords/flickraw

flickr method defined globally

Closed this issue · 12 comments

This gem creates an unscoped flickr method. It interferes with scopes and other things of the same name. It ends up causing this exception:

"RuntimeError: No API key or secret defined !"

I was able to get around that error by moving the require line from outside the class to inside a method declaration.

You need to set your api key and secret like that :

require 'flickraw'

FlickRaw.api_key = "... Your API key ..."
FlickRaw.shared_secret = "... Your shared secret ..."

The global flickr method is not a bug, it is part of the API.

Sorry, I don't think you understand. The bug is that it interferes with ActiveRecord scopes. Here's the scenario:

class SomeModel
  has_many :foos
end

class Foo
  belongs_to :some_model
  scope :flickr, where(:name => 'flickr')
end

Somewhere else in the codebase I require 'flickraw' and then when you try to use the scope above like this:

some_model.foos.flickr

It blows up with "RuntimeError: No API key or secret defined !"

Setting the api_key/secret is not the solution. I've done that elsewhere in the code. It seems like a bad design to just create the flickr method somewhere in the object graph outside of one of your modules. It's time for that feature to grow up. :)

The only workaround I found was to require 'flickraw' from within the method that makes the flickr client such that it wasn't required at base scope, like this:

def client
  require 'flickraw'
  @client = FlickRaw::Flickr.new
  @client.api_key = Settings.flickr.api_key
  @client.shared_secret = Settings.flickr.api_secret
  @client
end

It's also worth mentioning that I'm seeing these issues while running on JRuby 1.7.3, I haven't tried with MRI.

OK I get it. However this is a bug in ActiveRecord, not in flickraw... It will be triggered when you call any of Object methods.

I think the clean way to do what you want is:

require "flickraw"
class Object; remove_method :flickr end

The global flickr method is not going to be removed from flickraw.

9mm commented

The 'flickr' global variable in this is absolutely fucking insane... is it 1992? It's wrong on so many levels that it's painful.

I don't want to pour gasoline on the fire here, but I have to agree with the others here. It's a really unusual and unexpected way of doing things. And generally, unexpected and unusual behaviour is not a great feature of a gem.

Imagine if every gem started dumping methods in the global scope.

Would you consider revising your position on the issue?

9mm commented

My favorite part is this: However this is a bug in ActiveRecord, not in flickraw

Yes, if I douse your house with gasoline and burn it to the ground it MUST be the gasoline manufacturers fault.

9mm commented

I'll see myself out before I make it worse, everyone have a good day.

It can be done with a version bump.
Also doc and examples need to be updated

I can do a PR for the documentation if you like

@cyclotron3k send your PR :)

I created PR #102 👍

Done. The changes can't be released as part of the 0.9.x series because they are breaking changes, but they can be found in the Flickr gem, version 2.x.x