Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrite of Flickr::Base.initialize code.

  • Loading branch information...
commit 9d1536cf9de08030fb23c3d491a52191d2012bc5 1 parent 5e8072e
Maciej Bilas authored
Showing with 135 additions and 20 deletions.
  1. +38 −20 lib/flickr/base.rb
  2. +97 −0 spec/flickr/base_spec.rb
View
58 lib/flickr/base.rb
@@ -22,31 +22,49 @@ class Base
# Flickr::Auth::Token object
#
# or:
+ #
# * config_file (Required)
# yaml file to load configuration from
- # * token_cache (Optional)
- # location of the token cache file. This will override the setting in the config file
- #
+ # * options (Optional)
+ # hash containing any of the two options
+ # * token_cache
+ # location of the token cache file. This will override the setting in the config file
+ # * environment
+ # section in the config file that flickr_fu should look for the API key and secret
+ # Useful when using with Rails
+ #
# Config Example (yaml file)
# ---
- # key: YOUR_API_KEY
- # secret: YOUR_API_SECRET
- # token_cache: token.yml
- #
- def initialize(config_hash_or_file, token_cache = nil)
- if config_hash_or_file.is_a? Hash
- @api_key = config_hash_or_file[:key]
- @api_secret = config_hash_or_file[:secret]
- @token = config_hash_or_file[:token]
- raise 'config_hash must contain api key and secret' unless @api_key and @api_secret
- else
- config = YAML.load_file(config_hash_or_file)
-
- @api_key = config['key']
- @api_secret = config['secret']
- @token_cache = token_cache || config['token_cache']
- raise 'flickr config file must contain an api key and secret' unless @api_key and @api_secret
+ # key: YOUR_API_KEY
+ # secret: YOUR_API_SECRET
+ # token_cache: token.yml
+ #
+ # Example config file with two environments:
+ # ---
+ # development:
+ # key: YOUR_DEVELOPMENT_API_KEY
+ # secret: YOUR_DEVELOPMENT_API_SECRET
+ # production:
+ # key: YOUR_PRODUCTION_API_KEY
+ # secret: YOUR_PRODUCTION_API_SECRET
+ def initialize(config_param, options_param = {})
+ if options_param.is_a? String
+ options = {:token_cache => options_param}
+ else
+ options = options_param
+ end
+ if config_param.is_a? String
+ config = YAML.load_file(config_param)
+ config = config[options[:environment]] if options.has_key? :environment
+ else
+ config = config_param
end
+ @api_key = config[:key] || config["key"]
+ @api_secret = config[:secret] || config["secret"]
+ @token_cache = options[:token_cache] || config["token_cache"]
+ @token = config[:token] || options[:token]
+ raise 'config file must contain an api key and secret' unless @api_key and @api_secret
+ raise 'you cannot specify both the token and token_cache' if @token and @token_cache
end
# sends a request to the flickr REST api
View
97 spec/flickr/base_spec.rb
@@ -0,0 +1,97 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Flickr do
+
+ before :all do
+ @token = Flickr::Auth::Token.new(:permissions => "write", :token => "foo", :user_id => "80755658@N00",
+ :user_real_name => "Maciej Bilas", :username => "Maciej Bilas")
+ @api_key = "foo"
+ @api_secret = "bar"
+ @yaml_hash = {"key" => @api_key, "secret" => @api_secret}
+ end
+
+ describe ".new" do
+
+ describe "with no environment option specified" do
+ it "should inititialize Flickr from a Hash" do
+ # AFAIK there is no spec on how the key and secret look like
+ # so we can test with simple values
+ init_hash = {:key => @api_key, :secret => @api_secret}
+ flickr = Flickr.new(init_hash)
+ flickr.api_key.should == @api_key
+ flickr.api_secret.should == @api_secret
+
+ init_hash_with_token = init_hash.merge(:token => @token)
+ flickr = Flickr.new(init_hash_with_token)
+ flickr.token.should == @token
+ end
+
+ it "should initialize Flickr from a YAML file" do
+ YAML.should_receive(:load_file).once.and_return(@yaml_hash)
+ flickr = Flickr.new("flickr.yml")
+ flickr.api_key.should == @api_key
+ flickr.api_secret.should == @api_secret
+ end
+
+ it "should fail if API key or secret value is absent" do
+ invalid_hash = {:key => @api_key}
+ lambda { Flickr.new(invalid_hash) }.should raise_error
+ invalid_hash = {:secret => @api_secret}
+ lambda { Flickr.new(invalid_hash) }.should raise_error
+ end
+
+ end
+
+ describe "with an environment option specified" do
+ before :all do
+ @environment_specified = "development"
+ end
+
+ it "should initialize Flickr from a YAML file" do
+ yaml_hash = {@environment_specified => {"key" => @api_key, "secret" => @api_secret}}
+ YAML.should_receive(:load_file).once.and_return(yaml_hash)
+ flickr = Flickr.new("flickr.yml", :environment => @environment_specified)
+ flickr.api_key.should == @api_key
+ flickr.api_secret.should == @api_secret
+ end
+ end
+
+ describe "when token_cache is passed (only with a YAML file)" do
+
+ before :all do
+ @expected_token_cache = "token_cache.yml"
+ end
+
+ # For backward compatibility
+ it "should initialize Flickr with token_cache when passed as the second parameter" do
+ YAML.should_receive(:load_file).once.and_return(@yaml_hash)
+ flickr = Flickr.new("flickr.yml", @expected_token_cache)
+ flickr.token_cache.should == @expected_token_cache
+ end
+
+ it "should initialize Flickr with token_cache when passed as an option" do
+ YAML.should_receive(:load_file).once.and_return(@yaml_hash)
+ flickr = Flickr.new("flickr.yml", :token_cache => @expected_token_cache)
+ flickr.token_cache.should == @expected_token_cache
+ end
+ end
+
+ describe "when token is passed as an options (only with a YAML file)" do
+
+ it "should initialize Flickr with a token if passed" do
+ YAML.should_receive(:load_file).once.and_return(@yaml_hash)
+ flickr = Flickr.new("flickr.yml", :token => @token)
+ flickr.token.should == @token
+ end
+
+ end
+
+ describe "when both token and token_cache are passed" do
+ it "should raise an error" do
+ YAML.should_receive(:load_file).once.and_return(@yaml_hash)
+ lambda { Flickr.new("flickr.yml", {:token => @token, :token_cache => "token_cache.yml"})}.should raise_error
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.