diff options
| -rw-r--r-- | README.mkd | 8 | ||||
| -rw-r--r-- | lib/evernote/user_store.rb | 12 | ||||
| -rw-r--r-- | spec/evernote/user_store_spec.rb | 26 |
3 files changed, 33 insertions, 13 deletions
@@ -9,7 +9,13 @@ Get yourself a "Client application" API key from Evernote (http://www.evernote.c require 'evernote' user_store_url = "https://sandbox.evernote.com/edam/user" - config={'username'=>'YOUR_USERNAME','password'=>'YOUR_PASSWORD','consumer_key'=>'YOUR_CONSUMER_KEY_FROM_EVERNOTE','consumer_secret'=>'YOUR_CONSUMER_SECRECT_FROM_EVERNOTE'} + + config = { + :username => 'YOUR_USERNAME', + :password => 'YOUR_PASSWORD', + :consumer_key => 'YOUR_CONSUMER_KEY_FROM_EVERNOTE', + :consumer_secret => 'YOUR_CONSUMER_SECRECT_FROM_EVERNOTE' + } # note, you could also read in your consumer key information from a YML file diff --git a/lib/evernote/user_store.rb b/lib/evernote/user_store.rb index 44574fc..884ca24 100644 --- a/lib/evernote/user_store.rb +++ b/lib/evernote/user_store.rb @@ -7,10 +7,14 @@ module Evernote def initialize(uri, credentials, thrift_client_options = {}) - @consumer_key = credentials["consumer_key"] - @consumer_secret = credentials["consumer_secret"] - @username = credentials["username"] - @password = credentials["password"] + raise ArgumentError, "credentials must be passed in as a hash" unless credentials.class == Hash + + credentials=credentials.inject({}) { |h,(k,v)| h[k.to_sym] = v; h } # convert any stringifyed hash keys into symbols + + @consumer_key = credentials[:consumer_key] + @consumer_secret = credentials[:consumer_key] + @username = credentials[:username] + @password = credentials[:password] unless @consumer_key && @consumer_secret && @username && @password raise ArgumentError, "'consumer_key', 'consumer_secret', 'username' and 'password' are required" diff --git a/spec/evernote/user_store_spec.rb b/spec/evernote/user_store_spec.rb index dcc33cd..1f769ee 100644 --- a/spec/evernote/user_store_spec.rb +++ b/spec/evernote/user_store_spec.rb @@ -2,44 +2,54 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Evernote::UserStore" do before(:each) do - @auth_file = File.dirname(__FILE__) + "/auth.yaml" - @env = :sandbox + @user_store_url = "https://sandbox.evernote.com/edam/user" + @config = { + :username => 'cgs', + :password => 'password', + :consumer_key => '12345', + :consumer_secret => 'ABCDE' + } end it "initializes an Evernote::Client and validate the client code version" do client = mock("Evernote::Client", :checkVersion => true) Evernote::Client.should_receive(:new).with(Evernote::EDAM::UserStore::UserStore::Client, "https://sandbox.evernote.com/edam/user", {}).and_return(client) - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + Evernote::UserStore.new(@user_store_url, @config) end it "should raise an error on init if the version is not up to date" do Evernote::Client.stub!(:new => mock("Evernote::Client", :checkVersion => false)) lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + Evernote::UserStore.new(@user_store_url, @config) }.should raise_error(Evernote::VersionOutOfDate, "The vendored Evernote client code is out of date and needs to be regenerated") end %w(consumer_key consumer_secret username password).each do |credential| it "raises an exception if no #{credential} is set" do lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, :invalid) + Evernote::UserStore.new(@user_store_url, Hash.new) }.should raise_error(ArgumentError, "'consumer_key', 'consumer_secret', 'username' and 'password' are required") end end it "should authenticate" do Evernote::Client.stub!(:new => mock("Evernote::Client", :checkVersion => true)) - user_store = Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + Evernote::UserStore.new(@user_store_url, @config) user_store.instance_variable_get(:@client).should_receive(:authenticate).with("cgs", "password", "12345", "ABCDE").and_return(nil) user_store.authenticate end + it "should raise an exception if the config parameter is not a hash" do + not_a_hash="hello, I am a string" + Evernote::UserStore.new(@user_store_url, not_a_hash).should raise_error(ArgumentError, "credentials must be passed in as a hash") + end + it "should wrap authentication failure" do Evernote::Client.stub!(:new => mock("Evernote::Client", :checkVersion => true)) - user_store = Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + Evernote::UserStore.new(@user_store_url, @config) user_store.instance_variable_get(:@client).should_receive(:authenticate).and_raise(Evernote::EDAM::Error::EDAMUserException) lambda { @@ -49,7 +59,7 @@ describe "Evernote::UserStore" do it "should proxy methods" do Evernote::Client.stub!(:new => mock("Evernote::Client", :checkVersion => true)) - user_store = Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + Evernote::UserStore.new(@user_store_url, @config) user_store.instance_variable_get(:@client).should_receive(:foobar).and_return(nil) user_store.foobar |
