diff options
| -rw-r--r-- | Rakefile | 2 | ||||
| -rw-r--r-- | VERSION | 2 | ||||
| -rw-r--r-- | evernote.gemspec | 30 | ||||
| -rw-r--r-- | lib/evernote/client.rb | 5 | ||||
| -rw-r--r-- | lib/evernote/user_store.rb | 34 | ||||
| -rw-r--r-- | spec/evernote/auth.yaml | 11 | ||||
| -rw-r--r-- | spec/evernote/client_spec.rb | 9 | ||||
| -rw-r--r-- | spec/evernote/user_store_spec.rb | 72 |
8 files changed, 97 insertions, 68 deletions
@@ -9,7 +9,7 @@ begin gem.email = "chris.sepic@gmail.com" gem.homepage = "http://github.com/cgs/evernote" gem.authors = ["Chris Sepic"] - gem.files.include %w{vendor/**/*} + gem.files.include %w{vendor/**/* spec/evernote/auth.yaml} gem.add_dependency("thrift_client") gem.add_development_dependency "rspec" gem.add_development_dependency "yard" @@ -1 +1 @@ -0.1.0 +0.2.0 diff --git a/evernote.gemspec b/evernote.gemspec index a6ee8d8..b630d3c 100644 --- a/evernote.gemspec +++ b/evernote.gemspec @@ -1,15 +1,15 @@ # Generated by jeweler -# DO NOT EDIT THIS FILE -# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{evernote} - s.version = "0.1.0" + s.version = "0.2.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Sepic"] - s.date = %q{2010-01-30} + s.date = %q{2010-02-27} s.email = %q{chris.sepic@gmail.com} s.extra_rdoc_files = [ "LICENSE", @@ -24,32 +24,24 @@ Gem::Specification.new do |s| "VERSION", "evernote.gemspec", "lib/evernote.rb", - "spec/evernote_spec.rb", + "lib/evernote/client.rb", + "lib/evernote/user_store.rb", + "spec/evernote/auth.yaml", + "spec/evernote/client_spec.rb", + "spec/evernote/user_store_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "vendor/gen-rb/evernote.rb", - "vendor/gen-rb/evernote.rb", - "vendor/gen-rb/evernote/edam/errors_types.rb", "vendor/gen-rb/evernote/edam/errors_types.rb", "vendor/gen-rb/evernote/edam/limits_constants.rb", - "vendor/gen-rb/evernote/edam/limits_constants.rb", "vendor/gen-rb/evernote/edam/limits_types.rb", - "vendor/gen-rb/evernote/edam/limits_types.rb", - "vendor/gen-rb/evernote/edam/note_store.rb", "vendor/gen-rb/evernote/edam/note_store.rb", "vendor/gen-rb/evernote/edam/note_store_constants.rb", - "vendor/gen-rb/evernote/edam/note_store_constants.rb", "vendor/gen-rb/evernote/edam/note_store_types.rb", - "vendor/gen-rb/evernote/edam/note_store_types.rb", - "vendor/gen-rb/evernote/edam/types_constants.rb", "vendor/gen-rb/evernote/edam/types_constants.rb", "vendor/gen-rb/evernote/edam/types_types.rb", - "vendor/gen-rb/evernote/edam/types_types.rb", "vendor/gen-rb/evernote/edam/user_store.rb", - "vendor/gen-rb/evernote/edam/user_store.rb", - "vendor/gen-rb/evernote/edam/user_store_constants.rb", "vendor/gen-rb/evernote/edam/user_store_constants.rb", - "vendor/gen-rb/evernote/edam/user_store_types.rb", "vendor/gen-rb/evernote/edam/user_store_types.rb" ] s.homepage = %q{http://github.com/cgs/evernote} @@ -58,7 +50,8 @@ Gem::Specification.new do |s| s.rubygems_version = %q{1.3.5} s.summary = %q{High level wrapper for the Evernote API} s.test_files = [ - "spec/evernote_spec.rb", + "spec/evernote/client_spec.rb", + "spec/evernote/user_store_spec.rb", "spec/spec_helper.rb" ] @@ -81,3 +74,4 @@ Gem::Specification.new do |s| s.add_dependency(%q<yard>, [">= 0"]) end end + diff --git a/lib/evernote/client.rb b/lib/evernote/client.rb index 1522d59..6293f17 100644 --- a/lib/evernote/client.rb +++ b/lib/evernote/client.rb @@ -1,6 +1,5 @@ module Evernote class Client - attr_reader :client THRIFT_DEFAULTS = { :transport => Thrift::HTTPClientTransport @@ -10,5 +9,9 @@ module Evernote thrift_opts = THRIFT_DEFAULTS.merge(thrift_client_options) @client = ThriftClient.new(klass, url, thrift_opts) end + + def method_missing(name, *args, &block) + @client.send(name, *args, &block) + end end end diff --git a/lib/evernote/user_store.rb b/lib/evernote/user_store.rb index b9ccc17..b8b6102 100644 --- a/lib/evernote/user_store.rb +++ b/lib/evernote/user_store.rb @@ -3,22 +3,34 @@ module Evernote VersionOutOfDate = Class.new(StandardError) class UserStore - attr_reader :client - - def initialize(uri, opts = {}, thrift_client_options = {}) - @consumer_key = opts[:consumer_key] - @consumer_secret = opts[:consumer_secret] - @username = opts[:username] - @password = opts[:password] + AuthenticationFailure = Class.new(StandardError) + + def initialize(uri, auth_file, auth_env, thrift_client_options = {}) + credentials = YAML.load_file(auth_file)[auth_env.to_s] + + @consumer_key = credentials["consumer_key"] + @consumer_secret = credentials["consumer_secret"] + @username = credentials["username"] + @password = credentials["password"] unless @consumer_key && @consumer_secret && @username && @password - raise ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required" + raise ArgumentError, "'consumer_key', 'consumer_secret', 'username' and 'password' are required" end @client = Evernote::Client.new(Evernote::EDAM::UserStore::UserStore::Client, uri, thrift_client_options) - + validate_version end + + def authenticate + @client.authenticate(@username, @password, @consumer_key, @consumer_secret) + rescue Evernote::EDAM::Error::EDAMUserException + raise AuthenticationFailure + end + + def method_missing(name, *args, &block) + @client.send(name, *args, &block) + end def validate_version raise VersionOutOfDate, "The vendored Evernote client code is out of date and needs to be regenerated" unless version_valid? @@ -27,9 +39,5 @@ module Evernote def version_valid? checkVersion("Ruby EDAMTest", Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR, Evernote::EDAM::UserStore::EDAM_VERSION_MINOR) end - - def method_missing(name, *args, &block) - @client.client.send(name, *args, &block) - end end end diff --git a/spec/evernote/auth.yaml b/spec/evernote/auth.yaml new file mode 100644 index 0000000..f238d53 --- /dev/null +++ b/spec/evernote/auth.yaml @@ -0,0 +1,11 @@ +sandbox: + username: cgs + password: password + consumer_key: "12345" + consumer_secret: ABCDE + +invalid: + username: + password: + consumer_key: + consumer_secret: diff --git a/spec/evernote/client_spec.rb b/spec/evernote/client_spec.rb index 5d3d08c..a2902f2 100644 --- a/spec/evernote/client_spec.rb +++ b/spec/evernote/client_spec.rb @@ -8,4 +8,13 @@ describe "Evernote::Client" do Evernote::Client.new(klass, "https://www.example.com") end + + it "should proxy methods" do + thrift_client = mock("ThriftClient") + ThriftClient.stub!(:new => thrift_client) + thrift_client.should_receive(:foobar) + + client = Evernote::Client.new(mock("SomeInternalEvernoteClass"), "https://www.example.com") + client.foobar + end end diff --git a/spec/evernote/user_store_spec.rb b/spec/evernote/user_store_spec.rb index 7b0127e..dcc33cd 100644 --- a/spec/evernote/user_store_spec.rb +++ b/spec/evernote/user_store_spec.rb @@ -2,52 +2,56 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Evernote::UserStore" do before(:each) do - @opts = { :consumer_key => "12345", :consumer_secret => "ABCDE", :username => "cgs", :password => "password" } - @thrift_client_opts = {} + @auth_file = File.dirname(__FILE__) + "/auth.yaml" + @env = :sandbox end it "initializes an Evernote::Client and validate the client code version" do - client = mock("Evernote::Client", :client => mock("ThriftClient", :checkVersion => true)) + 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", @opts, @thrift_client_opts) + + Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) end - + it "should raise an error on init if the version is not up to date" do - client = mock("Evernote::Client", :client => mock("ThriftClient", :checkVersion => false)) - Evernote::Client.should_receive(:new).with(Evernote::EDAM::UserStore::UserStore::Client, "https://sandbox.evernote.com/edam/user", {}).and_return(client) - - lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts) - }.should raise_error(Evernote::VersionOutOfDate, "The vendored Evernote client code is out of date and needs to be regenerated") - end + Evernote::Client.stub!(:new => mock("Evernote::Client", :checkVersion => false)) - it "raises an exception if no consumer key is set" do - @opts.delete(:consumer_key) lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts) - }.should raise_error(ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required") + Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @auth_file, @env) + }.should raise_error(Evernote::VersionOutOfDate, "The vendored Evernote client code is out of date and needs to be regenerated") end - it "raises an exception if no consumer secret is set" do - @opts.delete(:consumer_secret) - lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts) - }.should raise_error(ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required") + %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) + }.should raise_error(ArgumentError, "'consumer_key', 'consumer_secret', 'username' and 'password' are required") + end end - - it "raises an exception if no username is set" do - @opts.delete(:username) - lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts) - }.should raise_error(ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required") + + 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) + user_store.instance_variable_get(:@client).should_receive(:authenticate).with("cgs", "password", "12345", "ABCDE").and_return(nil) + + user_store.authenticate end - - it "raises an exception if no password is set" do - @opts.delete(:password) + + 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) + user_store.instance_variable_get(:@client).should_receive(:authenticate).and_raise(Evernote::EDAM::Error::EDAMUserException) + lambda { - Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts) - }.should raise_error(ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required") + user_store.authenticate + }.should raise_error(Evernote::UserStore::AuthenticationFailure) + end + + 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) + user_store.instance_variable_get(:@client).should_receive(:foobar).and_return(nil) + + user_store.foobar end - end |
