Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Renaming lib to ChatGram. Add a Campfire service.
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Apr 24, 2011
1 parent 65c2c20 commit c69853c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 44 deletions.
25 changes: 10 additions & 15 deletions instagram_campfire_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@
Sequel.connect(url) :
Sequel.sqlite

%w(service).each do |lib|
require File.expand_path("../lib/chat_gram/#{lib}", __FILE__)
end

class InstagramCampfireHookApp < Sinatra::Base
set :instagram_lat => ENV['INSTAGRAM_LAT'],
:instagram_lng => ENV['INSTAGRAM_LNG'],
:instagram_client => nil

set :campfire_http do
url = "https://#{ENV['CAMPFIRE_DOMAIN']}.campfirenow.com/room"
conn = Faraday.new url do |b|
b.request :yajl
b.adapter :excon
end
conn.basic_auth(ENV['CAMPFIRE_TOKEN'], 'X')
conn
end

set :campfire_room => ENV["CAMPFIRE_ROOM"]
:instagram_client => nil,
:service => ChatGram::Service::Campfire.new(
:domain => ENV['CAMPFIRE_DOMAIN'],
:token => ENV['CAMPFIRE_TOKEN'],
:room => ENV['CAMPFIRE_ROOM'])

before do
@instagram = settings.instagram_client || Instagram.client
Expand Down Expand Up @@ -101,8 +97,7 @@ class InstagramCampfireHookApp < Sinatra::Base

helpers do
def speak(text)
settings.campfire_http.post("#{settings.campfire_room}/speak.json",
:message => {:body => text})
settings.service.speak(text)
end

def image_text(img)
Expand Down
45 changes: 45 additions & 0 deletions lib/chat_gram/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module ChatGram
# Defines the simple API that all chat services should honor.
class Service
def initialize(options = {})
raise NotImplementedError
end

def speak(text)
raise NotImplementedError
end

require 'faraday'

# This is a class that knows how to speak in a Campfire channel.
class Campfire
# Sets up an HTTP client with the following Campfire options.
#
# options - Hash of options:
# domain - The String subdomain of the Campfire account.
# room - The String Campfire Room ID.
# token - The String token for the Campfire account.
#
# Returns nothing.
def initialize(options = {})
url = "https://%s.campfirenow.com/room/%s" % [
options[:domain],
options[:room]]
@conn = Faraday.new url do |b|
b.request :yajl
b.adapter :excon
end
@conn.basic_auth options[:token], 'X'
end

# Posts the given message to the Campfire room.
#
# text - The String message to be sent to the chat room.
#
# Returns nothing.
def speak(text)
@conn.post 'speak.json', :message => {:body => text}
end
end
end
end
61 changes: 32 additions & 29 deletions test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
c.adapter = :test
end

class InstagramCampfireHookApp
set :environment, :test
set :campfire_http do
obj = Object.new
def obj.post(room, hash)
$messages << [room.chomp("/speak.json"), hash[:message][:body]]
end
obj
end

set :instagram_client, Instagram.client
end

class Instagram::API
attr_accessor :stubs
def connection(raw = false)
Expand Down Expand Up @@ -62,6 +49,22 @@ def connection(raw = false)
DB[:users].insert :username => 'user!'

class InstagramCampfireHookTest < Test::Unit::TestCase
class TestService
attr_reader :messages
def initialize
@messages = []
end

def speak(text)
@messages << text
end
end

InstagramCampfireHookApp.set \
:environment => :test,
:instagram_client => Instagram.client,
:service => (@@service = TestService.new)

include Rack::Test::Methods

def test_receives_webhook
Expand All @@ -71,9 +74,9 @@ def test_receives_webhook
:object_id => '1234', :changed_aspect => 'media'}]

post '/image', nil, :input => Yajl.dump(events)
assert_equal ['photo', 'image!'], $messages.pop
assert_equal ['photo', 'caption! at location! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!'], $messages.pop
assert_nil $messages.pop
assert_equal 'image!', @@service.messages.pop
assert_equal 'caption! at location! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!', @@service.messages.pop
assert_nil @@service.messages.pop
end

def test_receives_webhook_without_caption
Expand All @@ -85,9 +88,9 @@ def test_receives_webhook_without_caption
:object_id => '1234', :changed_aspect => 'media'}]

post '/image', nil, :input => Yajl.dump(events)
assert_equal ['photo', 'image!'], $messages.pop
assert_equal ['photo', 'location! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!'], $messages.pop
assert_nil $messages.pop
assert_equal 'image!', @@service.messages.pop
assert_equal 'location! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!', @@service.messages.pop
assert_nil @@service.messages.pop
end

def test_receives_webhook_without_location
Expand All @@ -99,9 +102,9 @@ def test_receives_webhook_without_location
:object_id => '1234', :changed_aspect => 'media'}]

post '/image', nil, :input => Yajl.dump(events)
assert_equal ['photo', 'image!'], $messages.pop
assert_equal ['photo', 'caption! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!'], $messages.pop
assert_nil $messages.pop
assert_equal 'image!', @@service.messages.pop
assert_equal 'caption! by user! on Thu, Feb 03, 2011 @ 05:18 AM link!', @@service.messages.pop
assert_nil @@service.messages.pop
end

def test_receives_webhook_without_location_or_caption
Expand All @@ -113,9 +116,9 @@ def test_receives_webhook_without_location_or_caption
:object_id => '1234', :changed_aspect => 'media'}]

post '/image', nil, :input => Yajl.dump(events)
assert_equal ['photo', 'image!'], $messages.pop
assert_equal ['photo', 'by user! on Thu, Feb 03, 2011 @ 05:18 AM link!'], $messages.pop
assert_nil $messages.pop
assert_equal 'image!', @@service.messages.pop
assert_equal 'by user! on Thu, Feb 03, 2011 @ 05:18 AM link!', @@service.messages.pop
assert_nil @@service.messages.pop
end

def test_receives_webhook_with_recent_time
Expand All @@ -129,9 +132,9 @@ def test_receives_webhook_with_recent_time
:object_id => '1234', :changed_aspect => 'media'}]

post '/image', nil, :input => Yajl.dump(events)
assert_equal ['photo', 'image!'], $messages.pop
assert_equal ['photo', 'by user! @ 09:18 PM link!'], $messages.pop
assert_nil $messages.pop
assert_equal 'image!', @@service.messages.pop
assert_equal 'by user! @ 09:18 PM link!', @@service.messages.pop
assert_nil @@service.messages.pop
end

def test_searches_by_location
Expand All @@ -149,7 +152,7 @@ def test_responds_to_challenge
end

def setup
$messages = []
@@service.messages.clear
@instagram = InstagramCampfireHookApp.settings.instagram_client
@instagram.stubs = Faraday::Adapter::Test::Stubs.new
end
Expand Down

0 comments on commit c69853c

Please sign in to comment.