This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dimma.rb
144 lines (127 loc) · 3.93 KB
/
dimma.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'dimma/version'
require 'delegate'
require 'rest_client'
unless defined?(JSON.parse)
begin
require 'json'
rescue LoadError
warn "Dimma requires a json library supplying JSON::parse to be available"
raise
end
end
# See http://beaconpush.com/guide for specification details.
#
module Dimma
# The Beacon API (http://beaconpush.com/guide/rest-api) version Dimma conforms to.
API_VERSION = "1.0.0"
# @param (see Session#initialize)
# @return (see Session#initialize)
# @see Session#initialize
def Dimma.new(*args)
Session.new(*args)
end
# Missing method calls are delegated to the underlying RestClient::Resource
class Session < SimpleDelegator
# Initialize the Beacon API resource; you get both your API key and secret key from http://beaconpush.com/account/settings.
#
# @example
# # Specify that no requests must take longer than 10 seconds.
# dimma = Dimma::Session.new "key", "secret", :timeout => 10
#
# @param [#to_s] key API key.
# @param [nil, #to_s] secret Secret key. Set to nil if you have turned off authentication.
# @param [Hash] options (see RestClient#initialize)
def initialize(key, secret = nil, options = {})
url = "http://api.beaconpush.com/#{Dimma::API_VERSION}/#{key.to_s}/"
options[:headers] ||= {}
options[:headers].merge!({"X-Beacon-Secret-Key" => secret.to_s}) unless secret.nil?
__setobj__ RestClient::Resource.new(url, options)
end
# Retrieve number of online users.
#
# @return [Fixnum]
def users
JSON.parse(self['users'].get.body)["online"]
end
# Send a message to the 'default' channel.
#
# @param (see Channel#message)
# @return (see Channel#message)
# @see Channel#message
def message(message)
channel.message(message)
end
# Retrieve a user by name, bound to this Session.
#
# @param [#to_s] name
# @return (see User#initialize)
def user(name)
User.new(name, __getobj__)
end
# Retrieve a channel by name, bound to this Session.
#
# @param [#to_s] name
# @return (see Channel#initialize)
def channel(name = 'default')
Channel.new(name, __getobj__)
end
end
# Missing method calls are delegated to the underlying RestClient::Resource
class User < SimpleDelegator
# @return [String]
attr_reader :name
# Create a new User resource.
#
# @param [#to_s] name
# @param [RestClient::Resource] resource An object conformant with the RestClient API.
def initialize(name, resource)
__setobj__ resource["users/#{@name = name.to_s}"]
end
# True if the user is online.
#
# @return [Boolean]
def online?
get.code == 200 rescue false
end
# Force-logout the user.
#
# @return [RestClient::Response]
def logout
delete
end
# Send a message to the user.
#
# @param [#to_json] message Data to send to the user.
# @return [RestClient::Response]
# @raise [RestClient::Exception]
def message(message)
post message.to_json
end
end
# Missing method calls are delegated to the underlying RestClient::Resource
class Channel < SimpleDelegator
# @return [String]
attr_reader :name
# Create a new Channel resource.
#
# @param [#to_s] name
# @param [RestClient::Resource] resource An object conformant with the RestClient API.
def initialize(name, resource)
__setobj__ resource["channels/#{@name = name.to_s}"]
end
# Sends a message to all users in the channel.
#
# @param [#to_json] message Data to send to the channel.
# @return [RestClient::Response]
# @raise [RestClient::Exception]
def message(message)
post message.to_json
end
# Retrieve all users in the channel.
#
# @return [Array<User>] Array of users.
def users
JSON.parse(get.body)["users"].map { |user| User.new(user, __getobj__) }
end
end
end