pushbullet-ruby 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/pushbullet.rb +225 -0
- data/lib/v2/contacts.rb +70 -0
- data/lib/v2/devices.rb +68 -0
- data/lib/v2/push.rb +195 -0
- data/lib/v2/subscriptions.rb +67 -0
- data/lib/v2/test/test_contacts.rb +55 -0
- data/lib/v2/test/test_devices.rb +55 -0
- data/lib/v2/test/test_push.rb +46 -0
- data/lib/v2/test/test_subscriptions.rb +56 -0
- data/lib/v2/test/test_users.rb +53 -0
- data/lib/v2/users.rb +43 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61f287161d7639aa2b93b18eec1da8bbb4cffb53
|
4
|
+
data.tar.gz: f2691f77adc6853cc03944af01519f9b732424d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 082a09f965baaf7de420161304c851574969c35af79beb2b52efc427e631ba35d733e2162f616eb8b4bb3a38611acc532bca3c945d63bf3f1dc21089060cf533
|
7
|
+
data.tar.gz: 02fbfc35a761405e09e4c636ea811b282d0f4c4b7e47c128bd02ea9cc95604236b403985daabe582c9ff2e05ca6df3c0c0ee3cbb7c4b5b02a7c7b7bfa8a5c8a2
|
data/lib/pushbullet.rb
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/pushbullet.rb
|
4
|
+
#
|
5
|
+
# library for sending various messages through Pushbullet
|
6
|
+
# (api: https://docs.pushbullet.com/http/)
|
7
|
+
#
|
8
|
+
# created on : 2014.12.17
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'net/http'
|
14
|
+
require 'uri'
|
15
|
+
require 'base64'
|
16
|
+
require 'json'
|
17
|
+
|
18
|
+
# v2 api
|
19
|
+
require_relative 'v2/push'
|
20
|
+
require_relative 'v2/users'
|
21
|
+
require_relative 'v2/devices'
|
22
|
+
require_relative 'v2/contacts'
|
23
|
+
require_relative 'v2/subscriptions'
|
24
|
+
|
25
|
+
module Pushbullet
|
26
|
+
@@verbose = false
|
27
|
+
@@access_token = nil
|
28
|
+
|
29
|
+
module V2
|
30
|
+
private
|
31
|
+
|
32
|
+
# post request to Pushbullet with given url and params
|
33
|
+
# @param url [String] api url
|
34
|
+
# @param params [Hash] post parameters
|
35
|
+
# @return [Net::HTTPResponse]
|
36
|
+
def self.request(url, params)
|
37
|
+
Pushbullet.post_data(url, params.to_json, 'application/json', {
|
38
|
+
'Authorization' => "Basic #{Base64.encode64(Pushbullet::get_access_token).chomp}",
|
39
|
+
})
|
40
|
+
end
|
41
|
+
|
42
|
+
# get request to Pushbullet with given url and params
|
43
|
+
# @param url [String] api url
|
44
|
+
# @param params [Hash] get parameters
|
45
|
+
# @return [Net::HTTPResponse]
|
46
|
+
def self.request_get(url, params)
|
47
|
+
Pushbullet.get(url, params, {
|
48
|
+
'Authorization' => "Basic #{Base64.encode64(Pushbullet::get_access_token).chomp}",
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
# delete request to Pushbullet with given url and params
|
53
|
+
# @param url [String] api url
|
54
|
+
# @param params [Hash] delete parameters
|
55
|
+
# @return [Net::HTTPResponse]
|
56
|
+
def self.request_delete(url, params)
|
57
|
+
Pushbullet.delete(url, params, {
|
58
|
+
'Authorization' => "Basic #{Base64.encode64(Pushbullet::get_access_token).chomp}",
|
59
|
+
})
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
public
|
64
|
+
|
65
|
+
def self.set_verbose(is_verbose = true)
|
66
|
+
@@verbose = is_verbose
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.set_access_token(token)
|
70
|
+
@@access_token = token
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def self.is_verbose
|
76
|
+
@@verbose
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.get_access_token
|
80
|
+
unless @@access_token
|
81
|
+
puts "* Pushbullet's access token should be set first. (can be found at: https://www.pushbullet.com/account)"
|
82
|
+
exit 1
|
83
|
+
end
|
84
|
+
@@access_token
|
85
|
+
end
|
86
|
+
|
87
|
+
# urlencode given string
|
88
|
+
# http://tools.ietf.org/html/rfc3986#section-2.3
|
89
|
+
#
|
90
|
+
# @param str [String] string to urlencode
|
91
|
+
# @param conform_to_rfc3986 [true,false]
|
92
|
+
# @return [String] urlencoded string
|
93
|
+
def self.urlencode(str, conform_to_rfc3986 = true)
|
94
|
+
URI.escape(str.to_s, conform_to_rfc3986 ? Regexp.new("[^#{"-_.~a-zA-Z\\d"}]") : Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
95
|
+
end
|
96
|
+
|
97
|
+
# http get
|
98
|
+
#
|
99
|
+
# @param url [String] get url
|
100
|
+
# @param parameters [Hash] post parameters
|
101
|
+
# @param additional_headers [Hash] additional HTTP headers
|
102
|
+
# @return [Net::HTTPResponse]
|
103
|
+
def self.get(url, parameters, additional_headers)
|
104
|
+
parameters.each_pair{|key, value|
|
105
|
+
unless url.include?("?")
|
106
|
+
url += "?"
|
107
|
+
else
|
108
|
+
url += "&"
|
109
|
+
end
|
110
|
+
url += "#{Pushbullet.urlencode(key)}=#{Pushbullet.urlencode(value)}"
|
111
|
+
}
|
112
|
+
uri = URI.parse(url)
|
113
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https'){|http|
|
114
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
115
|
+
unless additional_headers.nil?
|
116
|
+
additional_headers.each_pair{|key, value|
|
117
|
+
req.add_field(key, value)
|
118
|
+
}
|
119
|
+
end
|
120
|
+
return http.request(req)
|
121
|
+
}
|
122
|
+
end
|
123
|
+
|
124
|
+
# http delete
|
125
|
+
#
|
126
|
+
# @param url [String] delete url
|
127
|
+
# @param parameters [Hash] post parameters
|
128
|
+
# @param additional_headers [Hash] additional HTTP headers
|
129
|
+
# @return [Net::HTTPResponse]
|
130
|
+
def self.delete(url, parameters, additional_headers)
|
131
|
+
parameters.each_pair{|key, value|
|
132
|
+
unless url.include?("?")
|
133
|
+
url += "?"
|
134
|
+
else
|
135
|
+
url += "&"
|
136
|
+
end
|
137
|
+
url += "#{Pushbullet.urlencode(key)}=#{Pushbullet.urlencode(value)}"
|
138
|
+
}
|
139
|
+
uri = URI.parse(url)
|
140
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https'){|http|
|
141
|
+
req = Net::HTTP::Delete.new(uri.request_uri)
|
142
|
+
unless additional_headers.nil?
|
143
|
+
additional_headers.each_pair{|key, value|
|
144
|
+
req.add_field(key, value)
|
145
|
+
}
|
146
|
+
end
|
147
|
+
return http.request(req)
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
# http post
|
152
|
+
#
|
153
|
+
# @param url [String] post url
|
154
|
+
# @param parameters [Hash] post parameters
|
155
|
+
# @param additional_headers [Hash] additional HTTP headers
|
156
|
+
# @return [Net::HTTPResponse]
|
157
|
+
def self.post(url, parameters, additional_headers)
|
158
|
+
uri = URI.parse(url)
|
159
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https'){|http|
|
160
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
161
|
+
req.set_form_data(parameters, ';')
|
162
|
+
additional_headers.each_pair{|key, value|
|
163
|
+
req.add_field(key.to_s, value.to_s)
|
164
|
+
}
|
165
|
+
return http.request(req)
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
169
|
+
# http post data
|
170
|
+
#
|
171
|
+
# @param url [String] post url
|
172
|
+
# @param data [Object] data
|
173
|
+
# @param content_type [String] mime type of data
|
174
|
+
# @param additional_headers [Hash] additional HTTP headers
|
175
|
+
# @return [Net::HTTPResponse]
|
176
|
+
def self.post_data(url, data, content_type, additional_headers)
|
177
|
+
uri = URI.parse(url)
|
178
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https'){|http|
|
179
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
180
|
+
req.body = data
|
181
|
+
req['Content-Type'] = content_type
|
182
|
+
additional_headers.each_pair{|key, value|
|
183
|
+
req.add_field(key.to_s, value.to_s)
|
184
|
+
}
|
185
|
+
return http.request(req)
|
186
|
+
}
|
187
|
+
end
|
188
|
+
|
189
|
+
# http post multipart
|
190
|
+
#
|
191
|
+
# @param url [String] post url
|
192
|
+
# @param parameters [Hash] post parameters
|
193
|
+
# @param additional_headers [Hash] additional HTTP headers
|
194
|
+
# @return [Net::HTTPResponse]
|
195
|
+
def self.post_multipart(url, parameters, additional_headers)
|
196
|
+
uri = URI.parse(url)
|
197
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https'){|http|
|
198
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
199
|
+
boundary = "____boundary_#{Time.now.to_i.to_s}____"
|
200
|
+
req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
201
|
+
body = ""
|
202
|
+
parameters.each_pair{|key, value|
|
203
|
+
body << "--#{boundary}\r\n"
|
204
|
+
if value.respond_to?(:read) # check if it's a File object
|
205
|
+
body << "Content-Disposition: form-data; name=\"#{Pushbullet.urlencode(key)}\"; filename=\"#{File.basename(value.path)}\"\r\n"
|
206
|
+
body << "Content-Type: #{MimeMagic.by_path(value.path)}\r\n\r\n"
|
207
|
+
body << value.read
|
208
|
+
else
|
209
|
+
body << "Content-Disposition: form-data; name=\"#{Pushbullet.urlencode(key)}\"\r\n"
|
210
|
+
body << "Content-Type: text/plain\r\n\r\n"
|
211
|
+
body << value
|
212
|
+
end
|
213
|
+
body << "\r\n"
|
214
|
+
}
|
215
|
+
body << "--#{boundary}--\r\n"
|
216
|
+
req.body = body
|
217
|
+
req["Content-Length"] = req.body.size
|
218
|
+
additional_headers.each_pair{|key, value|
|
219
|
+
req.add_field(key.to_s, value.to_s)
|
220
|
+
}
|
221
|
+
return http.request(req)
|
222
|
+
}
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
data/lib/v2/contacts.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/v2/contacts.rb
|
4
|
+
#
|
5
|
+
# functions for Pushbullet contacts api
|
6
|
+
#
|
7
|
+
# created on : 2014.12.18
|
8
|
+
# last update: 2014.12.18
|
9
|
+
#
|
10
|
+
# by meinside@gmail.com
|
11
|
+
|
12
|
+
require 'json'
|
13
|
+
require 'mimemagic'
|
14
|
+
|
15
|
+
module Pushbullet
|
16
|
+
module V2
|
17
|
+
class Contacts
|
18
|
+
API_URL = 'https://api.pushbullet.com/v2/contacts'
|
19
|
+
|
20
|
+
public
|
21
|
+
|
22
|
+
# get contacts list
|
23
|
+
#
|
24
|
+
# @return [JSON] result as json
|
25
|
+
def self.get
|
26
|
+
result = Pushbullet::V2::request_get(API_URL, {})
|
27
|
+
|
28
|
+
JSON.parse(result.body)
|
29
|
+
end
|
30
|
+
|
31
|
+
# create a new contact
|
32
|
+
#
|
33
|
+
# @param name [String] nickname of the contact
|
34
|
+
# @param email [String] email of the contact
|
35
|
+
# @return [JSON] result as json
|
36
|
+
def self.create(name, email)
|
37
|
+
result = Pushbullet::V2::request(API_URL, {
|
38
|
+
name: name,
|
39
|
+
email: email,
|
40
|
+
})
|
41
|
+
|
42
|
+
JSON.parse(result.body)
|
43
|
+
end
|
44
|
+
|
45
|
+
# update a contact
|
46
|
+
#
|
47
|
+
# @param contact_iden [String] contact identifier
|
48
|
+
# @param name [String] name of the contact
|
49
|
+
# @return [JSON] result as json
|
50
|
+
def self.update(contact_iden, name)
|
51
|
+
result = Pushbullet::V2::request(API_URL + '/' + contact_iden, {
|
52
|
+
name: name,
|
53
|
+
})
|
54
|
+
|
55
|
+
JSON.parse(result.body)
|
56
|
+
end
|
57
|
+
|
58
|
+
# delete a contact
|
59
|
+
#
|
60
|
+
# @param contact_iden [String] contact identifier
|
61
|
+
# @return [JSON] result as json
|
62
|
+
def self.delete(contact_iden)
|
63
|
+
result = Pushbullet::V2::request_delete(API_URL + '/' + contact_iden, {})
|
64
|
+
|
65
|
+
JSON.parse(result.body)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
data/lib/v2/devices.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/v2/devices.rb
|
4
|
+
#
|
5
|
+
# functions for Pushbullet devices api
|
6
|
+
#
|
7
|
+
# created on : 2014.12.18
|
8
|
+
# last update: 2014.12.18
|
9
|
+
#
|
10
|
+
# by meinside@gmail.com
|
11
|
+
|
12
|
+
require 'json'
|
13
|
+
require 'mimemagic'
|
14
|
+
|
15
|
+
module Pushbullet
|
16
|
+
module V2
|
17
|
+
class Devices
|
18
|
+
API_URL = 'https://api.pushbullet.com/v2/devices'
|
19
|
+
|
20
|
+
public
|
21
|
+
|
22
|
+
# get devices list
|
23
|
+
#
|
24
|
+
# @return [JSON] result as json
|
25
|
+
def self.get
|
26
|
+
result = Pushbullet::V2::request_get(API_URL, {})
|
27
|
+
|
28
|
+
JSON.parse(result.body)
|
29
|
+
end
|
30
|
+
|
31
|
+
# create a new device
|
32
|
+
#
|
33
|
+
# @param nickname [String] nickname of the device
|
34
|
+
# @param type [String] type of the device
|
35
|
+
# @return [JSON] result as json
|
36
|
+
def self.register(nickname, type)
|
37
|
+
result = Pushbullet::V2::request(API_URL, {
|
38
|
+
nickname: nickname,
|
39
|
+
type: type,
|
40
|
+
})
|
41
|
+
|
42
|
+
JSON.parse(result.body)
|
43
|
+
end
|
44
|
+
|
45
|
+
# update a device
|
46
|
+
#
|
47
|
+
# @param device_iden [String] device identifier
|
48
|
+
# @param values [Hash] values to update
|
49
|
+
# @return [JSON] result as json
|
50
|
+
def self.update(device_iden, values)
|
51
|
+
result = Pushbullet::V2::request(API_URL + '/' + device_iden, values)
|
52
|
+
|
53
|
+
JSON.parse(result.body)
|
54
|
+
end
|
55
|
+
|
56
|
+
# delete a device
|
57
|
+
#
|
58
|
+
# @param device_iden [String] device identifier
|
59
|
+
# @return [JSON] result as json
|
60
|
+
def self.delete(device_iden)
|
61
|
+
result = Pushbullet::V2::request_delete(API_URL + '/' + device_iden, {})
|
62
|
+
|
63
|
+
JSON.parse(result.body)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/lib/v2/push.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/v2/push.rb
|
4
|
+
#
|
5
|
+
# functions for Pushbullet push api
|
6
|
+
#
|
7
|
+
# created on : 2014.12.18
|
8
|
+
# last update: 2014.12.18
|
9
|
+
#
|
10
|
+
# by meinside@gmail.com
|
11
|
+
|
12
|
+
require 'mimemagic'
|
13
|
+
require 'json'
|
14
|
+
|
15
|
+
module Pushbullet
|
16
|
+
module V2
|
17
|
+
class Push
|
18
|
+
API_URL = 'https://api.pushbullet.com/v2/pushes'
|
19
|
+
|
20
|
+
public
|
21
|
+
|
22
|
+
# push note
|
23
|
+
#
|
24
|
+
# @param title [String] title of note
|
25
|
+
# @param text [String] body text of note
|
26
|
+
# @param recipient [Hash] key-value of either one of: 'email', 'device_iden', 'channel_tag', or 'client_iden' (can be nil)
|
27
|
+
# @return [JSON] result as json (nil if error)
|
28
|
+
def self.note(title, text, recipient = nil)
|
29
|
+
params = {
|
30
|
+
type: :note,
|
31
|
+
title: title,
|
32
|
+
body: text,
|
33
|
+
}
|
34
|
+
params.merge!(recipient) if recipient
|
35
|
+
|
36
|
+
result = Pushbullet::V2::request(API_URL, params)
|
37
|
+
|
38
|
+
case result
|
39
|
+
when Net::HTTPOK
|
40
|
+
return JSON.parse(result.body)
|
41
|
+
else
|
42
|
+
puts result.body if Pushbullet.is_verbose
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# push link
|
48
|
+
#
|
49
|
+
# @param title [String] title of note
|
50
|
+
# @param text [String] body text of note
|
51
|
+
# @param link [String] url of the link
|
52
|
+
# @param recipient [Hash] key-value of either one of: 'email', 'device_iden', 'channel_tag', or 'client_iden' (can be nil)
|
53
|
+
# @return [JSON] result as json (nil if error)
|
54
|
+
def self.link(title, text, link, recipient = nil)
|
55
|
+
params = {
|
56
|
+
type: :link,
|
57
|
+
title: title,
|
58
|
+
body: text,
|
59
|
+
url: link,
|
60
|
+
}
|
61
|
+
params.merge!(recipient) if recipient
|
62
|
+
|
63
|
+
result = Pushbullet::V2::request(API_URL, params)
|
64
|
+
|
65
|
+
case result
|
66
|
+
when Net::HTTPOK
|
67
|
+
return JSON.parse(result.body)
|
68
|
+
else
|
69
|
+
puts result.body if Pushbullet.is_verbose
|
70
|
+
return nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# push address
|
75
|
+
#
|
76
|
+
# @param name [String] name of address
|
77
|
+
# @param address [String] address itself, or query for the address
|
78
|
+
# @param recipient [Hash] key-value of either one of: 'email', 'device_iden', 'channel_tag', or 'client_iden' (can be nil)
|
79
|
+
# @return [JSON] result as json (nil if error)
|
80
|
+
def self.address(name, address, recipient = nil)
|
81
|
+
params = {
|
82
|
+
type: :address,
|
83
|
+
name: name,
|
84
|
+
address: address,
|
85
|
+
}
|
86
|
+
params.merge!(recipient) if recipient
|
87
|
+
|
88
|
+
result = Pushbullet::V2::request(API_URL, params)
|
89
|
+
|
90
|
+
case result
|
91
|
+
when Net::HTTPOK
|
92
|
+
return JSON.parse(result.body)
|
93
|
+
else
|
94
|
+
puts result.body if Pushbullet.is_verbose
|
95
|
+
return nil
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# push checklist
|
100
|
+
#
|
101
|
+
# @param title [String] title of note
|
102
|
+
# @param items [Array<String>] array of checklist items
|
103
|
+
# @param recipient [Hash] key-value of either one of: 'email', 'device_iden', 'channel_tag', or 'client_iden' (can be nil)
|
104
|
+
# @return [JSON] result as json (nil if error)
|
105
|
+
def self.checklist(title, items, recipient = nil)
|
106
|
+
params = {
|
107
|
+
type: :list,
|
108
|
+
title: title,
|
109
|
+
items: items,
|
110
|
+
}
|
111
|
+
params.merge!(recipient) if recipient
|
112
|
+
|
113
|
+
result = Pushbullet::V2::request(API_URL, params)
|
114
|
+
|
115
|
+
case result
|
116
|
+
when Net::HTTPOK
|
117
|
+
return JSON.parse(result.body)
|
118
|
+
else
|
119
|
+
puts result.body if Pushbullet.is_verbose
|
120
|
+
return nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# push file
|
125
|
+
#
|
126
|
+
# @param filepath [String] absolute path of file
|
127
|
+
# @param text [String] body text
|
128
|
+
# @param recipient [Hash] key-value of either one of: 'email', 'device_iden', 'channel_tag', or 'client_iden' (can be nil)
|
129
|
+
# @return [JSON] result as json (nil if error)
|
130
|
+
def self.file(filepath, text, recipient = nil)
|
131
|
+
to_upload = _request_upload_file(filepath)
|
132
|
+
if _upload_file(filepath, to_upload['upload_url'], to_upload['data'])
|
133
|
+
params = {
|
134
|
+
type: :file,
|
135
|
+
file_name: to_upload['file_name'],
|
136
|
+
file_type: to_upload['file_type'],
|
137
|
+
file_url: to_upload['file_url'],
|
138
|
+
body: text,
|
139
|
+
}
|
140
|
+
params.merge!(recipient) if recipient
|
141
|
+
|
142
|
+
result = Pushbullet::V2::request(API_URL, params)
|
143
|
+
|
144
|
+
case result
|
145
|
+
when Net::HTTPOK
|
146
|
+
return JSON.parse(result.body)
|
147
|
+
else
|
148
|
+
puts result.body if Pushbullet.is_verbose
|
149
|
+
return nil
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
false
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
def self._request_upload_file(filepath)
|
159
|
+
result = Pushbullet::post('https://api.pushbullet.com/v2/upload-request', {
|
160
|
+
file_name: File.basename(filepath),
|
161
|
+
file_type: MimeMagic.by_path(filepath),
|
162
|
+
}, {
|
163
|
+
'Authorization' => "Basic #{Base64.encode64(Pushbullet::get_access_token).chomp}",
|
164
|
+
})
|
165
|
+
|
166
|
+
JSON.parse(result.body)
|
167
|
+
end
|
168
|
+
|
169
|
+
def self._upload_file(filepath, upload_url, auth_data)
|
170
|
+
File.open(filepath, 'rb'){|file|
|
171
|
+
result = Pushbullet::post_multipart(upload_url, {
|
172
|
+
awsaccesskeyid: auth_data['awsaccesskeyid'],
|
173
|
+
acl: auth_data['acl'],
|
174
|
+
key: auth_data['key'],
|
175
|
+
signature: auth_data['signature'],
|
176
|
+
policy: auth_data['policy'],
|
177
|
+
'content-type' => auth_data['content-type'],
|
178
|
+
file: file,
|
179
|
+
}, {})
|
180
|
+
|
181
|
+
case result
|
182
|
+
when Net::HTTPNoContent
|
183
|
+
return true
|
184
|
+
else
|
185
|
+
puts result.body if Pushbullet.is_verbose
|
186
|
+
return false
|
187
|
+
end
|
188
|
+
}
|
189
|
+
|
190
|
+
false
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/v2/subscriptions.rb
|
4
|
+
#
|
5
|
+
# functions for Pushbullet subscriptions api
|
6
|
+
#
|
7
|
+
# created on : 2014.12.18
|
8
|
+
# last update: 2014.12.18
|
9
|
+
#
|
10
|
+
# by meinside@gmail.com
|
11
|
+
|
12
|
+
require 'json'
|
13
|
+
require 'mimemagic'
|
14
|
+
|
15
|
+
module Pushbullet
|
16
|
+
module V2
|
17
|
+
class Subscriptions
|
18
|
+
API_URL = 'https://api.pushbullet.com/v2/subscriptions'
|
19
|
+
|
20
|
+
public
|
21
|
+
|
22
|
+
# get subscriptions list
|
23
|
+
#
|
24
|
+
# @return [JSON] result as json
|
25
|
+
def self.get
|
26
|
+
result = Pushbullet::V2::request_get(API_URL, {})
|
27
|
+
|
28
|
+
JSON.parse(result.body)
|
29
|
+
end
|
30
|
+
|
31
|
+
# subscribe a new subscription
|
32
|
+
#
|
33
|
+
# @param channel_tag [String] channel tag to subscribe
|
34
|
+
# @return [JSON] result as json
|
35
|
+
def self.subscribe(channel_tag)
|
36
|
+
result = Pushbullet::V2::request(API_URL, {
|
37
|
+
channel_tag: channel_tag,
|
38
|
+
})
|
39
|
+
|
40
|
+
JSON.parse(result.body)
|
41
|
+
end
|
42
|
+
|
43
|
+
# unsubscribe a subscription
|
44
|
+
#
|
45
|
+
# @param subscription_iden [String] subscription identifier
|
46
|
+
# @return [JSON] result as json
|
47
|
+
def self.unsubscribe(subscription_iden)
|
48
|
+
result = Pushbullet::V2::request_delete(API_URL + '/' + subscription_iden, {})
|
49
|
+
|
50
|
+
JSON.parse(result.body)
|
51
|
+
end
|
52
|
+
|
53
|
+
# get information on given channel
|
54
|
+
#
|
55
|
+
# @param channel_tag [String] channel tag
|
56
|
+
# @return [JSON] result as json
|
57
|
+
def self.channel_info(channel_tag)
|
58
|
+
result = Pushbullet::V2::request_get('https://api.pushbullet.com/v2/channel-info', {
|
59
|
+
tag: channel_tag,
|
60
|
+
})
|
61
|
+
|
62
|
+
JSON.parse(result.body)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: UTF-8
|
3
|
+
|
4
|
+
# test_contacts.rb
|
5
|
+
#
|
6
|
+
# test script for lib/v2/contacts.rb
|
7
|
+
#
|
8
|
+
# created on : 2014.12.18
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
|
15
|
+
require 'io/console'
|
16
|
+
|
17
|
+
require_relative '../../pushbullet'
|
18
|
+
|
19
|
+
class TestContacts < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def setup
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_contacts
|
26
|
+
print '> input your Pushbullet access token: '
|
27
|
+
input = STDIN.noecho(&:gets)
|
28
|
+
|
29
|
+
assert_not_nil(input)
|
30
|
+
access_token = input.chomp
|
31
|
+
|
32
|
+
Pushbullet.set_access_token(access_token)
|
33
|
+
|
34
|
+
# get
|
35
|
+
assert_not_nil(Pushbullet::V2::Contacts.get)
|
36
|
+
|
37
|
+
# create
|
38
|
+
created = Pushbullet::V2::Contacts.create('test contact', 'email-that-does-not-exist@earth.com')
|
39
|
+
assert_not_nil(created)
|
40
|
+
|
41
|
+
# update
|
42
|
+
new_name = 'test contact 2'
|
43
|
+
updated = Pushbullet::V2::Contacts.update(created['iden'], new_name)
|
44
|
+
assert_not_nil(updated)
|
45
|
+
assert_equal(updated['name'], new_name)
|
46
|
+
|
47
|
+
# delete
|
48
|
+
assert_not_nil(Pushbullet::V2::Contacts.delete(created['iden']))
|
49
|
+
end
|
50
|
+
|
51
|
+
def teardown
|
52
|
+
# do nothing
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: UTF-8
|
3
|
+
|
4
|
+
# test_devices.rb
|
5
|
+
#
|
6
|
+
# test script for lib/v2/devices.rb
|
7
|
+
#
|
8
|
+
# created on : 2014.12.18
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
|
15
|
+
require 'io/console'
|
16
|
+
|
17
|
+
require_relative '../../pushbullet'
|
18
|
+
|
19
|
+
class TestDevices < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def setup
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_devices
|
26
|
+
print '> input your Pushbullet access token: '
|
27
|
+
input = STDIN.noecho(&:gets)
|
28
|
+
|
29
|
+
assert_not_nil(input)
|
30
|
+
access_token = input.chomp
|
31
|
+
|
32
|
+
Pushbullet.set_access_token(access_token)
|
33
|
+
|
34
|
+
# get
|
35
|
+
assert_not_nil(Pushbullet::V2::Devices.get)
|
36
|
+
|
37
|
+
# register
|
38
|
+
registered = Pushbullet::V2::Devices.register('test device', 'android')
|
39
|
+
assert_not_nil(registered)
|
40
|
+
|
41
|
+
# update
|
42
|
+
new_name = 'test device 2'
|
43
|
+
updated = Pushbullet::V2::Devices.update(registered['iden'], {nickname: new_name})
|
44
|
+
assert_not_nil(updated)
|
45
|
+
assert_equal(updated['nickname'], new_name)
|
46
|
+
|
47
|
+
# delete
|
48
|
+
assert_not_nil(Pushbullet::V2::Devices.delete(registered['iden']))
|
49
|
+
end
|
50
|
+
|
51
|
+
def teardown
|
52
|
+
# do nothing
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: UTF-8
|
3
|
+
|
4
|
+
# test_push.rb
|
5
|
+
#
|
6
|
+
# test script for lib/v2/push.rb
|
7
|
+
#
|
8
|
+
# created on : 2014.12.18
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
|
15
|
+
require 'io/console'
|
16
|
+
|
17
|
+
require_relative '../../pushbullet'
|
18
|
+
|
19
|
+
class TestPush < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def setup
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_push
|
26
|
+
print '> input your Pushbullet access token: '
|
27
|
+
input = STDIN.noecho(&:gets)
|
28
|
+
|
29
|
+
assert_not_nil(input)
|
30
|
+
access_token = input.chomp
|
31
|
+
|
32
|
+
Pushbullet.set_access_token(access_token)
|
33
|
+
|
34
|
+
assert_not_nil(Pushbullet::V2::Push.note('Testing pushbullet-ruby push.note', 'This note is for testing.'))
|
35
|
+
assert_not_nil(Pushbullet::V2::Push.link('Testing pushbullet-ruby push.link', 'This link is for testing.', 'https://docs.pushbullet.com/v2/pushes/'))
|
36
|
+
assert_not_nil(Pushbullet::V2::Push.address('Testing pushbullet-ruby push.address', 'Google Korea LLC. 22nd Floor, Gangnam Finance Center 152 Teheran-ro, Gangnam-gu'))
|
37
|
+
assert_not_nil(Pushbullet::V2::Push.checklist('Testing pushbullet-ruby push.list', ['Test 1', 'Test 2', 'Test 3']))
|
38
|
+
assert_not_nil(Pushbullet::V2::Push.file(__FILE__, 'Testing pushbullet-ruby push.file'))
|
39
|
+
end
|
40
|
+
|
41
|
+
def teardown
|
42
|
+
# do nothing
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: UTF-8
|
3
|
+
|
4
|
+
# test_subscriptions.rb
|
5
|
+
#
|
6
|
+
# test script for lib/v2/subscriptions.rb
|
7
|
+
#
|
8
|
+
# created on : 2014.12.18
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
|
15
|
+
require 'io/console'
|
16
|
+
|
17
|
+
require_relative '../../pushbullet'
|
18
|
+
|
19
|
+
class TestSubscriptions < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def setup
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_subscriptions
|
26
|
+
print '> input your Pushbullet access token: '
|
27
|
+
input = STDIN.noecho(&:gets)
|
28
|
+
|
29
|
+
assert_not_nil(input)
|
30
|
+
access_token = input.chomp
|
31
|
+
|
32
|
+
Pushbullet.set_access_token(access_token)
|
33
|
+
|
34
|
+
# subscribe
|
35
|
+
channel_tag = 'pushbullet'
|
36
|
+
subscribed = Pushbullet::V2::Subscriptions.subscribe(channel_tag)
|
37
|
+
assert_not_nil(subscribed) # XXX - should check its result (it can be 'Already subscribed to this channel.' error)
|
38
|
+
|
39
|
+
# get
|
40
|
+
subscriptions = Pushbullet::V2::Subscriptions.get
|
41
|
+
assert_not_nil(subscriptions)
|
42
|
+
|
43
|
+
subscribed = subscriptions['subscriptions'].first
|
44
|
+
|
45
|
+
# channel info
|
46
|
+
assert_not_nil(Pushbullet::V2::Subscriptions.channel_info(subscribed['channel']['tag']))
|
47
|
+
|
48
|
+
# unsubscribe
|
49
|
+
assert_not_nil(Pushbullet::V2::Subscriptions.unsubscribe(subscribed['iden']))
|
50
|
+
end
|
51
|
+
|
52
|
+
def teardown
|
53
|
+
# do nothing
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: UTF-8
|
3
|
+
|
4
|
+
# test_users.rb
|
5
|
+
#
|
6
|
+
# test script for lib/v2/users.rb
|
7
|
+
#
|
8
|
+
# created on : 2014.12.18
|
9
|
+
# last update: 2014.12.18
|
10
|
+
#
|
11
|
+
# by meinside@gmail.com
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
|
15
|
+
require 'io/console'
|
16
|
+
|
17
|
+
require_relative '../../pushbullet'
|
18
|
+
|
19
|
+
class TestUsers < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def setup
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_users
|
26
|
+
print '> input your Pushbullet access token: '
|
27
|
+
input = STDIN.noecho(&:gets)
|
28
|
+
|
29
|
+
assert_not_nil(input)
|
30
|
+
access_token = input.chomp
|
31
|
+
|
32
|
+
Pushbullet.set_access_token(access_token)
|
33
|
+
|
34
|
+
# my info
|
35
|
+
info = Pushbullet::V2::Users.me
|
36
|
+
assert_not_nil(info)
|
37
|
+
|
38
|
+
# update my preference
|
39
|
+
test_flag = 'test_flag'
|
40
|
+
test_value = 'this is a flag for testing'
|
41
|
+
updated = Pushbullet::V2::Users.update_my_preferences({test_flag => test_value})
|
42
|
+
assert_not_nil(updated)
|
43
|
+
assert_equal(updated['preferences'][test_flag], test_value)
|
44
|
+
|
45
|
+
# rollback my preference
|
46
|
+
assert_not_nil(Pushbullet::V2::Users.update_my_preferences(info['preferences']))
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
# do nothing
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/lib/v2/users.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
# lib/v2/users.rb
|
4
|
+
#
|
5
|
+
# functions for Pushbullet users api
|
6
|
+
#
|
7
|
+
# created on : 2014.12.18
|
8
|
+
# last update: 2014.12.18
|
9
|
+
#
|
10
|
+
# by meinside@gmail.com
|
11
|
+
|
12
|
+
require 'json'
|
13
|
+
require 'mimemagic'
|
14
|
+
|
15
|
+
module Pushbullet
|
16
|
+
module V2
|
17
|
+
class Users
|
18
|
+
API_URL = 'https://api.pushbullet.com/v2/users'
|
19
|
+
|
20
|
+
public
|
21
|
+
|
22
|
+
# get my information
|
23
|
+
#
|
24
|
+
# @return [JSON] result as json
|
25
|
+
def self.me
|
26
|
+
result = Pushbullet::V2::request_get(API_URL + '/me', {})
|
27
|
+
|
28
|
+
JSON.parse(result.body)
|
29
|
+
end
|
30
|
+
|
31
|
+
# update my preferences
|
32
|
+
#
|
33
|
+
# @param preferences [Hash] values to overwrite
|
34
|
+
# @return [JSON] result as json
|
35
|
+
def self.update_my_preferences(preferences)
|
36
|
+
result = Pushbullet::V2::request(API_URL + '/me', {preferences: preferences})
|
37
|
+
|
38
|
+
JSON.parse(result.body)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pushbullet-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sungjin Han
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mimemagic
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.0'
|
27
|
+
description: Ruby library for Pushbullet's HTTP API (https://docs.pushbullet.com/http/)
|
28
|
+
email: meinside@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/pushbullet.rb
|
34
|
+
- lib/v2/contacts.rb
|
35
|
+
- lib/v2/devices.rb
|
36
|
+
- lib/v2/push.rb
|
37
|
+
- lib/v2/subscriptions.rb
|
38
|
+
- lib/v2/test/test_contacts.rb
|
39
|
+
- lib/v2/test/test_devices.rb
|
40
|
+
- lib/v2/test/test_push.rb
|
41
|
+
- lib/v2/test/test_subscriptions.rb
|
42
|
+
- lib/v2/test/test_users.rb
|
43
|
+
- lib/v2/users.rb
|
44
|
+
homepage: http://github.com/meinside/pushbullet-ruby
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.4.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Ruby library for Pushbullet
|
68
|
+
test_files: []
|