├── .document ├── .gitignore ├── .rspec ├── .ruby-gemset ├── .ruby-version ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── hipchat-api.gemspec ├── lib ├── hipchat-api.rb └── hipchat-api │ └── version.rb └── spec ├── fakeweb ├── rooms_create_response.json ├── rooms_delete_response.json ├── rooms_history_response.json ├── rooms_list_response.json ├── rooms_message_response.json ├── rooms_show_response.json ├── rooms_topic_response.json ├── users_create_response.json ├── users_delete_response.json ├── users_list_include_deleted_response.json ├── users_list_response.json ├── users_show_response.json ├── users_undelete_response.json └── users_update_response.json ├── hipchat-api_spec.rb └── spec_helper.rb /.document: -------------------------------------------------------------------------------- 1 | lib/**/*.rb 2 | bin/* 3 | - 4 | features/**/*.feature 5 | LICENSE.txt 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | rdoc 3 | doc 4 | .yardoc 5 | .bundle 6 | pkg 7 | Gemfile.lock 8 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --colour 2 | --format doc 3 | --order random -------------------------------------------------------------------------------- /.ruby-gemset: -------------------------------------------------------------------------------- 1 | hipchat-api_gem -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 1.9.3 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | rvm: 2 | - 1.9.3 3 | gemfile: 4 | - Gemfile 5 | env: 6 | - MOCHA_OPTIONS=skip_integration -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 1.0.6 (2014-03-09) 4 | 5 | * Added `include_deleted` parameter to `users_list` method [#7](https://github.com/czarneckid/hipchat-api/pull/7) 6 | 7 | ## 1.0.5 (2012-12-04) 8 | 9 | * Added `rooms_topic` method to set a room's topic. 10 | * Added `users_undelete` method to undelete a user. 11 | 12 | ## 1.0.4 (2012-07-16) 13 | 14 | * Change reject! to reject to fix create user issue. Fixes #2. Thanks @bradleybuda. 15 | 16 | ## 1.0.3 (2012-07-13) 17 | 18 | * Add support to the new message_format param when publishing messages to a room. Thanks @rtopitt. 19 | 20 | ## 1.0.2 (2011-09-09) 21 | 22 | * Added missing HipChat API methods: `rooms_create`, `rooms_delete` 23 | 24 | ## 1.0.1 (2011-05-09) 25 | 26 | * Set the default HIPCHAT_API_URL to use https 27 | 28 | ## 1.0.0 (2011-05-07) 29 | 30 | * Initial release 31 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2014 David Czarnecki 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hipchat-api 2 | 3 | [![Project Status](http://stillmaintained.com/czarneckid/hipchat-api.png)](http://stillmaintained.com/czarneckid/hipchat-api) [![Build Status](https://travis-ci.org/czarneckid/hipchat-api.png)](https://travis-ci.org/czarneckid/hipchat-api) 4 | 5 | Ruby gem for interacting with HipChat API 6 | 7 | * https://www.hipchat.com/docs/api 8 | 9 | ## Compatibility 10 | 11 | hipchat-API has been tested under Ruby 1.9.3 12 | 13 | ## Requirements 14 | 15 | * HTTParty 16 | 17 | ## Install 18 | 19 | ``` 20 | gem install hipchat-api 21 | ``` 22 | 23 | ## Example 24 | 25 | ```ruby 26 | require 'hipchat-api' 27 | => true 28 | hipchat_api = HipChat::API.new('api_token') 29 | => # 30 | ``` 31 | 32 | ## API methods 33 | 34 | * Room-related methods 35 | 36 | ```ruby 37 | rooms_create(name, owner_user_id, privacy = 'public', topic = '', guest_access = 0) 38 | rooms_delete(room_id) 39 | rooms_list 40 | rooms_history(room_id, date, timezone) 41 | rooms_message(room_id, from, message, notify = 0, color = 'yellow', message_format = 'html') 42 | rooms_topic(toom_id, topic, from = 'API') 43 | rooms_show(room_id) 44 | ``` 45 | 46 | * User-related methods 47 | 48 | ```ruby 49 | users_list(include_deleted = 0) 50 | users_create(email, name, title, is_group_admin = 0, password = nil, timezone = 'UTC') 51 | users_delete(user_id) 52 | users_show(user_id) 53 | users_undelete(user_id) 54 | users_update(user_id, email = nil, name = nil, title = nil, is_group_admin = nil, password = nil, timezone = nil) 55 | ``` 56 | 57 | ## Contributing to hipchat-api 58 | 59 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet 60 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it 61 | * Fork the project 62 | * Start a feature/bugfix branch 63 | * Commit and push until you are happy with your contribution 64 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 65 | * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. 66 | 67 | ## Copyright 68 | 69 | Copyright (c) 2011-2014 David Czarnecki. See LICENSE.txt for further details. 70 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'rspec/core' 5 | require 'rspec/core/rake_task' 6 | RSpec::Core::RakeTask.new(:spec) do |spec| 7 | spec.pattern = FileList['spec/**/*_spec.rb'] 8 | spec.rspec_opts = ['--backtrace'] 9 | # spec.ruby_opts = ['-w'] 10 | end 11 | 12 | task :default => :spec 13 | -------------------------------------------------------------------------------- /hipchat-api.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | 4 | require 'hipchat-api/version' 5 | 6 | Gem::Specification.new do |s| 7 | s.name = "hipchat-api" 8 | s.version = HipChat::API::VERSION.dup 9 | s.authors = ["David Czarnecki"] 10 | s.email = ["czarneckid@acm.org"] 11 | s.homepage = "http://github.com/czarneckid/hipchat-api" 12 | s.summary = %q{Ruby gem for interacting with HipChat} 13 | s.description = %q{Ruby gem for interacting with HipChat} 14 | 15 | s.rubyforge_project = "hipchat-api" 16 | 17 | s.files = `git ls-files`.split("\n") 18 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 19 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 20 | s.require_paths = ["lib"] 21 | 22 | s.add_dependency('httparty') 23 | 24 | s.add_development_dependency('fakeweb') 25 | s.add_development_dependency('mocha', '~> 0.11.4') 26 | s.add_development_dependency('rspec') 27 | s.add_development_dependency('rake') 28 | end 29 | -------------------------------------------------------------------------------- /lib/hipchat-api.rb: -------------------------------------------------------------------------------- 1 | require 'httparty' 2 | require 'hipchat-api/version' 3 | 4 | module HipChat 5 | class API 6 | include HTTParty 7 | 8 | # Default timeout of 3 seconds 9 | DEFAULT_TIMEOUT = 3 10 | 11 | # Default headers for HTTP requests 12 | DEFAULT_HEADERS = { 13 | 'User-Agent' => "HipChat gem #{VERSION}", 14 | } 15 | 16 | # HipChat API URL 17 | HIPCHAT_API_URL = 'https://api.hipchat.com/v1' 18 | 19 | headers(DEFAULT_HEADERS) 20 | default_timeout(DEFAULT_TIMEOUT) 21 | format(:json) 22 | 23 | # HipChat API URL 24 | attr_accessor :hipchat_api_url 25 | 26 | # HipChat access token 27 | attr_accessor :token 28 | 29 | # Create a new instance of the +HipChat::API+ for interacting with HipChat 30 | # 31 | # @param token [String] HipChat access token 32 | # @param hipchat_api_url [String] HipChat API URL 33 | def initialize(token, hipchat_api_url = HIPCHAT_API_URL) 34 | @token = token 35 | @hipchat_api_url = hipchat_api_url 36 | end 37 | 38 | # Turn on HTTParty debugging 39 | # 40 | # @param location [Object] Output "sink" for HTTP debugging 41 | def debug(location = $stderr) 42 | self.class.debug_output(location) 43 | end 44 | 45 | # Set new HTTP headers for requests 46 | # 47 | # @param http_headers [Hash] HTTP headers 48 | def set_http_headers(http_headers = {}) 49 | http_headers.merge!(DEFAULT_HEADERS) 50 | self.class.headers(http_headers) 51 | end 52 | 53 | # Set new HTTP timeout for requests 54 | # 55 | # @param timeout [int] Timeout in seconds 56 | def set_timeout(timeout) 57 | self.class.default_timeout(timeout) 58 | end 59 | 60 | # Creates a new room. 61 | # 62 | # @param name [String] Name of the room. 63 | # @param owner_user_id [int] User ID of the room's owner. 64 | # @param privacy [String, 'public'] Privacy setting for room. 65 | # @param topic [String, ''] Room topic. 66 | # @param guest_access [int, 0] Whether or not to enable guest access for this room. 0 = false, 1 = true. (default: 0). 67 | # 68 | # @see https://www.hipchat.com/docs/api/method/rooms/create 69 | def rooms_create(name, owner_user_id, privacy = 'public', topic = '', guest_access = 0) 70 | self.class.post(hipchat_api_url_for('rooms/create'), :body => {:auth_token => @token, :name => name, :owner_user_id => owner_user_id, 71 | :topic => topic, :privacy => privacy, :guest_access => guest_access}) 72 | end 73 | 74 | # Deletes a room and kicks the current participants. 75 | # 76 | # @param room_id [int] ID of the room 77 | # 78 | # @see https://www.hipchat.com/docs/api/method/rooms/delete 79 | def rooms_delete(room_id) 80 | self.class.post(hipchat_api_url_for('rooms/delete'), :body => {:auth_token => @token, :room_id => room_id}) 81 | end 82 | 83 | # Fetch chat history for this room. 84 | # 85 | # @param room_id [int] ID of the room. 86 | # @param date [String] Either the date to fetch history for in YYYY-MM-DD format, or "recent" to fetch the latest 50 messages. 87 | # @param timezone [String] Your timezone. Must be a supported PHP timezone. (default: UTC) 88 | # 89 | # @see https://www.hipchat.com/docs/api/method/rooms/history 90 | def rooms_history(room_id, date, timezone) 91 | self.class.get(hipchat_api_url_for('rooms/history'), :query => {:auth_token => @token, :room_id => room_id, :date => date, 92 | :timezone => timezone}) 93 | end 94 | 95 | # List rooms for this group. 96 | # 97 | # @see https://www.hipchat.com/docs/api/method/rooms/list 98 | def rooms_list 99 | self.class.get(hipchat_api_url_for('rooms/list'), :query => {:auth_token => @token}) 100 | end 101 | 102 | # Send a message to a room. 103 | # 104 | # @param room_id [int] ID of the room. 105 | # @param from [String] Name the message will appear be sent from. Must be less than 15 characters long. May contain letters, numbers, -, _, and spaces. 106 | # @param message [String] The message body. Must be valid XHTML. HTML entities must be escaped (e.g.: & instead of &). May contain basic tags: a, b, i, strong, em, br, img, pre, code. 5000 characters max. 107 | # @param notify [int] Boolean flag of whether or not this message should trigger a notification for people in the room (based on their individual notification preferences). 0 = false, 1 = true. (default: 0) 108 | # @param color [String] Background color for message. One of "yellow", "red", "green", "purple", or "random". (default: yellow) 109 | # @param message_format [String] Determines how the message is treated by HipChat's server and rendered inside HipChat applications. One of "html" or "text". (default: html) 110 | # @see https://www.hipchat.com/docs/api/method/rooms/message 111 | def rooms_message(room_id, from, message, notify = 0, color = 'yellow', message_format = 'html') 112 | self.class.post(hipchat_api_url_for('rooms/message'), :body => {:auth_token => @token, :room_id => room_id, :from => from, 113 | :message => message, :notify => notify, :color => color, :message_format => message_format}) 114 | end 115 | 116 | # Set a room's topic. Useful for displaying statistics, important links, server status, you name it! 117 | # 118 | # @param room_id [int] ID of the room. 119 | # @param topic [String] The topic body. 250 characters max. 120 | # @param from [String, 'API'] Name of the service changing the topic. (default: API). 121 | # 122 | # @see https://www.hipchat.com/docs/api/method/rooms/topic 123 | def rooms_topic(room_id, topic, from = 'API') 124 | self.class.post(hipchat_api_url_for('rooms/topic'), :body => {:auth_token => @token, :room_id => room_id, :topic => topic, :from => from}) 125 | end 126 | 127 | # Get room details. 128 | # 129 | # @param room_id [int] ID of the room. 130 | # 131 | # @see https://www.hipchat.com/docs/api/method/rooms/show 132 | def rooms_show(room_id) 133 | self.class.get(hipchat_api_url_for('rooms/show'), :query => {:auth_token => @token, :room_id => room_id}) 134 | end 135 | 136 | # Create a new user in your group. 137 | # 138 | # @param email [String] User's email. 139 | # @param name [String] User's full name. 140 | # @param title [String] User's title. 141 | # @param is_group_admin [int] Whether or not this user is an admin. 0 = false, 1 = true. (default: 0) 142 | # @param password [String, nil] User's password. If not provided, a randomly generated password will be returned. 143 | # @param timezone [String, 'UTC'] User's timezone. Must be a PHP supported timezone. (default: UTC) 144 | # 145 | # @see https://www.hipchat.com/docs/api/method/users/create 146 | def users_create(email, name, title, is_group_admin = 0, password = nil, timezone = 'UTC') 147 | self.class.post(hipchat_api_url_for('users/create'), :body => {:auth_token => @token, :email => email, :name => name, :title => title, 148 | :is_group_admin => is_group_admin, :password => password, :timezone => timezone}.reject{|key, value| value.nil?}) 149 | end 150 | 151 | # Delete a user. 152 | # 153 | # @param user_id [String] ID or email address of the user. 154 | # 155 | # @see https://www.hipchat.com/docs/api/method/users/delete 156 | def users_delete(user_id) 157 | self.class.post(hipchat_api_url_for('users/delete'), :body => {:auth_token => @token, :user_id => user_id}) 158 | end 159 | 160 | # List all users in the group. 161 | # 162 | # @param notify [bool] Boolean flag of whether or not include deleted users 0 = false, 1 = true. (default: 0) 163 | # 164 | # @see https://www.hipchat.com/docs/api/method/users/list 165 | def users_list(include_deleted = 0) 166 | query = {:auth_token => @token} 167 | query[:include_deleted] = 1 if include_deleted == 1 168 | self.class.get(hipchat_api_url_for('users/list'), :query => query) 169 | end 170 | 171 | # Get a user's details. 172 | # 173 | # @param user_id [String] ID or email address of the user. 174 | # 175 | # @see https://www.hipchat.com/docs/api/method/users/show 176 | def users_show(user_id) 177 | self.class.get(hipchat_api_url_for('users/show'), :query => {:auth_token => @token, :user_id => user_id}) 178 | end 179 | 180 | # Undelete a user. They will be sent an email requiring them to click a link to reactivate the account. 181 | # 182 | # @param user_id [String] ID or email address of the user. 183 | # 184 | # @see https://www.hipchat.com/docs/api/method/users/undelete 185 | def users_undelete(user_id) 186 | self.class.post(hipchat_api_url_for('users/undelete'), :body => {:auth_token => @token, :user_id => user_id}) 187 | end 188 | 189 | # Update a user. 190 | # 191 | # @param email [String] User's email. 192 | # @param name [String] User's full name. 193 | # @param title [String] User's title. 194 | # @param is_group_admin [int] Whether or not this user is an admin. 0 = false, 1 = true. (default: 0) 195 | # @param password [String, nil] User's password. 196 | # @param timezone [String, nil] User's timezone. Must be a PHP supported timezone. (default: UTC) 197 | # 198 | # @see https://www.hipchat.com/docs/api/method/users/update 199 | def users_update(user_id, email = nil, name = nil, title = nil, is_group_admin = nil, password = nil, timezone = nil) 200 | self.class.post(hipchat_api_url_for('users/update'), :body => {:auth_token => @token, :user_id => user_id, :email => email, 201 | :name => name, :title => title, :is_group_admin => is_group_admin, :password => password, :timezone => timezone}.reject{|key, value| value.nil?}) 202 | end 203 | 204 | private 205 | 206 | # Create a URL appropriate for accessing the HipChat API with a given API method. 207 | # 208 | # @param method [String] HipChat API method 209 | # 210 | # @return URL appropriate for accessing the HipChat API with a given API method. 211 | def hipchat_api_url_for(method) 212 | "#{@hipchat_api_url}/#{method}" 213 | end 214 | end 215 | end 216 | -------------------------------------------------------------------------------- /lib/hipchat-api/version.rb: -------------------------------------------------------------------------------- 1 | module HipChat 2 | class API 3 | VERSION = '1.0.6'.freeze 4 | end 5 | end -------------------------------------------------------------------------------- /spec/fakeweb/rooms_create_response.json: -------------------------------------------------------------------------------- 1 | { "room": { "room_id": 5, "name": "Development", "topic": "", "last_active": 0, "created": 1269010311, "owner_user_id": 5, "participants": [ ], "guest_access_url": null } } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_delete_response.json: -------------------------------------------------------------------------------- 1 | { "deleted": true } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_history_response.json: -------------------------------------------------------------------------------- 1 | { "messages" : [ { "date" : "2010-11-19T15:48:19-0800", 2 | "from" : { "name" : "Garret Heaton", 3 | "user_id" : 10 4 | }, 5 | "message" : "Good morning! This is a regular message." 6 | }, 7 | { "date" : "2010-11-19T15:49:44-0800", 8 | "file" : { "name" : "Screenshot.png", 9 | "size" : 141909, 10 | "url" : "http://uploads.hipchat.com/xxx/Screenshot.png" 11 | }, 12 | "from" : { "name" : "Garret Heaton", 13 | "user_id" : 10 14 | }, 15 | "message" : "This is a file upload" 16 | }, 17 | { "date" : "2010-11-19T16:13:40-0800", 18 | "from" : { "name" : "Deploy Bot", 19 | "user_id" : "api" 20 | }, 21 | "message" : "This message is sent via the API so the user_id is 'api'." 22 | } 23 | ] } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_list_response.json: -------------------------------------------------------------------------------- 1 | { "rooms" : [ { "last_active" : 1269020400, 2 | "name" : "Development", 3 | "owner_user_id" : 1, 4 | "room_id" : 7, 5 | "topic" : "Make sure to document your API functions well!" 6 | }, 7 | { "last_active" : 1269010500, 8 | "name" : "Ops", 9 | "owner_user_id" : 5, 10 | "room_id" : 10, 11 | "topic" : "Chef is so awesome." 12 | } 13 | ] } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_message_response.json: -------------------------------------------------------------------------------- 1 | { "status": "sent" } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_show_response.json: -------------------------------------------------------------------------------- 1 | { "room" : { "created" : 1269010311, 2 | "last_active" : 1269020400, 3 | "name" : "Ops", 4 | "owner_user_id" : 5, 5 | "participants" : [ { "name" : "Garret Heaton", 6 | "user_id" : 5 7 | }, 8 | { "name" : "Chris Rivers", 9 | "user_id" : 1 10 | } 11 | ], 12 | "room_id" : 5, 13 | "topic" : "Chef is so awesome." 14 | } } -------------------------------------------------------------------------------- /spec/fakeweb/rooms_topic_response.json: -------------------------------------------------------------------------------- 1 | { "status": "ok" } -------------------------------------------------------------------------------- /spec/fakeweb/users_create_response.json: -------------------------------------------------------------------------------- 1 | { "user" : { "email" : "new@company.com", 2 | "is_group_admin" : 0, 3 | "name" : "New Guy", 4 | "password" : "d294H58zlE", 5 | "photo_url" : "https://www.hipchat.com/img/silhouette_125.png", 6 | "status" : "offline", 7 | "status_message" : "", 8 | "title" : "Intern", 9 | "user_id" : 3 10 | } } -------------------------------------------------------------------------------- /spec/fakeweb/users_delete_response.json: -------------------------------------------------------------------------------- 1 | { "deleted": true } -------------------------------------------------------------------------------- /spec/fakeweb/users_list_include_deleted_response.json: -------------------------------------------------------------------------------- 1 | { "users" : [ { "email" : "chris@hipchat.com", 2 | "is_group_admin" : 1, 3 | "name" : "Chris Rivers", 4 | "photo_url" : "https://www.hipchat.com/chris.png", 5 | "status" : "away", 6 | "status_message" : "gym, bbl", 7 | "title" : "Developer", 8 | "user_id" : 1 9 | }, 10 | { "email" : "pete@hipchat.com", 11 | "is_group_admin" : 1, 12 | "name" : "Peter Curley", 13 | "photo_url" : "https://www.hipchat.com/pete.png", 14 | "status" : "offline", 15 | "status_message" : "", 16 | "title" : "Designer", 17 | "user_id" : 3 18 | }, 19 | { "email" : "garret@hipchat.com", 20 | "is_group_admin" : 1, 21 | "name" : "Garret Heaton", 22 | "photo_url" : "https://www.hipchat.com/garret.png", 23 | "status" : "available", 24 | "status_message" : "Come see what I'm working on!", 25 | "title" : "Co-founder", 26 | "user_id" : 5, 27 | "is_deleted" : 1 28 | } 29 | ] } 30 | -------------------------------------------------------------------------------- /spec/fakeweb/users_list_response.json: -------------------------------------------------------------------------------- 1 | { "users" : [ { "email" : "chris@hipchat.com", 2 | "is_group_admin" : 1, 3 | "name" : "Chris Rivers", 4 | "photo_url" : "https://www.hipchat.com/chris.png", 5 | "status" : "away", 6 | "status_message" : "gym, bbl", 7 | "title" : "Developer", 8 | "user_id" : 1 9 | }, 10 | { "email" : "pete@hipchat.com", 11 | "is_group_admin" : 1, 12 | "name" : "Peter Curley", 13 | "photo_url" : "https://www.hipchat.com/pete.png", 14 | "status" : "offline", 15 | "status_message" : "", 16 | "title" : "Designer", 17 | "user_id" : 3 18 | }, 19 | { "email" : "garret@hipchat.com", 20 | "is_group_admin" : 1, 21 | "name" : "Garret Heaton", 22 | "photo_url" : "https://www.hipchat.com/garret.png", 23 | "status" : "available", 24 | "status_message" : "Come see what I'm working on!", 25 | "title" : "Co-founder", 26 | "user_id" : 5 27 | } 28 | ] } -------------------------------------------------------------------------------- /spec/fakeweb/users_show_response.json: -------------------------------------------------------------------------------- 1 | { "user" : { "email" : "garret@hipchat.com", 2 | "is_group_admin" : 1, 3 | "name" : "Garret Heaton", 4 | "photo_url" : "https://www.hipchat.com/img/silhouette_125.png", 5 | "status" : "available", 6 | "status_message" : "Come see what I'm working on!", 7 | "title" : "Co-founder", 8 | "user_id" : 5 9 | } } -------------------------------------------------------------------------------- /spec/fakeweb/users_undelete_response.json: -------------------------------------------------------------------------------- 1 | { "undeleted": true } -------------------------------------------------------------------------------- /spec/fakeweb/users_update_response.json: -------------------------------------------------------------------------------- 1 | { "user" : { "email" : "new-email-address@hipchat.com", 2 | "is_group_admin" : 1, 3 | "name" : "Garret Heaton", 4 | "photo_url" : "https://www.hipchat.com/img/silhouette_125.png", 5 | "status" : "available", 6 | "status_message" : "Come see what I'm working on!", 7 | "title" : "Co-founder", 8 | "user_id" : 5 9 | } } -------------------------------------------------------------------------------- /spec/hipchat-api_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'fakeweb' 3 | require 'mocha' 4 | 5 | describe "HipChat::API" do 6 | before(:each) do 7 | FakeWeb.allow_net_connect = false 8 | FakeWeb.clean_registry 9 | @hipchat_api = HipChat::API.new('token') 10 | end 11 | 12 | after(:each) do 13 | FakeWeb.allow_net_connect = true 14 | end 15 | 16 | it "should be the correct version" do 17 | HipChat::API::VERSION.should == '1.0.6' 18 | end 19 | 20 | it "should create a new instance with the correct parameters" do 21 | @hipchat_api.token.should be == 'token' 22 | @hipchat_api.hipchat_api_url.should == HipChat::API::HIPCHAT_API_URL 23 | end 24 | 25 | it "should allow http headers to be set" do 26 | @hipchat_api.expects(:headers).at_least_once 27 | 28 | @hipchat_api.set_http_headers({'Accept' => 'application/json'}) 29 | end 30 | 31 | it "should allow a timeout to be set" do 32 | @hipchat_api.expects(:default_timeout).at_least_once 33 | 34 | @hipchat_api.set_timeout(10) 35 | end 36 | 37 | it "should allow you to create a room" do 38 | FakeWeb.register_uri(:post, 39 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/create|, 40 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_create_response.json'), 41 | :content_type => "application/json") 42 | 43 | rooms_create_response = @hipchat_api.rooms_create('Development', 5) 44 | rooms_create_response.should_not be nil 45 | rooms_create_response['room']['name'].should == 'Development' 46 | rooms_create_response['room']['owner_user_id'].should == 5 47 | end 48 | 49 | it "should allow you to delete a room" do 50 | FakeWeb.register_uri(:post, 51 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/delete|, 52 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_delete_response.json'), 53 | :content_type => "application/json") 54 | 55 | rooms_delete_response = @hipchat_api.rooms_delete(5) 56 | rooms_delete_response.should_not be nil 57 | rooms_delete_response['deleted'].should be true 58 | end 59 | 60 | it "should return a list of rooms" do 61 | FakeWeb.register_uri(:get, 62 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/list|, 63 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_list_response.json'), 64 | :content_type => "application/json") 65 | 66 | rooms_list_response = @hipchat_api.rooms_list 67 | rooms_list_response.should_not be nil 68 | rooms_list_response['rooms'].size.should be 2 69 | end 70 | 71 | it "should return the correct room informaton for a room_id" do 72 | FakeWeb.register_uri(:get, 73 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/show|, 74 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_show_response.json'), 75 | :content_type => "application/json") 76 | 77 | rooms_show_response = @hipchat_api.rooms_show(7) 78 | rooms_show_response.should_not be nil 79 | rooms_show_response['room']['topic'].should == 'Chef is so awesome.' 80 | end 81 | 82 | it "should send a message" do 83 | FakeWeb.register_uri(:post, 84 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/message|, 85 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_message_response.json'), 86 | :content_type => "application/json") 87 | 88 | rooms_message_response = @hipchat_api.rooms_message(10, 'Alerts', 'A new user signed up') 89 | rooms_message_response.should_not be nil 90 | rooms_message_response['status'].should == 'sent' 91 | end 92 | 93 | it "should update a room's topic" do 94 | FakeWeb.register_uri(:post, 95 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/topic|, 96 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_topic_response.json'), 97 | :content_type => "application/json") 98 | 99 | rooms_message_response = @hipchat_api.rooms_topic(10, 'This is a new topic') 100 | rooms_message_response.should_not be nil 101 | rooms_message_response['status'].should == 'ok' 102 | end 103 | 104 | it "should return a history of messages" do 105 | FakeWeb.register_uri(:get, 106 | %r|#{HipChat::API::HIPCHAT_API_URL}/rooms/history|, 107 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'rooms_history_response.json'), 108 | :content_type => "application/json") 109 | 110 | rooms_history_response = @hipchat_api.rooms_history(10, '2010-11-19', 'PST') 111 | rooms_history_response.should_not be nil 112 | rooms_history_response['messages'].size.should be 3 113 | end 114 | 115 | it "should create a user" do 116 | FakeWeb.register_uri(:post, 117 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/create|, 118 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_create_response.json'), 119 | :content_type => "application/json") 120 | 121 | users_create_response = @hipchat_api.users_create('new@company.com', 'New Guy', 'intern', is_group_admin = 0, 'changeme') 122 | users_create_response.should_not be nil 123 | users_create_response['user']['name'].should == 'New Guy' 124 | 125 | # Make sure the new user's password was sent 126 | FakeWeb.last_request.body.should_not be_nil 127 | FakeWeb.last_request.body.should match 'changeme' 128 | end 129 | 130 | it "should delete a user" do 131 | FakeWeb.register_uri(:post, 132 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/delete|, 133 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_delete_response.json'), 134 | :content_type => "application/json") 135 | 136 | users_delete_response = @hipchat_api.users_delete(5) 137 | users_delete_response.should_not be nil 138 | users_delete_response['deleted'].should be true 139 | end 140 | 141 | it "should return a list of users" do 142 | FakeWeb.register_uri(:get, 143 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/list|, 144 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_list_response.json'), 145 | :content_type => "application/json") 146 | 147 | users_list_response = @hipchat_api.users_list 148 | users_list_response.should_not be nil 149 | users_list_response['users'].size.should be 3 150 | end 151 | 152 | it "should return a list of users including deleted" do 153 | FakeWeb.register_uri(:get, 154 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/list.*include_deleted=1|, 155 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_list_include_deleted_response.json'), 156 | :content_type => "application/json") 157 | 158 | users_list_response = @hipchat_api.users_list(include_deleted = 1) 159 | users_list_response.should_not be nil 160 | users_list_response['users'].size.should be 3 161 | users_list_response['users'].select { |u| u['is_deleted'] == 1 }.size.should be 1 162 | users_list_response['users'].select { |u| u['is_deleted'] != 1 }.size.should be 2 163 | end 164 | 165 | it "should show the details for a user" do 166 | FakeWeb.register_uri(:get, 167 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/show|, 168 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_show_response.json'), 169 | :content_type => "application/json") 170 | 171 | users_show_response = @hipchat_api.users_show(5) 172 | users_show_response.should_not be nil 173 | users_show_response['user']['name'].should == 'Garret Heaton' 174 | end 175 | 176 | it "should undelete a user" do 177 | FakeWeb.register_uri(:post, 178 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/undelete|, 179 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_undelete_response.json'), 180 | :content_type => "application/json") 181 | 182 | users_update_response = @hipchat_api.users_undelete('new-email-address@hipchat.com') 183 | users_update_response.should_not be nil 184 | users_update_response['undeleted'].should be true 185 | end 186 | 187 | it "should update a user" do 188 | FakeWeb.register_uri(:post, 189 | %r|#{HipChat::API::HIPCHAT_API_URL}/users/update|, 190 | :body => File.join(File.dirname(__FILE__), 'fakeweb', 'users_update_response.json'), 191 | :content_type => "application/json") 192 | 193 | users_update_response = @hipchat_api.users_update(5, 'new-email-address@hipchat.com') 194 | users_update_response.should_not be nil 195 | users_update_response['user']['email'].should == 'new-email-address@hipchat.com' 196 | end 197 | end 198 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require 'hipchat-api' 3 | 4 | # Requires supporting files with custom matchers and macros, etc, 5 | # in ./support/ and its subdirectories. 6 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 7 | 8 | RSpec.configure do |config| 9 | end 10 | --------------------------------------------------------------------------------