esbit 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rspec
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bradly Feeley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Esbit
2
+
3
+ A lightweight gem to interact with the Campfire API that doesn't depend on EventMachine or a Twitter gem.
4
+
5
+ There are currently only a few API calls implemented based on what I needed, but please feel free to send a pull request with any you find usefull.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'esbit'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install esbit
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'esbit'
25
+ campfire = Esbit::Campfire.new('your_campfire_subdomain', 'your_campfire_token')
26
+ campfire.rooms
27
+ room = campfire.rooms.find_room_by_name('My Room')
28
+ room.say 'Hello, World!'
29
+ ```
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/esbit.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'esbit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "esbit"
8
+ spec.version = Esbit::VERSION
9
+ spec.authors = ["Bradly Feeley"]
10
+ spec.email = ["bradlyf@gmail.com"]
11
+ spec.description = %q{A lightweight gem to interact with the Campfire API}
12
+ spec.summary = %q{A lightweight gem to interact with the Campfire API}
13
+ spec.homepage = "https://github.com/bradly/esbit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client", "~> 1.6"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
data/lib/esbit.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "json"
2
+ require "rest-client"
3
+ require "esbit/version"
4
+ require "esbit/campfire"
5
+ require "esbit/connection"
6
+ require "esbit/room"
7
+
8
+ module Esbit
9
+ end
@@ -0,0 +1,29 @@
1
+ module Esbit
2
+ class Campfire
3
+ attr_reader :subdomain, :token, :connection
4
+
5
+ def initialize(subdomain, token)
6
+ @subdomain = subdomain
7
+ @token = token
8
+ @connection = Esbit::Connection.new(self)
9
+ end
10
+
11
+ def rooms
12
+ @rooms ||= @connection.get('rooms')['rooms'].collect { |room_json|
13
+ Room.new(room_json, @connection)
14
+ }
15
+ end
16
+
17
+ def room(room_id)
18
+ @connection.get 'rooms', id: room_id
19
+ end
20
+
21
+ def find_room_by_id(room_id)
22
+ self.rooms.find { |room| room.id == room_id }
23
+ end
24
+
25
+ def find_room_by_name(room_name)
26
+ self.rooms.find { |room| room.name == room_name }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ module Esbit
2
+ class Connection
3
+ def initialize(campfire)
4
+ @campfire = campfire
5
+ end
6
+
7
+ def get(path, params = {})
8
+ make_request do
9
+ RestClient.get request_url(path), params
10
+ end
11
+ end
12
+
13
+ def post(path, params = {})
14
+ make_request do
15
+ RestClient.post request_url(path), params
16
+ end
17
+ end
18
+
19
+ private
20
+ def make_request
21
+ raw_response = yield
22
+ handle_response(raw_response)
23
+ end
24
+
25
+ def handle_response(raw_response)
26
+ JSON.parse raw_response
27
+ end
28
+
29
+ def base_url
30
+ "https://#{@campfire.token}@#{@campfire.subdomain}.campfirenow.com"
31
+ end
32
+
33
+ def request_url(path)
34
+ "#{URI.join(base_url, path)}.json"
35
+ end
36
+ end
37
+ end
data/lib/esbit/room.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Esbit
2
+ class Room
3
+ attr_reader :id, :name, :connection
4
+
5
+ def initialize(room_json, connection)
6
+ @id = room_json["id"]
7
+ @name = room_json["name"]
8
+ @connection = connection
9
+ end
10
+
11
+ def say(message)
12
+ json_message = {message: {type: 'TextMessage', body: message}}
13
+ @connection.post "/room/#{@id}/speak", json_message(message)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Esbit
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Esbit::Campfire do
4
+ let(:campfire) { Esbit::Campfire.new('subdomain', 'token') }
5
+
6
+ before(:each) do
7
+ RestClient.stub(:get).and_return(MOCK_ROOMS_API_RESPONSE)
8
+ end
9
+
10
+ describe "initialization" do
11
+ it "should accept a subdomain and token" do
12
+ campfire.subdomain.should eql 'subdomain'
13
+ campfire.token.should eql 'token'
14
+ end
15
+
16
+ it "should setup a connection" do
17
+ campfire.connection.should be_a Esbit::Connection
18
+ end
19
+ end
20
+
21
+ describe "rooms" do
22
+ it "should return an array of rooms" do
23
+ campfire.rooms.all? { |room| room.is_a? Esbit::Room }.should be_true
24
+ end
25
+
26
+ it "should only make one request" do
27
+ RestClient.should_receive(:get).once
28
+ 3.times { campfire.rooms }
29
+ end
30
+ end
31
+
32
+ describe "finding rooms" do
33
+ it "should find a room by id" do
34
+ campfire.find_room_by_id(1).name.should eql "Chat Room 1"
35
+ end
36
+
37
+ it "should find a room by name" do
38
+ campfire.find_room_by_name("Chat Room 2").id.should eql 2
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Esbit::Connection do
4
+ let(:campfire) { Esbit::Campfire.new('subdomain', 'token') }
5
+ let(:connection) { Esbit::Connection.new(campfire) }
6
+
7
+ describe "urls" do
8
+ it "should set a base_url with token and subdomain" do
9
+ connection.send(:base_url).should start_with('https://token@subdomain.')
10
+ end
11
+
12
+ it "should construct a request url" do
13
+ connection.send(:request_url, 'rooms').should eql 'https://token@subdomain.campfirenow.com/rooms.json'
14
+ end
15
+ end
16
+
17
+ describe "get" do
18
+ it "handle response after the request is made" do
19
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
20
+ json_string = '{"test":"json"}'
21
+ RestClient.stub('get').and_return(json_string)
22
+ connection.should_receive(:handle_response).with(json_string)
23
+ connection.get('rooms')
24
+ end
25
+
26
+ it "should make a request to a path" do
27
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
28
+ RestClient.should_receive('get').with(url, {}).and_return('{}')
29
+ connection.get('rooms')
30
+ end
31
+
32
+ it "should allow optional parameters" do
33
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
34
+ RestClient.should_receive('get').with(url, {some_param: 42}).and_return('{}')
35
+ connection.get('rooms', some_param: 42)
36
+ end
37
+ end
38
+
39
+ describe "post" do
40
+ it "should make post a request to a path" do
41
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
42
+ RestClient.should_receive('post').with(url, {}).and_return('{}')
43
+ connection.post('rooms')
44
+ end
45
+
46
+ it "should allow optional parameters" do
47
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
48
+ RestClient.should_receive('post').with(url, {some_param: 42}).and_return('{}')
49
+ connection.post('rooms', some_param: 42)
50
+ end
51
+
52
+ it "handle response after the request is made" do
53
+ url = 'https://token@subdomain.campfirenow.com/rooms.json'
54
+ json_string = '{"test":"json"}'
55
+ RestClient.stub('post').and_return(json_string)
56
+ connection.should_receive(:handle_response).with(json_string)
57
+ connection.post('rooms')
58
+ end
59
+ end
60
+
61
+ describe "handle_response" do
62
+ it "should parse json" do
63
+ json_string = '{"test":"json"}'
64
+ connection.send(:handle_response, json_string).should eql({"test" => "json"})
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,10 @@
1
+ require 'esbit'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ config.order = 'random'
8
+ end
9
+
10
+ MOCK_ROOMS_API_RESPONSE = "{\"rooms\":[{\"updated_at\":\"2012/10/09 23:57:53 +0000\",\"topic\":\"Chat Room 1\",\"name\":\"Chat Room 1\",\"membership_limit\":25,\"created_at\":\"2012/01/26 06:25:58 +0000\",\"locked\":false,\"id\":1},{\"updated_at\":\"2012/06/13 13:25:52 +0000\",\"topic\":\"Chat Room 2\",\"name\":\"Chat Room 2\",\"membership_limit\":25,\"created_at\":\"2012/01/26 06:40:14 +0000\",\"locked\":false,\"id\":2}]}"
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bradly Feeley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A lightweight gem to interact with the Campfire API
79
+ email:
80
+ - bradlyf@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - esbit.gemspec
91
+ - lib/esbit.rb
92
+ - lib/esbit/campfire.rb
93
+ - lib/esbit/connection.rb
94
+ - lib/esbit/room.rb
95
+ - lib/esbit/version.rb
96
+ - spec/esbit/campfire_spec.rb
97
+ - spec/esbit/connection_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/bradly/esbit
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: A lightweight gem to interact with the Campfire API
124
+ test_files:
125
+ - spec/esbit/campfire_spec.rb
126
+ - spec/esbit/connection_spec.rb
127
+ - spec/spec_helper.rb
128
+ has_rdoc: