tinder 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.txt CHANGED
@@ -1,4 +1,8 @@
1
- 0.1.3
1
+ 0.1.4 - 2007-07-23
2
+ * Support for transcripts
3
+ * Fixed Room#leave, which was broken by a Campfire deployment [Andy Smith]
4
+
5
+ 0.1.3 - 2007-02-12
2
6
  * added ssl support [Tero Parviainen]
3
7
 
4
8
  0.1.2 - 2007-01-27
data/Manifest.txt CHANGED
@@ -7,6 +7,3 @@ lib/tinder.rb
7
7
  lib/tinder/campfire.rb
8
8
  lib/tinder/room.rb
9
9
  lib/tinder/version.rb
10
- test/remote/remote_campfire_test.rb
11
- test/test_helper.rb
12
- test/unit/campfire_test.rb
data/lib/tinder.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  require 'rubygems'
3
2
  require 'active_support'
4
3
  require 'uri'
@@ -9,3 +8,7 @@ require 'hpricot'
9
8
 
10
9
 
11
10
  Dir[File.join(File.dirname(__FILE__), 'tinder/**/*.rb')].sort.each { |lib| require lib }
11
+
12
+ module Tinder
13
+ class Error < StandardError; end
14
+ end
@@ -11,7 +11,7 @@ module Tinder
11
11
  attr_reader :subdomain, :uri
12
12
 
13
13
  # Create a new connection to the campfire account with the given +subdomain+.
14
- # There's an optional +:ssl+ option to use SSL for the connection.
14
+ # There's an +:ssl+ option to use SSL for the connection.
15
15
  #
16
16
  # c = Tinder::Campfire.new("mysubdomain", :ssl => true)
17
17
  def initialize(subdomain, options = {})
@@ -23,11 +23,15 @@ module Tinder
23
23
 
24
24
  # Log in to campfire using your +email+ and +password+
25
25
  def login(email, password)
26
- @logged_in = verify_response(post("login", :email_address => email, :password => password), :redirect_to => url_for)
26
+ unless verify_response(post("login", :email_address => email, :password => password), :redirect_to => url_for(:only_path => false))
27
+ raise Error, "Campfire login failed"
28
+ end
29
+ @logged_in = true
27
30
  end
28
31
 
32
+ # Returns true when successfully logged in
29
33
  def logged_in?
30
- @logged_in
34
+ @logged_in === true
31
35
  end
32
36
 
33
37
  def logout
@@ -35,16 +39,27 @@ module Tinder
35
39
  @logged_in = !result
36
40
  end
37
41
  end
42
+
43
+ # Get an array of all the available rooms
44
+ # TODO: detect rooms that are full (no link)
45
+ def rooms
46
+ Hpricot(get.body).search("//h2/a").collect do |a|
47
+ Room.new(self, room_id_from_url(a.attributes['href']), a.inner_html)
48
+ end
49
+ end
38
50
 
51
+ # Find a campfire room by name
52
+ def find_room_by_name(name)
53
+ rooms.detect {|room| room.name == name }
54
+ end
55
+
39
56
  # Creates and returns a new Room with the given +name+ and optionally a +topic+
40
57
  def create_room(name, topic = nil)
41
58
  find_room_by_name(name) if verify_response(post("account/create/room?from=lobby", {:room => {:name => name, :topic => topic}}, :ajax => true), :success)
42
59
  end
43
60
 
44
- # Find a campfire room by name
45
- def find_room_by_name(name)
46
- link = Hpricot(get.body).search("//h2/a").detect { |a| a.inner_html == name }
47
- link.blank? ? nil : Room.new(self, link.attributes['href'].scan(/room\/(\d*)$/).to_s, name)
61
+ def find_or_create_room_by_name(name)
62
+ find_room_by_name(name) || create_room(name)
48
63
  end
49
64
 
50
65
  # List the users that are currently chatting in any room
@@ -56,7 +71,23 @@ module Tinder
56
71
  end
57
72
  users.flatten.compact.uniq.sort
58
73
  end
59
-
74
+
75
+ # Get the dates of the available transcripts by room
76
+ #
77
+ # campfire.available_transcripts
78
+ # #=> {"15840" => [#<Date: 4908311/2,0,2299161>, #<Date: 4908285/2,0,2299161>]}
79
+ #
80
+ def available_transcripts(room = nil)
81
+ url = "files%2Btranscripts"
82
+ url += "?room_id#{room}" if room
83
+ transcripts = (Hpricot(get(url).body) / ".transcript").inject({}) do |result,transcript|
84
+ link = (transcript / "a").first.attributes['href']
85
+ (result[room_id_from_url(link)] ||= []) << Date.parse(link.scan(/\/transcript\/(\d{4}\/\d{2}\/\d{2})/).to_s)
86
+ result
87
+ end
88
+ room ? transcripts[room.to_s] : transcripts
89
+ end
90
+
60
91
  # Deprecated: only included for backwards compatability
61
92
  def host #:nodoc:
62
93
  uri.host
@@ -68,9 +99,15 @@ module Tinder
68
99
  end
69
100
 
70
101
  private
102
+
103
+ def room_id_from_url(url)
104
+ url.scan(/room\/(\d*)/).to_s
105
+ end
71
106
 
72
- def url_for(path = "")
73
- "#{uri}/#{path}"
107
+ def url_for(*args)
108
+ options = {:only_path => true}.merge(args.last.is_a?(Hash) ? args.pop : {})
109
+ path = args.shift
110
+ "#{options[:only_path] ? '' : uri}/#{path}"
74
111
  end
75
112
 
76
113
  def post(path, data = {}, options = {})
@@ -88,6 +125,7 @@ module Tinder
88
125
 
89
126
  def prepare_request(request, options = {})
90
127
  returning request do
128
+ request.add_field 'User-Agent', "Tinder/#{Tinder::VERSION::STRING} (http://tinder.rubyforge.org)"
91
129
  request.add_field 'Cookie', @cookie if @cookie
92
130
  if options[:ajax]
93
131
  request.add_field 'X-Requested-With', 'XMLHttpRequest'
@@ -106,7 +144,7 @@ module Tinder
106
144
  end
107
145
  end
108
146
 
109
- # flatten a nested hash
147
+ # flatten a nested hash (:room => {:name => 'foobar'} to 'user[name]' => 'foobar')
110
148
  def flatten(params)
111
149
  params = params.dup
112
150
  params.stringify_keys!.each do |k,v|
@@ -131,6 +169,6 @@ module Tinder
131
169
  false
132
170
  end
133
171
  end
134
-
172
+
135
173
  end
136
174
  end
data/lib/tinder/room.rb CHANGED
@@ -12,31 +12,37 @@ module Tinder
12
12
  # Join the room. Pass +true+ to join even if you've already joined.
13
13
  def join(force = false)
14
14
  @room = returning(get("room/#{id}")) do |room|
15
- return false unless verify_response(room, :success)
15
+ raise Error, "Could not join room" unless verify_response(room, :success)
16
16
  @membership_key = room.body.scan(/\"membershipKey\": \"([a-z0-9]+)\"/).to_s
17
17
  @user_id = room.body.scan(/\"userID\": (\d+)/).to_s
18
18
  @last_cache_id = room.body.scan(/\"lastCacheID\": (\d+)/).to_s
19
19
  @timestamp = room.body.scan(/\"timestamp\": (\d+)/).to_s
20
- end unless @room || force
20
+ end if @room.nil? || force
21
21
  true
22
22
  end
23
23
 
24
24
  # Leave a room
25
25
  def leave
26
- returning verify_response(get("room/#{id}/leave"), :redirect) do
26
+ returning verify_response(post("room/#{id}/leave"), :redirect) do
27
27
  @room, @membership_key, @user_id, @last_cache_id, @timestamp = nil
28
28
  end
29
29
  end
30
30
 
31
31
  # Toggle guest access on or off
32
32
  def toggle_guest_access
33
- verify_response(post("room/#{id}/toggle_guest_access"), :success)
33
+ # re-join the room to get the guest url
34
+ verify_response(post("room/#{id}/toggle_guest_access"), :success) && join(true)
34
35
  end
35
36
 
36
37
  # Get the url for guest access
37
38
  def guest_url
38
39
  join
39
- (Hpricot(@room.body)/"#guest_access h4").first.inner_html
40
+ link = (Hpricot(@room.body)/"#guest_access h4").first
41
+ link.inner_html if link
42
+ end
43
+
44
+ def guest_access_enabled?
45
+ !guest_url.nil?
40
46
  end
41
47
 
42
48
  # The invite code use for guest
@@ -136,6 +142,28 @@ module Tinder
136
142
  end
137
143
  messages
138
144
  end
145
+
146
+ # Get the dates for the available transcripts for this room
147
+ def available_transcripts
148
+ @campfire.available_transcripts(id)
149
+ end
150
+
151
+ # Get the transcript for the given date (Returns a hash in the same format as #listen)
152
+ #
153
+ # room.transcript(room.available_transcripts.first)
154
+ # #=> [{:message=>"foobar!", :user_id=>"99999", :person=>"Brandon", :id=>"18659245"}]
155
+ #
156
+ def transcript(date)
157
+ (Hpricot(get("room/#{id}/transcript/#{date.to_date.strftime('%Y/%m/%d')}").body) / ".message").collect do |message|
158
+ person = (message / '.person span').first
159
+ body = (message / '.body div').first
160
+ {:id => message.attributes['id'].scan(/message_(\d+)/).to_s,
161
+ :person => person ? person.inner_html : nil,
162
+ :user_id => message.attributes['class'].scan(/user_(\d+)/).to_s,
163
+ :message => body ? body.inner_html : nil
164
+ }
165
+ end
166
+ end
139
167
 
140
168
  private
141
169
 
@@ -2,7 +2,7 @@ module Tinder #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 3
5
+ TINY = 4
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -3,24 +3,34 @@ require File.dirname(__FILE__) + '/../test_helper'
3
3
  class RemoteCampfireTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
- @campfire = Tinder::Campfire.new 'opensoul'
7
- @user, @pass = 'brandon@opensoul.org', 'testing'
6
+ @campfire = Tinder::Campfire.new 'domain'
7
+ #@user, @pass = 'email@example.com', 'password'
8
+ raise "Set your campfire credentials before running the remote tests" unless @user && @pass
8
9
  end
9
10
 
10
11
  def test_create_and_delete_room
11
12
  assert login
12
- room = @campfire.create_room('Testing123')
13
+ assert @campfire.logged_in?
13
14
 
14
- assert Tinder::Room, room
15
+ room = @campfire.create_room("Testing#{Time.now.to_i}")
16
+
17
+ assert_instance_of Tinder::Room, room
15
18
  assert_not_nil room.id
16
- assert_equal "new name", room.rename("new name")
19
+
20
+ room.name = "new name"
21
+ assert_equal "new name", room.name
17
22
 
18
23
  room.destroy
19
- assert_nil @campfire.find_room_by_name('Testing123')
24
+ assert_nil @campfire.find_room_by_name(room.name)
25
+
26
+ assert @campfire.logout
27
+ ensure
28
+ room.destroy rescue nil
20
29
  end
21
30
 
22
31
  def test_failed_login
23
32
  assert !@campfire.login(@user, 'notmypassword')
33
+ assert !@campfire.logged_in?
24
34
  end
25
35
 
26
36
  def test_find_nonexistent_room
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RoomTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @campfire = Tinder::Campfire.new("foobar")
7
+ @room = Tinder::Room.new(@campfire, 1, "Room 1")
8
+ end
9
+
10
+ def test_toggle_guest_access_rejoins_room
11
+ @response = mock("response")
12
+ @response.expects(:code).returns("200")
13
+ @campfire.expects(:post).with('room/1/toggle_guest_access').returns(@response)
14
+ @campfire.expects(:get).with('room/1').returns(@response)
15
+ @room.toggle_guest_access
16
+ end
17
+
18
+ def test_guest_access?
19
+ mock_response :guest_access_enabled
20
+ assert @room.guest_access_enabled?
21
+ end
22
+
23
+ private
24
+
25
+ def mock_response(response)
26
+ FakeWeb.register_uri('http://foobar.campfirenow.com/room/1',
27
+ :file => "#{FIXTURE_DIR}/pages/room/#{response}.html")
28
+ end
29
+
30
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: tinder
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.3
7
- date: 2007-02-12 00:00:00 -05:00
6
+ version: 0.1.4
7
+ date: 2007-07-23 00:00:00 +03:00
8
8
  summary: An (unofficial) Campfire API
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Brandon Keepers
30
31
  files:
@@ -37,16 +38,16 @@ files:
37
38
  - lib/tinder/campfire.rb
38
39
  - lib/tinder/room.rb
39
40
  - lib/tinder/version.rb
40
- - test/remote/remote_campfire_test.rb
41
- - test/test_helper.rb
42
- - test/unit/campfire_test.rb
43
41
  test_files:
44
42
  - test/remote/remote_campfire_test.rb
45
- - test/unit/campfire_test.rb
46
- rdoc_options: []
47
-
48
- extra_rdoc_files: []
49
-
43
+ - test/unit/room_test.rb
44
+ rdoc_options:
45
+ - --main
46
+ - README.txt
47
+ extra_rdoc_files:
48
+ - CHANGELOG.txt
49
+ - Manifest.txt
50
+ - README.txt
50
51
  executables: []
51
52
 
52
53
  extensions: []
@@ -79,5 +80,5 @@ dependencies:
79
80
  requirements:
80
81
  - - ">="
81
82
  - !ruby/object:Gem::Version
82
- version: 1.1.7
83
+ version: 1.2.1
83
84
  version:
data/test/test_helper.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- require 'active_support'
4
- require 'breakpoint'
5
- require File.dirname(__FILE__) + '/../lib/tinder.rb'
6
- require 'mocha'
7
- require 'stubba'
@@ -1,61 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- class CampfireTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @campfire = Tinder::Campfire.new("foobar")
7
- @response = mock("response")
8
- end
9
-
10
- def test_verify_response_redirect_true
11
- @response.expects(:code).returns(302)
12
- assert true === @campfire.send(:verify_response, @response, :redirect)
13
- end
14
-
15
- def test_verify_response_redirect_false
16
- @response.expects(:code).returns(200)
17
- assert false === @campfire.send(:verify_response, @response, :redirect)
18
- end
19
-
20
- def test_verify_response_success
21
- @response.expects(:code).returns(200)
22
- assert true === @campfire.send(:verify_response, @response, :success)
23
- end
24
-
25
- def test_verify_response_redirect_to
26
- @response.expects(:code).returns(304)
27
- @response.expects(:[]).with('location').returns("/foobar")
28
- assert true === @campfire.send(:verify_response, @response, :redirect_to => '/foobar')
29
- end
30
-
31
- def test_verify_response_redirect_to_without_redirect
32
- @response.expects(:code).returns(200)
33
- assert false === @campfire.send(:verify_response, @response, :redirect_to => '/foobar')
34
- end
35
-
36
- def test_verify_response_redirect_to_wrong_path
37
- @response.expects(:code).returns(302)
38
- @response.expects(:[]).with('location').returns("/baz")
39
- assert false === @campfire.send(:verify_response, @response, :redirect_to => '/foobar')
40
- end
41
-
42
- def test_prepare_request_returns_request
43
- request = Net::HTTP::Get.new("does_not_matter")
44
- assert_equal request, @campfire.send(:prepare_request, request)
45
- end
46
-
47
- def test_prepare_request_sets_cookie
48
- request = Net::HTTP::Get.new("does_not_matter")
49
- @campfire.instance_variable_set("@cookie", "foobar")
50
- assert_equal "foobar", @campfire.send(:prepare_request, request)['Cookie']
51
- end
52
-
53
- def test_perform_request
54
- response = mock("response")
55
- Net::HTTP.any_instance.stubs(:request).returns(response)
56
- request = Net::HTTP::Get.new("does_not_matter")
57
- response.expects(:[]).with('set-cookie')
58
-
59
- assert_equal response, @campfire.send(:perform_request) { request }
60
- end
61
- end