Skip to content
This repository
Browse code

Rewrite of Flickr::Base.initialize code.

  • Loading branch information...
commit 9d1536cf9de08030fb23c3d491a52191d2012bc5 1 parent 5e8072e
Maciej Bilas authored

Showing 2 changed files with 135 additions and 20 deletions. Show diff stats Hide diff stats

  1. +38 20 lib/flickr/base.rb
  2. +97 0 spec/flickr/base_spec.rb
58 lib/flickr/base.rb
@@ -22,31 +22,49 @@ class Base
22 22 # Flickr::Auth::Token object
23 23 #
24 24 # or:
  25 + #
25 26 # * config_file (Required)
26 27 # yaml file to load configuration from
27   - # * token_cache (Optional)
28   - # location of the token cache file. This will override the setting in the config file
29   - #
  28 + # * options (Optional)
  29 + # hash containing any of the two options
  30 + # * token_cache
  31 + # location of the token cache file. This will override the setting in the config file
  32 + # * environment
  33 + # section in the config file that flickr_fu should look for the API key and secret
  34 + # Useful when using with Rails
  35 + #
30 36 # Config Example (yaml file)
31 37 # ---
32   - # key: YOUR_API_KEY
33   - # secret: YOUR_API_SECRET
34   - # token_cache: token.yml
35   - #
36   - def initialize(config_hash_or_file, token_cache = nil)
37   - if config_hash_or_file.is_a? Hash
38   - @api_key = config_hash_or_file[:key]
39   - @api_secret = config_hash_or_file[:secret]
40   - @token = config_hash_or_file[:token]
41   - raise 'config_hash must contain api key and secret' unless @api_key and @api_secret
42   - else
43   - config = YAML.load_file(config_hash_or_file)
44   -
45   - @api_key = config['key']
46   - @api_secret = config['secret']
47   - @token_cache = token_cache || config['token_cache']
48   - raise 'flickr config file must contain an api key and secret' unless @api_key and @api_secret
  38 + # key: YOUR_API_KEY
  39 + # secret: YOUR_API_SECRET
  40 + # token_cache: token.yml
  41 + #
  42 + # Example config file with two environments:
  43 + # ---
  44 + # development:
  45 + # key: YOUR_DEVELOPMENT_API_KEY
  46 + # secret: YOUR_DEVELOPMENT_API_SECRET
  47 + # production:
  48 + # key: YOUR_PRODUCTION_API_KEY
  49 + # secret: YOUR_PRODUCTION_API_SECRET
  50 + def initialize(config_param, options_param = {})
  51 + if options_param.is_a? String
  52 + options = {:token_cache => options_param}
  53 + else
  54 + options = options_param
  55 + end
  56 + if config_param.is_a? String
  57 + config = YAML.load_file(config_param)
  58 + config = config[options[:environment]] if options.has_key? :environment
  59 + else
  60 + config = config_param
49 61 end
  62 + @api_key = config[:key] || config["key"]
  63 + @api_secret = config[:secret] || config["secret"]
  64 + @token_cache = options[:token_cache] || config["token_cache"]
  65 + @token = config[:token] || options[:token]
  66 + raise 'config file must contain an api key and secret' unless @api_key and @api_secret
  67 + raise 'you cannot specify both the token and token_cache' if @token and @token_cache
50 68 end
51 69
52 70 # sends a request to the flickr REST api
97 spec/flickr/base_spec.rb
... ... @@ -0,0 +1,97 @@
  1 +require File.dirname(__FILE__) + '/../spec_helper'
  2 +
  3 +describe Flickr do
  4 +
  5 + before :all do
  6 + @token = Flickr::Auth::Token.new(:permissions => "write", :token => "foo", :user_id => "80755658@N00",
  7 + :user_real_name => "Maciej Bilas", :username => "Maciej Bilas")
  8 + @api_key = "foo"
  9 + @api_secret = "bar"
  10 + @yaml_hash = {"key" => @api_key, "secret" => @api_secret}
  11 + end
  12 +
  13 + describe ".new" do
  14 +
  15 + describe "with no environment option specified" do
  16 + it "should inititialize Flickr from a Hash" do
  17 + # AFAIK there is no spec on how the key and secret look like
  18 + # so we can test with simple values
  19 + init_hash = {:key => @api_key, :secret => @api_secret}
  20 + flickr = Flickr.new(init_hash)
  21 + flickr.api_key.should == @api_key
  22 + flickr.api_secret.should == @api_secret
  23 +
  24 + init_hash_with_token = init_hash.merge(:token => @token)
  25 + flickr = Flickr.new(init_hash_with_token)
  26 + flickr.token.should == @token
  27 + end
  28 +
  29 + it "should initialize Flickr from a YAML file" do
  30 + YAML.should_receive(:load_file).once.and_return(@yaml_hash)
  31 + flickr = Flickr.new("flickr.yml")
  32 + flickr.api_key.should == @api_key
  33 + flickr.api_secret.should == @api_secret
  34 + end
  35 +
  36 + it "should fail if API key or secret value is absent" do
  37 + invalid_hash = {:key => @api_key}
  38 + lambda { Flickr.new(invalid_hash) }.should raise_error
  39 + invalid_hash = {:secret => @api_secret}
  40 + lambda { Flickr.new(invalid_hash) }.should raise_error
  41 + end
  42 +
  43 + end
  44 +
  45 + describe "with an environment option specified" do
  46 + before :all do
  47 + @environment_specified = "development"
  48 + end
  49 +
  50 + it "should initialize Flickr from a YAML file" do
  51 + yaml_hash = {@environment_specified => {"key" => @api_key, "secret" => @api_secret}}
  52 + YAML.should_receive(:load_file).once.and_return(yaml_hash)
  53 + flickr = Flickr.new("flickr.yml", :environment => @environment_specified)
  54 + flickr.api_key.should == @api_key
  55 + flickr.api_secret.should == @api_secret
  56 + end
  57 + end
  58 +
  59 + describe "when token_cache is passed (only with a YAML file)" do
  60 +
  61 + before :all do
  62 + @expected_token_cache = "token_cache.yml"
  63 + end
  64 +
  65 + # For backward compatibility
  66 + it "should initialize Flickr with token_cache when passed as the second parameter" do
  67 + YAML.should_receive(:load_file).once.and_return(@yaml_hash)
  68 + flickr = Flickr.new("flickr.yml", @expected_token_cache)
  69 + flickr.token_cache.should == @expected_token_cache
  70 + end
  71 +
  72 + it "should initialize Flickr with token_cache when passed as an option" do
  73 + YAML.should_receive(:load_file).once.and_return(@yaml_hash)
  74 + flickr = Flickr.new("flickr.yml", :token_cache => @expected_token_cache)
  75 + flickr.token_cache.should == @expected_token_cache
  76 + end
  77 + end
  78 +
  79 + describe "when token is passed as an options (only with a YAML file)" do
  80 +
  81 + it "should initialize Flickr with a token if passed" do
  82 + YAML.should_receive(:load_file).once.and_return(@yaml_hash)
  83 + flickr = Flickr.new("flickr.yml", :token => @token)
  84 + flickr.token.should == @token
  85 + end
  86 +
  87 + end
  88 +
  89 + describe "when both token and token_cache are passed" do
  90 + it "should raise an error" do
  91 + YAML.should_receive(:load_file).once.and_return(@yaml_hash)
  92 + lambda { Flickr.new("flickr.yml", {:token => @token, :token_cache => "token_cache.yml"})}.should raise_error
  93 + end
  94 + end
  95 +
  96 + end
  97 +end

0 comments on commit 9d1536c

Please sign in to comment.
Something went wrong with that request. Please try again.