Skip to content

Commit

Permalink
Rewrite of Flickr::Base.initialize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Bilas committed May 8, 2009
1 parent 5e8072e commit 9d1536c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 20 deletions.
58 changes: 38 additions & 20 deletions lib/flickr/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions spec/flickr/base_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9d1536c

Please sign in to comment.