kindling 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/kindling.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kindling}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wal McConnell"]
12
- s.date = %q{2009-12-21}
12
+ s.date = %q{2009-12-24}
13
13
  s.description = %q{Ruby wrapper for the 37 Signals Campfire API}
14
14
  s.email = %q{wal@contrast.ie}
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "lib/kindling/campfire.rb",
27
27
  "lib/kindling/request.rb",
28
28
  "test/fixtures/rooms.json",
29
+ "test/kindling/campfire_test.rb",
29
30
  "test/kindling/request_test.rb",
30
31
  "test/test_helper.rb"
31
32
  ]
@@ -35,7 +36,8 @@ Gem::Specification.new do |s|
35
36
  s.rubygems_version = %q{1.3.5}
36
37
  s.summary = %q{Ruby wrapper for the 37 Signals Campfire API}
37
38
  s.test_files = [
38
- "test/kindling/request_test.rb",
39
+ "test/kindling/campfire_test.rb",
40
+ "test/kindling/request_test.rb",
39
41
  "test/test_helper.rb"
40
42
  ]
41
43
 
@@ -2,26 +2,43 @@ module Kindling
2
2
 
3
3
  class Campfire
4
4
 
5
- attr_accessor :api_token, :subdomain, :room_id, :ssl
5
+ attr_accessor :api_token, :subdomain, :ssl
6
6
 
7
- def initialize(api_token, subdomain, room_id, ssl=false)
7
+ def initialize(api_token, subdomain, ssl=false)
8
8
  @api_token = api_token
9
- @subdomain = subdomain
10
- @room_id = room_id
9
+ @subdomain = subdomain
11
10
  @ssl = ssl
12
- rooms
13
11
  end
14
12
 
15
13
  def rooms
16
14
  Kindling::Request.rooms(self)
17
15
  end
18
16
 
19
- def speak(message)
20
- Kindling::Request.speak(message, self)
17
+ def speak(room, message)
18
+ if is_campfire_id?(room)
19
+ Kindling::Request.speak(message, self, room)
20
+ else
21
+ Kindling::Request.speak(message, self, map_room_name(room))
22
+ end
21
23
  end
22
24
 
23
25
  def credentials
24
26
  {:username => @api_token, :password => 'x'}
25
27
  end
28
+
29
+ private
30
+
31
+ def is_campfire_id?(s)
32
+ s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
33
+ end
34
+
35
+ def map_room_name(name)
36
+ rms = rooms.select{|r| r["name"] == name}
37
+ unless rms.empty?
38
+ rms[0]["id"]
39
+ else
40
+ raise InvalidRoomName.new(name)
41
+ end
42
+ end
26
43
  end
27
44
  end
@@ -16,17 +16,17 @@ class Kindling::Request
16
16
  when 404
17
17
  raise Kindling::InvalidSubdomain.new(campfire.subdomain)
18
18
  when 401
19
- raise Kindling::InvalidAPIToken.new(campfire.api_token)
19
+ raise Kindling::InvalidAPIToken.new(campfire.api_token)
20
20
  when 200
21
21
  return response["rooms"]
22
22
  end
23
23
  end
24
24
 
25
- def self.speak(message, campfire)
26
- response = post room_url(campfire), :body => {:message => {:body => message, :type => "Textmessage"}}.to_json, :basic_auth => campfire.credentials
25
+ def self.speak(message, campfire, room_id)
26
+ response = post(room_url(campfire, room_id), :body => {:message => {:body => message, :type => "Textmessage"}}.to_json, :basic_auth => campfire.credentials)
27
27
  case response.code
28
28
  when 404
29
- raise Kindling::InvalidRoomID.new(campfire.room_id)
29
+ raise Kindling::InvalidRoomID.new(room_id)
30
30
  when 401
31
31
  raise Kindling::InvalidAPIToken.new(campfire.api_token)
32
32
  when 200..201
@@ -38,8 +38,8 @@ class Kindling::Request
38
38
 
39
39
  private
40
40
 
41
- def self.room_url(campfire)
42
- "#{subdomain(campfire)}/room/#{campfire.room_id}/speak.json"
41
+ def self.room_url(campfire, room_id)
42
+ "#{subdomain(campfire)}/room/#{room_id}/speak.json"
43
43
  end
44
44
 
45
45
  def self.rooms_url(campfire)
data/lib/kindling.rb CHANGED
@@ -3,6 +3,7 @@ module Kindling
3
3
  class InvalidSubdomain < StandardError; end
4
4
  class InvalidAPIToken < StandardError; end
5
5
  class InvalidRoomID < StandardError; end
6
+ class InvalidRoomName < StandardError; end
6
7
  end
7
8
 
8
9
  ['campfire', 'request'].each do |file|
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class CampfireTest < Test::Unit::TestCase
4
+ context "campfire" do
5
+ context "with room name" do
6
+ setup do
7
+ @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN)
8
+ end
9
+
10
+ should "send message if correct name" do
11
+ Kindling::Request.expects(:rooms).returns(JSON.parse(fixture_file("rooms.json"))["rooms"])
12
+ Kindling::Request.expects(:speak).with("test message", @campfire, TEST_ROOM_ID)
13
+ @campfire.speak(TEST_ROOM_NAME, "test message")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,12 +1,12 @@
1
1
  require 'test_helper'
2
2
  require 'json'
3
3
 
4
+
4
5
  class RequestTest < Test::Unit::TestCase
5
6
 
6
- context "Request" do
7
+ context "Request no SSL" do
7
8
  setup do
8
- stub_rooms_request({:code => 200, :[] => JSON.parse(fixture_file("rooms.json"))["rooms"]})
9
- @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, TEST_ROOM_ID)
9
+ @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, false)
10
10
  end
11
11
 
12
12
  context "rooms" do
@@ -32,29 +32,44 @@ class RequestTest < Test::Unit::TestCase
32
32
  end
33
33
 
34
34
  context "send message" do
35
- should "send message successfully if 200" do
36
- stub_send_message_request('test message', {:code => 200})
37
- Kindling::Request.speak("test message", @campfire)
38
- end
39
-
40
- should "send message successfully if 201" do
41
- stub_send_message_request('test message', {:code => 201})
42
- Kindling::Request.speak("test message", @campfire)
43
- end
35
+
36
+ context "with room id" do
37
+ should "send message successfully if 20x" do
38
+ [200, 201].each do |code|
39
+ stub_send_message_request('test message', {:code => code})
40
+ Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID)
41
+ end
42
+ end
44
43
 
45
- should "raise error if invalid room id" do
46
- stub_send_message_request('test message', {:code => 404})
47
- assert_raises Kindling::InvalidRoomID do
48
- Kindling::Request.speak("test message", @campfire)
44
+ should "raise error if invalid room id" do
45
+ stub_send_message_request('test message', {:code => 404})
46
+ assert_raises Kindling::InvalidRoomID do
47
+ Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID)
48
+ end
49
49
  end
50
- end
51
50
 
52
- should "raise error if invalid API token" do
53
- stub_send_message_request('test message', {:code => 401})
54
- assert_raises Kindling::InvalidAPIToken do
55
- Kindling::Request.speak("test message", @campfire)
51
+ should "raise error if invalid API token" do
52
+ stub_send_message_request('test message', {:code => 401})
53
+ assert_raises Kindling::InvalidAPIToken do
54
+ Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID)
55
+ end
56
56
  end
57
57
  end
58
58
  end
59
+ end
60
+
61
+ context "Request SSL" do
62
+ setup do
63
+ @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, true)
64
+ end
65
+
66
+ context "with room id" do
67
+ should "send message successfully if 20x" do
68
+ [200, 201].each do |code|
69
+ stub_send_message_request('test message', {:code => code}, true)
70
+ Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID)
71
+ end
72
+ end
73
+ end
59
74
  end
60
- end
75
+ end
data/test/test_helper.rb CHANGED
@@ -12,17 +12,21 @@ end
12
12
 
13
13
  TEST_SUBDOMAIN = 'testsubdomain'
14
14
  TEST_API_TOKEN = '787654567654323456789098765432345678765432'
15
- TEST_ROOM_ID = '201712'
15
+ TEST_ROOM_ID = 206877
16
+ TEST_ROOM_NAME = 'Room 3'
16
17
 
17
18
  def stub_rooms_request(resp)
18
19
  response = mock('Net::HTTPResponse', resp)
19
20
  Kindling::Request.expects(:get).with("http://#{TEST_SUBDOMAIN}.campfirenow.com/rooms.json", {:basic_auth => {:password => 'x', :username => "#{TEST_API_TOKEN}"}}).returns(response)
20
21
  end
21
22
 
22
- def stub_send_message_request(message, resp)
23
+ def stub_send_message_request(message, resp, ssl = false)
23
24
  response = mock('Net::HTTPResponse', resp)
24
25
  Kindling::Request.expects(:post).with(
25
- "http://#{TEST_SUBDOMAIN}.campfirenow.com/room/#{TEST_ROOM_ID}/speak.json",
26
+ "#{ssl ? 'https' : 'http'}://#{TEST_SUBDOMAIN}.campfirenow.com/room/#{TEST_ROOM_ID}/speak.json",
26
27
  :body => {:message => {:body => message, :type => "Textmessage"}}.to_json,
27
28
  :basic_auth => {:password => 'x', :username => "#{TEST_API_TOKEN}"}).returns(response)
28
29
  end
30
+
31
+
32
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wal McConnell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-21 00:00:00 +00:00
12
+ date: 2009-12-24 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,6 +32,7 @@ files:
32
32
  - lib/kindling/campfire.rb
33
33
  - lib/kindling/request.rb
34
34
  - test/fixtures/rooms.json
35
+ - test/kindling/campfire_test.rb
35
36
  - test/kindling/request_test.rb
36
37
  - test/test_helper.rb
37
38
  has_rdoc: true
@@ -63,5 +64,6 @@ signing_key:
63
64
  specification_version: 3
64
65
  summary: Ruby wrapper for the 37 Signals Campfire API
65
66
  test_files:
67
+ - test/kindling/campfire_test.rb
66
68
  - test/kindling/request_test.rb
67
69
  - test/test_helper.rb