diff --git a/Manifest b/Manifest index c8ec1ff..99855da 100644 --- a/Manifest +++ b/Manifest @@ -3,11 +3,12 @@ Gemfile.lock Manifest README.md Rakefile -bin/thimbl +bin/thimblr lib/thimbl.rb lib/thimbl/base.rb lib/thimbl/command.rb lib/thimbl/finger.rb +lib/thimbl/utils.rb test/fixtures/cache.json test/fixtures/finger_dk_telekommunisten_org.txt test/fixtures/finger_dk_telekommunisten_org_two_break_lines.txt diff --git a/README.md b/README.md index 6f2d5f6..3cc77e7 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ I have follow the style of the [Thimbl Python client](https://github.com/blippy/ ## Commands -* setup * follow * fetch * post @@ -30,44 +29,34 @@ This version is in development, not ready for any production environment. require 'rubygems' require 'thimbl' thimbl = - Thimbl::Base.new( - 'plan_path' => '/tmp/plan', - 'cache_path' => '/tmp/thimbl_cache', - 'user' => 'fguillen@thimblserver.com', - 'password' => 'my_thimblserver_password' + Thimbl::Base.new( + 'bio' => 'my bio', + 'website' => 'my website', + 'mobile' => 'my mobile', + 'email' => 'my email', + 'address' => 'me@thimbl.net', + 'name' => 'my name' ) - thimbl.setup( - 'bio' => 'my bio', - 'website' => 'my website', - 'mobile' => 'my mobile', - 'email' => 'my email', - 'address' => 'my address', - 'name' => 'my name' - ) thimbl.follow 'dk', 'dk@telekommunisten.org' thimbl.fetch thimbl.print thimbl.post 'My first post' - thimbl.push + thimbl.push 'password' ## Shell Command The gem comes with a *shell command*, you can use it like this: - - thimbl setup ~/thimbl_plan ~/thimbl_cache 'user@thimblserver.com' 'thimblpass' - thimbl follow 'dk' 'dk@telekommunisten.org' - thimbl fetch - thimbl print - thimbl post "My first message :)" - thimbl push + + thimblr setup 'user@thimblrserver.com' + thimblr follow 'dk' 'dk@telekommunisten.org' + thimblr fetch + thimblr print + thimblr post "My first message :)" + thimblr push ## TODO -* Shell script /bin/thimbl -* Thinking that the *plan_path* is not needed. -* Not save thimbl password, request for it in any *thimbl.push* * Support *simbolize* hash keys * In the Thimbl::Command.setup ask for the rest of the configuration options *bio*, *mobile*, ... * thimbl.unfollow -* ERROR: If finger respond empty Plan -* ERROR: the message format is without *address* key. \ No newline at end of file +* ERROR: If finger respond empty Plan \ No newline at end of file diff --git a/Rakefile b/Rakefile index 64030b0..c82c246 100644 --- a/Rakefile +++ b/Rakefile @@ -2,12 +2,12 @@ require 'rubygems' require 'rake' require 'echoe' -Echoe.new('thimbl', '0.0.3') do |p| +Echoe.new('thimbl', '0.1.1') do |p| p.description = "Small client for the distributed microbloging protocol: [thimbl](http://www.thimbl.net/)" p.url = "http://github.com/fguillen/ThimblClient" p.author = "Fernando Guillen" p.email = "fguillen.mail@gmail.com" p.ignore_pattern = ['etc/*'] p.development_dependencies = ['mocha', 'ruby-debug', 'delorean'] - p.runtime_dependencies = ['json', 'net-scp'] + p.runtime_dependencies = ['json', 'net-scp', 'tempfile'] end \ No newline at end of file diff --git a/bin/thimbl b/bin/thimblr similarity index 68% rename from bin/thimbl rename to bin/thimblr index 4da6324..5f57c44 100755 --- a/bin/thimbl +++ b/bin/thimblr @@ -1,12 +1,12 @@ #!/usr/bin/env ruby # Use: -# thimbl setup ~/thimbl_plan ~/thimbl_cache 'user@thimblserver.com' 'thimblpass' -# thimbl follow 'dk' 'dk@telekommunisten.org' -# thimbl fetch -# thimbl print -# thimbl post "My first message :)" -# thimbl push +# thimblr setup 'user@thimblrserver.com' +# thimblr follow 'dk' 'dk@telekommunisten.org' +# thimblr fetch +# thimblr print +# thimblr post "My first message :)" +# thimblr push begin require 'thimbl' @@ -26,11 +26,13 @@ begin when 'print'; puts Thimbl::Command.print when 'fetch'; Thimbl::Command.fetch; puts "fecth completed" when 'post'; Thimbl::Command.post ARGV[1]; puts "post completed" - when 'push'; Thimbl::Command.push; puts "push completed" + when 'push'; Thimbl::Command.push ARGV[1]; puts "push completed" when 'follow'; Thimbl::Command.follow( ARGV[1], ARGV[2] ); puts "follow completed" else puts "command not valid '#{ARGV[0]}'" + exit 1 end rescue ArgumentError => e puts e.message + exit 1 end \ No newline at end of file diff --git a/lib/thimbl.rb b/lib/thimbl.rb index cff92e7..4f7c859 100644 --- a/lib/thimbl.rb +++ b/lib/thimbl.rb @@ -2,6 +2,8 @@ require 'json' require 'net/scp' require 'fileutils' +require 'tempfile' require "#{File.dirname(__FILE__)}/thimbl/base" require "#{File.dirname(__FILE__)}/thimbl/command" require "#{File.dirname(__FILE__)}/thimbl/finger" +require "#{File.dirname(__FILE__)}/thimbl/utils" diff --git a/lib/thimbl/base.rb b/lib/thimbl/base.rb index b46e85b..cb0cb94 100644 --- a/lib/thimbl/base.rb +++ b/lib/thimbl/base.rb @@ -7,56 +7,31 @@ # Use: # require 'rubygems' # require 'thimbl' -# thimbl = -# Thimbl::Base.new( -# 'plan_path' => '/tmp/plan', -# 'cache_path' => '/tmp/thimbl_cache', -# 'user' => 'fguillen@thimblserver.com', -# 'password' => 'my_thimblserver_password' +# thimbl = +# Thimbl::Base.new( +# 'bio' => 'my bio', +# 'website' => 'my website', +# 'mobile' => 'my mobile', +# 'email' => 'my email', +# 'address' => 'username@thimbl.net', +# 'name' => 'my name' # ) -# thimbl.setup( -# 'bio' => 'my bio', -# 'website' => 'my website', -# 'mobile' => 'my mobile', -# 'email' => 'my email', -# 'address' => 'my address', -# 'name' => 'my name' -# ) # thimbl.follow 'dk', 'dk@telekommunisten.org' # thimbl.fetch # thimbl.print # thimbl.post 'My first post' -# thimbl.push +# thimbl.push # module Thimbl class Base - attr_reader :plan_path, :cache_path, :data, :address, :user, :password + attr_accessor :data - # Initialize the thimbl client. - # - # Use: - # Thimbl.new( - # :plan_path => , - # :cache_path => - # :user => , - # :password => - # ) - # - def initialize( opts = {} ) - @plan_path = opts['plan_path'] - @cache_path = opts['cache_path'] - @user = opts['user'] - @password = opts['password'] - - @data = nil - @address = nil - end - - # Setup a new configuration, the execution of this method + # Initialize a new configuration, the execution of this method # will delete any thing in the `thimbl.plan_path` file and `thimbl.cache_path` file. # # Use: - # thimbl.setup( + # thimbl = + # Thimbl::Base.new( # :bio => 'bio', # :website => 'website', # :mobile => 'mobile', @@ -65,7 +40,10 @@ def initialize( opts = {} ) # :name => 'name' # ) # - def setup( opts = {} ) + # or just: + # + # thimbl = Thimbl::Base.new + def initialize( opts = {} ) opts = { 'bio' => 'bio', 'website' => 'website', @@ -92,70 +70,67 @@ def setup( opts = {} ) } } } - - @address = opts['address'] - - save_data end # Post a new message in your time-line. - # - # Use: - # thimbl.post - # - # To publish your comment you have to call: - # thimbl.push - # + # _This method doesn't push the modifications to de server._ def post( text ) message = { - 'address' => address, 'time' => Time.now.strftime('%Y%m%d%H%M%S'), 'text' => text } - data['plans'][address]['messages'] << message - save_data + data['plans'][me]['messages'] << message + end + + # Post a new message in your time-line and push the modifications to the server. + def post!( text, password ) + post + push password end - # Add a new user to follow - # - # Use: - # thimbl.follow 'nick', 'address' - # - # To publish your following users you have to call: - # thimbl.push - # + # Add a new user to your following + # _This method doesn't push the modifications to de server._ def follow( follow_nick, follow_address ) - data['plans'][address]['following'] << { 'nick' => follow_nick, 'address' => follow_address } - save_data + return if data['plans'][me]['following'].count { |e| e['address'] == follow_address } != 0 + data['plans'][me]['following'] << { 'nick' => follow_nick, 'address' => follow_address } + end + + # Add a new user to your following and push the modifications to the server. + def follow!( follow_nick, follow_address, password ) + follow follow_nick, follow_address + push password end # Fetch all the info and timelines of all the users you are following. - # - # Use: - # thimbl.fetch - # + # Even you, so any not pushed modification will be overwritten def fetch - following.map { |f| f['address'] }.each do |followed_address| - address_finger = Thimbl::Finger.run followed_address + following_and_me = following.map { |f| f['address'] } << me + following_and_me.uniq.each do |address| + address_finger = Thimbl::Finger.run address + next if address_finger.nil? || address_finger.match(/Plan:\s*(.*)/m).nil? address_plan = address_finger.match(/Plan:\s*(.*)/m)[1].gsub("\\\n",'') - data['plans'][followed_address] = JSON.load( address_plan ) + data['plans'][address] = JSON.load( address_plan ) end + end - save_data + # Send your actual `plan` file to your server + # It requires the password of your thimbl user + def push( password ) + tmp_path = Thimbl::Utils.to_file plan.to_json + Net::SCP.start( me.split('@')[1], me.split('@')[0], :password => password ) do |scp| + scp.upload!( tmp_path, ".plan" ) + end end # Print every message of you and all the users you are following. # - # Use: - # thimbl.print # The method doesn't print anything by it self. It just returns an string # with all the comments. - # def print result = "" messages.each do |message| - result += Thimbl::Base.parse_time( message['time'] ).strftime( '%Y-%m-%d %H:%M:%S' ) + result += Thimbl::Utils.parse_time( message['time'] ).strftime( '%Y-%m-%d %H:%M:%S' ) result += " #{message['address']}" result += " > #{message['text']}" result += "\n" @@ -164,61 +139,31 @@ def print return result end - # Send your actual `plan` file to your server - # - # Use: - # thimbl.push - # - def push - Net::SCP.start( user.split('@')[1], user.split('@')[0], :password => password ) do |scp| - scp.upload!( plan_path, ".plan" ) + # Returns all the messages of you and all the users you are following + # in a chronologic order into a json format. + def messages + _messages = [] + data['plans'].each_pair do |k,v| + _messages += v['messages'].map { |m| m['address'] = k; m } end + _messages = _messages.sort { |a,b| a['time'].to_s <=> b['time'].to_s } + + return _messages end - # Charge into the `thimbl` object all the data into your `cache` file. - # Use: - # thimbl.load_data - # - def load_data - @data = JSON.load( File.read cache_path ) - @address = data['me'] - end - - # Save all the data into the `thimbl` objecto into your `cache` file and - # `plan` file. - # - # Use: - # thimbl.save_data - # - def save_data - File.open( cache_path, 'w' ) { |f| f.write data.to_json } - File.open( plan_path, 'w' ) { |f| f.write data['plans'][address].to_json } + # Returns the actual thimbl user account + def me + data['me'] end - + # Returns all the info about the users you are following. - # - # Use: - # thimbl.following - # def following - data['plans'][address]['following'] + data['plans'][me]['following'] end - - # Returns all the messages of you and all the users you are following - # in a chronologic order into a json format. - # - # Use: - # thimbl.messages - # - def messages - _messages = data['plans'].values.map { |e| e['messages'] }.flatten - _messages = _messages.sort { |a,b| a['time'] <=> b['time'] } - return _messages - end - - def self.parse_time( time ) - Time.utc( time[0,4], time[4,2], time[6,2], time[8,2], time[10,2], time[12,2] ) + # Returns the actual plan + def plan + data['plans'][me] end end end \ No newline at end of file diff --git a/lib/thimbl/command.rb b/lib/thimbl/command.rb index 9c8daaf..873ac50 100644 --- a/lib/thimbl/command.rb +++ b/lib/thimbl/command.rb @@ -1,32 +1,35 @@ module Thimbl class Command - CONFIG_FILE = File.expand_path( "~#{ENV['USER']}/.thimbl/thimbl.cnf" ) + CACHE_PATH = File.expand_path( "~#{ENV['USER']}/.thimbl/cache.json" ) def self.setup( *args ) - if( args.size != 4 ) - raise ArgumentError, "use: $ thimbl setup " + if( args.size != 1 ) + raise ArgumentError, "use: $ thimbl setup " end - FileUtils.mkdir_p File.dirname( config_file ) - - File.open( config_file, 'w' ) do |f| - f.write( { :plan_path => args[0], :cache_path => args[1], :user => args[2], :password => args[3] }.to_json ) - end + thimbl = Thimbl::Base.new( 'address' => args[0] ) + save_cache thimbl.data + end - thimbl = Thimbl::Command.load - thimbl.setup( 'address' => args[2] ) + def self.save_cache data + if !File.exists? File.dirname( cache_path ) + FileUtils.mkdir_p File.dirname( cache_path ) + end + + File.open( cache_path, 'w' ) do |f| + f.write( data.to_json ) + end end - + def self.print thimbl = Thimbl::Command.load - thimbl.load_data return thimbl.print end def self.fetch thimbl = Thimbl::Command.load - thimbl.load_data thimbl.fetch + save_cache thimbl.data end def self.post( text ) @@ -34,14 +37,16 @@ def self.post( text ) raise ArgumentError, "use: $ thimbl post " end thimbl = Thimbl::Command.load - thimbl.load_data thimbl.post text + save_cache thimbl.data end - - def self.push + + def self.push( password ) + if( password.nil? || password.empty? ) + raise ArgumentError, "use: $ thimbl push " + end thimbl = Thimbl::Command.load - thimbl.load_data - thimbl.push + thimbl.push password end def self.follow( nick, address ) @@ -49,23 +54,23 @@ def self.follow( nick, address ) raise ArgumentError, "use: $ thimbl follow
" end thimbl = Thimbl::Command.load - thimbl.load_data thimbl.follow nick, address + save_cache thimbl.data end def self.load - if( !File.exists? config_file ) - raise ArgumentError, "Thimbl need to setup, use: $ thimbl setup " + if( !File.exists? cache_path ) + raise ArgumentError, "Thimbl need to setup, use: $ thimbl setup " end - config = JSON.load( File.read config_file ) - thimbl = Thimbl::Base.new( config ) + thimbl = Thimbl::Base.new + thimbl.data = JSON.load File.read cache_path return thimbl end - def self.config_file - CONFIG_FILE + def self.cache_path + CACHE_PATH end end end \ No newline at end of file diff --git a/lib/thimbl/finger.rb b/lib/thimbl/finger.rb index fa10ac6..f9252bd 100644 --- a/lib/thimbl/finger.rb +++ b/lib/thimbl/finger.rb @@ -1,7 +1,6 @@ module Thimbl class Finger # Wrapper for `finger` system call - # def self.run( *args ) %x[`whereis finger` #{args.join(' ')}] end diff --git a/lib/thimbl/utils.rb b/lib/thimbl/utils.rb new file mode 100644 index 0000000..1b088c9 --- /dev/null +++ b/lib/thimbl/utils.rb @@ -0,0 +1,14 @@ +module Thimbl + class Utils + def self.to_file( text ) + file = Tempfile.new('plan.json') + file.write( text ) + file.close + file.path + end + + def self.parse_time( time ) + Time.utc( time[0,4], time[4,2], time[6,2], time[8,2], time[10,2], time[12,2] ) + end + end +end \ No newline at end of file diff --git a/test/thimbl_base_test.rb b/test/thimbl_base_test.rb index d564c89..facc357 100644 --- a/test/thimbl_base_test.rb +++ b/test/thimbl_base_test.rb @@ -24,29 +24,12 @@ def setup def teardown end - def test_new - thimbl = - Thimbl::Base.new( - 'plan_path' => 'plan_path', - 'cache_path' => 'cache_path', - 'user' => 'user', - 'password' => 'password' - ) - - assert_equal( 'plan_path', thimbl.plan_path ) - assert_equal( 'cache_path', thimbl.cache_path ) - assert_equal( 'user', thimbl.user ) - assert_equal( 'password', thimbl.password ) - end - - def test_setup_without_options + def test_new_without_options thimbl = Thimbl::Base.new - thimbl.expects(:save_data) - thimbl.setup - assert_equal( 'address', thimbl.address ) + assert_equal( 'address', thimbl.me ) - data = thimbl.data['plans'][thimbl.address] + data = thimbl.data['plans'][thimbl.me] assert_equal( 'name', data['name'] ) assert_equal( [], data['messages'] ) assert_equal( 'bio', data['bio'] ) @@ -57,21 +40,20 @@ def test_setup_without_options assert_equal( 'email', data['properties']['email'] ) end - def test_setup_with_options - thimbl = Thimbl::Base.new - thimbl.expects(:save_data) - thimbl.setup( - 'bio' => 'my bio', - 'website' => 'my website', - 'mobile' => 'my mobile', - 'email' => 'my email', - 'address' => 'my address', - 'name' => 'my name' - ) - - assert_equal( 'my address', thimbl.address ) - - data = thimbl.data['plans'][thimbl.address] + def test_new_with_options + thimbl = + Thimbl::Base.new( + 'bio' => 'my bio', + 'website' => 'my website', + 'mobile' => 'my mobile', + 'email' => 'my email', + 'address' => 'my address', + 'name' => 'my name' + ) + + assert_equal( 'my address', thimbl.me ) + + data = thimbl.data['plans'][thimbl.me] assert_equal( 'my name', data['name'] ) assert_equal( 'my bio', data['bio'] ) assert_equal( 'my mobile', data['properties']['mobile'] ) @@ -80,31 +62,23 @@ def test_setup_with_options end def test_post - Thimbl::Base.any_instance.expects(:save_data) thimbl = Thimbl::Base.new - thimbl.stubs( :address ).returns( @data['me'] ) - thimbl.stubs( :data ).returns( @data ) - Delorean.time_travel_to("2011-02-03 04:05:06") do thimbl.post( "wadus wadus" ) end - message = thimbl.data['plans'][thimbl.address]['messages'].last - assert_equal( thimbl.address, message['address'] ) + message = thimbl.data['plans'][thimbl.me]['messages'].last assert_equal( '20110203040506', message['time'] ) assert_equal( 'wadus wadus', message['text'] ) end def test_follow - Thimbl::Base.any_instance.expects(:save_data) thimbl = Thimbl::Base.new - thimbl.stubs( :address ).returns( @data['me'] ) - thimbl.stubs( :data ).returns( @data ) thimbl.follow( 'nick', 'address' ) - following = thimbl.data['plans'][thimbl.address]['following'].last + following = thimbl.data['plans'][thimbl.me]['following'].last assert_equal( 'nick', following['nick'] ) assert_equal( 'address', following['address'] ) end @@ -125,10 +99,13 @@ def test_fetch returns( finger_fixture ). in_sequence( finger_sequence ) - Thimbl::Base.any_instance.expects(:save_data) - - thimbl = Thimbl::Base.new - thimbl.stubs(:data).returns( @data ) + Thimbl::Finger. + expects(:run). + with( 'me@thimbl.net' ). + returns( finger_fixture ). + in_sequence( finger_sequence ) + + thimbl = Thimbl::Base.new( 'address' => 'me@thimbl.net' ) thimbl.expects(:following).returns( [ {'nick' => 'wadus1', 'address' => 'wadus1@telekommunisten.org' }, {'nick' => 'wadus2', 'address' => 'wadus2@telekommunisten.org' }, @@ -142,40 +119,24 @@ def test_fetch def test_fetch_with_plan_with_two_break_lines finger_fixture = File.read "#{File.dirname(__FILE__)}/fixtures/finger_dk_telekommunisten_org_two_break_lines.txt" - Thimbl::Finger. - expects(:run). - with( 'wadus1@telekommunisten.org' ). - returns( finger_fixture ) - - Thimbl::Base.any_instance.expects(:save_data) - - thimbl = Thimbl::Base.new - thimbl.stubs(:data).returns( @data ) - thimbl.expects(:following).returns( [{'nick' => 'wadus1', 'address' => 'wadus1@telekommunisten.org' }] ) + Thimbl::Finger.expects(:run).with( 'me@thimbl.net' ).returns( finger_fixture ) + thimbl = Thimbl::Base.new( 'address' => 'me@thimbl.net' ) thimbl.fetch - assert_equal( 22, thimbl.data['plans']['wadus1@telekommunisten.org']['messages'].count ) + assert_equal( 22, thimbl.data['plans']['me@thimbl.net']['messages'].count ) end def test_push - thimbl = Thimbl::Base.new( 'user' => 'user@domain', 'password' => 'my password' ) - Net::SCP.expects(:start).with( 'domain', 'user', :password => 'my password' ) - thimbl.push + thimbl = Thimbl::Base.new( 'address' => 'user@domain.com' ) + Net::SCP.expects(:start).with( 'domain.com', 'user', :password => 'my password' ) + thimbl.push 'my password' end - - def test_load_data - thimbl = Thimbl::Base.new( 'cache_path' => "#{File.dirname(__FILE__)}/fixtures/cache.json" ) - thimbl.load_data - assert_equal( 'fguillen@telekommunisten.org', thimbl.data['me'] ) - assert_equal( 2, thimbl.data['plans'].size ) - assert_equal( 4, thimbl.data['plans']['fguillen@telekommunisten.org']['messages'].size ) - end - def test_messages - thimbl = Thimbl::Base.new( 'cache_path' => "#{File.dirname(__FILE__)}/fixtures/cache.json" ) - thimbl.load_data + thimbl = Thimbl::Base.new + thimbl.data = JSON.load File.read "#{File.dirname(__FILE__)}/fixtures/cache.json" + assert_equal( 24, thimbl.messages.size ) end @@ -202,19 +163,16 @@ def test_print assert_equal( "2011-01-31 00:22:02 fguillen@telekommunisten.org > Here I am\n", print.lines.to_a[0] ) assert_equal( "2011-02-05 15:06:37 fguillen@telekommunisten.org > testing :)\n", print.lines.to_a[1] ) end - - def test_save_data - thimbl = Thimbl::Base.new( 'plan_path' => '/tmp/plan_path', 'cache_path' => '/tmp/cache_path' ) - thimbl.stubs( :address ).returns( @data['me'] ) - thimbl.stubs( :data ).returns( @data ) - thimbl.data - thimbl.save_data - - assert_equal( thimbl.data['plans'][thimbl.address], JSON.load( File.read '/tmp/plan_path' ) ) - assert_equal( thimbl.data, JSON.load( File.read '/tmp/cache_path' ) ) + + def test_parse_time + assert_equal( Time.utc( 2010, 11, 29, 6, 3, 35 ), Thimbl::Utils.parse_time( '20101129060335' ) ) end - def test_parse_time - assert_equal( Time.utc( 2010, 11, 29, 6, 3, 35 ), Thimbl::Base.parse_time( '20101129060335' ) ) + def test_not_adding_a_following_if_already_there + thimbl = Thimbl::Base.new + thimbl.follow 'wadus', 'wadus@thimbl.net' + thimbl.follow 'wadus', 'wadus@thimbl.net' + + assert_equal( 1, thimbl.following.size ) end end \ No newline at end of file diff --git a/test/thimbl_command_test.rb b/test/thimbl_command_test.rb index ce1eb66..e404920 100644 --- a/test/thimbl_command_test.rb +++ b/test/thimbl_command_test.rb @@ -14,26 +14,19 @@ def test_setup_should_exit_if_parameters_not_correct end def test_setup - Thimbl::Command.stubs( :config_file ).returns( '/tmp/.thimbl/config_file' ) - Thimbl::Command.expects( :load ).returns( Thimbl::Base.new ) - Thimbl::Base.any_instance.expects( :setup ) - - Thimbl::Command.setup( 'plan_path', 'cache_path', 'user', 'password' ) + temp_file = Tempfile.new('cache.json') + Thimbl::Command.stubs( :cache_path ).returns( temp_file.path ) + Thimbl::Command.setup( 'me@thimbl.net' ) - config_written = JSON.load( File.read( '/tmp/.thimbl/config_file' ) ) - - assert_equal( 'plan_path', config_written['plan_path'] ) - assert_equal( 'cache_path', config_written['cache_path'] ) - assert_equal( 'user', config_written['user'] ) - assert_equal( 'password', config_written['password'] ) + cache_written = JSON.load temp_file.read + assert_equal 'me@thimbl.net', cache_written['me'] - FileUtils.remove_dir '/tmp/.thimbl' + temp_file.unlink end def test_print thimbl = Thimbl::Base.new Thimbl::Command.expects( :load ).returns( thimbl ) - thimbl.expects( :load_data ) thimbl.expects( :print ) Thimbl::Command.print @@ -42,7 +35,6 @@ def test_print def test_fetch thimbl = Thimbl::Base.new Thimbl::Command.expects( :load ).returns( thimbl ) - thimbl.expects( :load_data ) thimbl.expects( :fetch ) Thimbl::Command.fetch @@ -57,7 +49,6 @@ def test_post_should_raise_error_if_wrong_parameters def test_post thimbl = Thimbl::Base.new Thimbl::Command.expects( :load ).returns( thimbl ) - thimbl.expects( :load_data ) thimbl.expects( :post ).with( 'wadus' ) Thimbl::Command.post 'wadus' @@ -66,10 +57,9 @@ def test_post def test_push thimbl = Thimbl::Base.new Thimbl::Command.expects( :load ).returns( thimbl ) - thimbl.expects( :load_data ) - thimbl.expects( :push ) + thimbl.expects( :push ).with( 'pass' ) - Thimbl::Command.push + Thimbl::Command.push 'pass' end def test_follow_should_raise_error_if_wrong_parameters @@ -81,14 +71,13 @@ def test_follow_should_raise_error_if_wrong_parameters def test_follow thimbl = Thimbl::Base.new Thimbl::Command.expects( :load ).returns( thimbl ) - thimbl.expects( :load_data ) thimbl.expects( :follow ).with( 'wadus', 'wadus@domain.com' ) Thimbl::Command.follow 'wadus', 'wadus@domain.com' end def test_load_should_raise_error_if_not_config_file - Thimbl::Command.stubs( :config_file ).returns( '/tmp/not_exists_file' ) + Thimbl::Command.stubs( :cache_path ).returns( '/tmp/not_exists_file' ) assert_raise( ArgumentError ) do Thimbl::Command.load @@ -96,11 +85,16 @@ def test_load_should_raise_error_if_not_config_file end def test_load - Thimbl::Command.stubs( :config_file ).returns( '/tmp/config_file' ) - File.expects( :exists? ).with( '/tmp/config_file' ).returns( true ) - File.expects( :read ).with( '/tmp/config_file' ).returns( "{ \"a\" : 1 }" ) - Thimbl::Base.expects( :new ).with( { 'a' => 1 } ) + temp_file = Tempfile.new 'cache.json' + temp_file.write "{ \"a\" : 1 }" + temp_file.close + + Thimbl::Command.stubs( :cache_path ).returns( temp_file.path ) - Thimbl::Command.load + thimbl = Thimbl::Command.load + + assert_equal 1, thimbl.data['a'] + + temp_file.unlink end end \ No newline at end of file