gsruby 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gsruby.rb +291 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23ff2baa1d370618ac40b8b963463f79f7607e57
4
+ data.tar.gz: e915f680237f1720f51d293abed3ba378c2daa06
5
+ SHA512:
6
+ metadata.gz: 8e419837ad81682154c8574813ff3e235efa7f84113c475b406fb58207a3bbab3c5329bd2da342fb598807d28745d71b5d28c4d0f0f363729cdf2095148a8af0
7
+ data.tar.gz: 51935ab5b5916fd96ebf0480cbdbdbf58868ca1bae70a79307e3e3320b10a15536349e54e59797a618a26140c229b8e2f6a89c5aff3e7241198a0664c0aaf222
@@ -0,0 +1,291 @@
1
+ =begin
2
+ This file is part of GSRuby.
3
+
4
+ GSRuby is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ GSRuby is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with GSRuby. If not, see <http://www.gnu.org/licenses/>.
16
+ =end
17
+
18
+ require 'net/https'
19
+ require 'openssl'
20
+ require 'json'
21
+
22
+ module GNUSocial
23
+
24
+ # This class manages calls to GNU Social API
25
+ class Client
26
+
27
+ # Creates and configure a Client for make calls to GNU Social API.
28
+ #
29
+ # Arguments:
30
+ # node: GNU Social instance
31
+ # user: Username
32
+ # passwd: Password
33
+ #
34
+ # Optional:
35
+ # ssl: Whether to use SSL, default false
36
+ # ssl_insecure: If is true, SSL cert will not be verified
37
+ # proxy_host: Proxy url used for the connection
38
+ # proxy_port: Proxy port used for the connection
39
+ # source: This is used for identify your app, default 'api'
40
+ def initialize(node, user, passwd, options = {})
41
+ node = URI(node)
42
+ if options[:proxy_host] && options[:proxy_port]
43
+ proxy = [options[:proxy_host],options[:proxy_port]]
44
+ end
45
+ @node = Net::HTTP.new(node.hostname,node.port,*proxy)
46
+ @user = user
47
+ @passwd = passwd
48
+ @source = options[:source] ? options[:source] : 'api'
49
+ @node.use_ssl = true if options[:ssl]
50
+ @node.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:ssl_insecure]
51
+ end
52
+
53
+ # Makes a request to the GNU Social instance.
54
+ #
55
+ # Arguments:
56
+ # path: Path to do the connection
57
+ # params: A hash containing additional parameters
58
+ def connect(path, params)
59
+ request = Net::HTTP::Post.new(path)
60
+ request.basic_auth(@user,@passwd)
61
+ request.set_form_data(params)
62
+ res = @node.request(request)
63
+ end
64
+
65
+ # Makes a request to GNU Social instance and returns a Timeline object.
66
+ #
67
+ # Arguments:
68
+ # path: Path to do the connection
69
+ # options: A hash containing additional parameters, that can be:
70
+ # since: Notice ID (integer). Request will return newer notices than the specified
71
+ # before: Notice ID (integer). Request will return older notices than the specified
72
+ # count: Integer. Number of notices that will be returned
73
+ # page: Number of page that will be returned, 1 page == 20 notices
74
+ def get_timeline(path, options = {})
75
+ params = {}
76
+ params[:since_id] = options[:since] if options[:since]
77
+ params[:max_id] = options[:before] if options[:before]
78
+ params[:count] = options[:count] if options[:count]
79
+ params[:page] = options[:page] if options[:page]
80
+ Timeline.from_json(connect(path,params).body)
81
+ end
82
+
83
+ # Gets the instance Public Timeline.
84
+ #
85
+ # Arguments:
86
+ # options: Check get_timeline options
87
+ def public_timeline(options = {})
88
+ get_timeline('/api/statuses/public_timeline.json',options)
89
+ end
90
+
91
+ # Gets the user's Home Timeline.
92
+ #
93
+ # Arguments:
94
+ # options: Check get_timeline options
95
+ def home_timeline(options = {})
96
+ get_timeline('/api/statuses/home_timeline.json',options)
97
+ end
98
+
99
+ # Gets the user's mentions.
100
+ #
101
+ # Arguments:
102
+ # options: Check get_timeline options
103
+ def mentions(options = {})
104
+ get_timeline('/api/statuses/mentions.json',options)
105
+ end
106
+
107
+ #def user_timeline(user, options = {})
108
+ #end
109
+
110
+ # Gets the full conversation of the given Notice.
111
+ #
112
+ # Arguments:
113
+ # notice: NoticeID (integer) or Notice object
114
+ # options: Check get_timeline options
115
+ def conversation(notice, options = {})
116
+ id = get_id(notice)
117
+ get_timeline("/api/statusnet/conversation/#{id}.json",options)
118
+ end
119
+
120
+ #Returns the notice with the given ID.
121
+ #
122
+ # Arguments:
123
+ # id: ID of the notice to be returned
124
+ def notice(id)
125
+ res = @node.get("/api/statuses/show/#{id.to_s}.json")
126
+ Notice.new(JSON.parse(res.body))
127
+ end
128
+
129
+ # Publish a new status.
130
+ #
131
+ # Arguments:
132
+ # msg: Message to be published
133
+ # options: A hash containing additional parameters, that can be:
134
+ # reply: Publish as reply of the given NoticeID (integer) or Notice object
135
+ # source: Change the source of the status, default is source property
136
+ def send_status(msg, options = {})
137
+ params = {:status => msg}
138
+ params[:in_reply_to_status_id] = options[:reply] if options[:reply]
139
+ params[:source] = options[:source] ? options[:source] : @source
140
+ connect('/api/statuses/update.json',params)
141
+ end
142
+
143
+ # Return the ID of the given notice.
144
+ def get_id(notice)
145
+ if notice.is_a? Fixnum
146
+ return notice
147
+ elsif notice.is_a? Notice
148
+ return notice.id
149
+ else
150
+ raise "Must be a integer or a Notice"
151
+ end
152
+ end
153
+
154
+ # Publish a reply to the given Notice.
155
+ #
156
+ # Arguments:
157
+ # msg: Message to be published
158
+ # notice: NoticeID (integer) or Notice object
159
+ # options: Check send_status options
160
+ def reply(msg, notice, options = {})
161
+ options[:reply] = get_id(notice)
162
+ send_status(msg,options)
163
+ end
164
+
165
+ # Marks the given notice as favorite.
166
+ #
167
+ # Arguments:
168
+ # notice: Notice to be marked as fav
169
+ def fav(notice)
170
+ id = get_id(notice)
171
+ params = {:id => id}
172
+ connect("/api/favorites/create/#{id}.json",params)
173
+ end
174
+
175
+ # Repeat the given notice.
176
+ #
177
+ # Arguments:
178
+ # notice: Notice to be repeated
179
+ # options: A hash containing additional parameters, that can be:
180
+ # source: Change the source of the status, default is source property
181
+ def repeat(notice, options = {})
182
+ id = get_id(notice)
183
+ params = {:id => id}
184
+ params[:source] = options[:source] ? options[:source] : @source
185
+ connect("/api/statuses/retweet/#{id}.json",params)
186
+ end
187
+
188
+ def delete(notice)
189
+ connect('',params)
190
+ end
191
+
192
+ end
193
+
194
+ # Represents Timeline or group of notices.
195
+ class Timeline
196
+
197
+ # Creates a Timeline object from JSON response.
198
+ def self.from_json(json)
199
+ notices = []
200
+ JSON.parse(json).each do |notice|
201
+ notices.push(Notice.new(notice))
202
+ end
203
+ Timeline.new(notices)
204
+ end
205
+
206
+ # Creates a new Timeline object
207
+ def initialize(notices)
208
+ @notices = notices
209
+ end
210
+
211
+ # Return the notice with the given index on this timeline.
212
+ def notice(index)
213
+ return 'Index must be a integer' unless index.is_a?(Fixnum)
214
+ (0..size-1).cover?(index) ? @notices[index] : 'Bad index'
215
+ end
216
+
217
+ # Returns the number of notices on this timeline.
218
+ def size
219
+ @notices.size
220
+ end
221
+
222
+ # Return the most recent notice on this timeline.
223
+ def newest_notice
224
+ @notices[0]
225
+ end
226
+
227
+ # Returns the less recent notice on this timeline.
228
+ def oldest_notice
229
+ @notices[-1]
230
+ end
231
+
232
+ # Calls the given block for each notice on this timeline.
233
+ def notices
234
+ return "Need a block" unless block_given?
235
+ @notices.each do |n|
236
+ yield(n)
237
+ end
238
+ end
239
+
240
+ end
241
+
242
+ # Represents a status or activity
243
+ class Notice
244
+
245
+ attr_reader :text, :id, :created_at, :uri, :source
246
+
247
+ # Parses and create a new Notice object from data
248
+ def initialize(data)
249
+ data.each do |key, value|
250
+ case key
251
+ when "created_at" then set_time(value)
252
+ else instance_variable_set(:"@#{key}",value)
253
+ end
254
+ end
255
+ end
256
+
257
+ def to_s
258
+ "#{@text} by #{@user["name"]} at #{@created_at} ID [#{@id}]"
259
+ end
260
+
261
+ def set_time(at)
262
+ /\w{3} (?<mo>\w{3}) (?<d>\d+) (?<h>\d+):(?<m>\d+):(?<s>\d+) \+\d+ (?<y>\d+)/ =~ at
263
+ @created_at = Time.utc(y,mo,d,h,m,s).getlocal
264
+ end
265
+
266
+ def author
267
+ @user["name"]
268
+ end
269
+
270
+ def is_reply?
271
+ @in_reply_to_status_id != nil
272
+ end
273
+
274
+ def reply_to
275
+ @in_reply_to_status_id
276
+ end
277
+
278
+ def reply_to_name
279
+ @in_reply_to_screen_name
280
+ end
281
+
282
+ def reply_to_userid
283
+ @in_reply_to_user_id
284
+ end
285
+
286
+ def html
287
+ @statusnet_html
288
+ end
289
+
290
+ end
291
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gsruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Raito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: GNU Social for Ruby. A simple library for interact with GNU Social.
14
+ email: raito@openmailbox.org
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/gsruby.rb
20
+ homepage: https://notabug.org/raito/gsruby
21
+ licenses:
22
+ - GPL 3
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Simple GNU Social library
44
+ test_files: []