diff options
| -rw-r--r-- | lib/evernote/client.rb | 12 | ||||
| -rw-r--r-- | lib/evernote/user_store.rb | 17 | ||||
| -rw-r--r-- | spec/evernote/client_spec.rb | 9 | ||||
| -rw-r--r-- | spec/evernote/user_store_spec.rb | 32 | 
4 files changed, 54 insertions, 16 deletions
| diff --git a/lib/evernote/client.rb b/lib/evernote/client.rb index 1141087..e6962dc 100644 --- a/lib/evernote/client.rb +++ b/lib/evernote/client.rb @@ -1,13 +1,19 @@ +require 'forwardable' +  module Evernote    class Client -   +      THRIFT_DEFAULTS = {        :transport => Thrift::HTTPClientTransport      }.freeze      def initialize(klass, url, thrift_client_options = {})        thrift_opts = THRIFT_DEFAULTS.merge(thrift_client_options) -      ThriftClient.new(klass, url, thrift_opts) +      @client = ThriftClient.new(klass, url, thrift_opts) +    end + +    def method_missing(name, *args, &block) +      @client.send(name, *args, &block)      end    end -end
\ No newline at end of file +end diff --git a/lib/evernote/user_store.rb b/lib/evernote/user_store.rb index 1be8cb6..2b60756 100644 --- a/lib/evernote/user_store.rb +++ b/lib/evernote/user_store.rb @@ -1,5 +1,10 @@  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] @@ -10,7 +15,17 @@ module Evernote          raise ArgumentError, ":consumer_key, :consumer_secret, :username and :password are required"        end -      Evernote::Client.new(Evernote::EDAM::UserStore::UserStore::Client, uri, thrift_client_options) +      @client = Evernote::Client.new(Evernote::EDAM::UserStore::UserStore::Client, uri, thrift_client_options) + +      validate_version +    end + +    def validate_version +      raise VersionOutOfDate, "The vendored Evernote client code is out of date and needs to be regenerated" unless version_valid? +    end + +    def version_valid? +      @client.checkVersion("Ruby EDAMTest", Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR, Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)      end    end  end diff --git a/spec/evernote/client_spec.rb b/spec/evernote/client_spec.rb index 5d3d08c..2a75a74 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 "delegates method calls to the underlying ThriftClient" do +    client = Evernote::Client.new(Evernote::EDAM::UserStore::UserStore::Client, "https://www.example.com") +    internal_client = client.instance_variable_get(:@client) +    internal_client.stub(:checkVersion) +    internal_client.should_receive(:checkVersion) + +    client.checkVersion +  end  end diff --git a/spec/evernote/user_store_spec.rb b/spec/evernote/user_store_spec.rb index 623d80d..6196ffe 100644 --- a/spec/evernote/user_store_spec.rb +++ b/spec/evernote/user_store_spec.rb @@ -3,43 +3,51 @@ 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 = {}    end -  it "initializes an Evernote::Client" do -    Evernote::Client.should_receive(:new).with(Evernote::EDAM::UserStore::UserStore::Client, "https://sandbox.evernote.com/edam/user", {}) +  it "initializes an Evernote::Client and validate the client code version" do +    client = mock("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) +    Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, @thrift_client_opts)    end +  it "should raise an error on init if the version is not up to date" do +    client = mock("client", :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 +      it "raises an exception if no consumer key is set" do -    thrift_client_opts = {}      @opts.delete(:consumer_key)      lambda { -      Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, thrift_client_opts) +      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")    end -               +    it "raises an exception if no consumer secret is set" do -    thrift_client_opts = {}      @opts.delete(:consumer_secret)      lambda { -      Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, thrift_client_opts) +      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")    end    it "raises an exception if no username is set" do -    thrift_client_opts = {}      @opts.delete(:username)      lambda { -      Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, thrift_client_opts) +      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")    end    it "raises an exception if no password is set" do -    thrift_client_opts = {}      @opts.delete(:password)      lambda { -      Evernote::UserStore.new("https://sandbox.evernote.com/edam/user", @opts, thrift_client_opts) +      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")    end +  end | 
