tweetkit 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,494 @@
1
+ module Conjunctions
2
+ def contains(*terms)
3
+ join(terms, connector: ' ')
4
+ end
5
+
6
+ def contains_one(*terms)
7
+ contains_one_of(*terms)
8
+ end
9
+
10
+ def contains_one_of(*terms)
11
+ join(terms, connector: ' OR ')
12
+ end
13
+
14
+ def without(*terms)
15
+ does_not_contain(*terms)
16
+ end
17
+
18
+ def does_not_contain(*terms)
19
+ join_with_negated_operator(terms)
20
+ end
21
+
22
+ def has(*terms)
23
+ join_with_operator(terms, connector: ' ', operator: 'has')
24
+ end
25
+
26
+ def has_one(*terms)
27
+ has_one_of(*terms)
28
+ end
29
+
30
+ def has_one_of(*terms)
31
+ join_with_operator(terms, connector: ' OR ', operator: 'has')
32
+ end
33
+
34
+ def has_no(*terms)
35
+ does_not_have(*terms)
36
+ end
37
+
38
+ def does_not_have(*terms)
39
+ join_with_operator(terms, connector: ' ', operator: '-has')
40
+ end
41
+
42
+ def is_a(*terms)
43
+ is(*terms)
44
+ end
45
+
46
+ def is(*terms)
47
+ join_with_operator(terms, connector: ' ', operator: 'is')
48
+ end
49
+
50
+ def is_one_of(*terms)
51
+ join_with_operator(terms, connector: ' OR ', operator: 'is')
52
+ end
53
+
54
+ def is_not(*terms)
55
+ join_with_operator(terms, connector: ' ', operator: '-is')
56
+ end
57
+
58
+ def from(*terms)
59
+ join_with_operator(terms, connector: ' ', operator: 'from')
60
+ end
61
+
62
+ def from_one(*terms)
63
+ from_one_of(*terms)
64
+ end
65
+
66
+ def from_one_of(*terms)
67
+ join_with_operator(terms, connector: ' OR ', operator: 'from')
68
+ end
69
+
70
+ def not_from(*terms)
71
+ is_not_from(*terms)
72
+ end
73
+
74
+ def is_not_from(*terms)
75
+ join_with_operator(terms, connector: ' ', operator: '-from')
76
+ end
77
+
78
+ def to(*terms)
79
+ join_with_operator(terms, connector: ' ', operator: 'to')
80
+ end
81
+
82
+ def to_one_of(*terms)
83
+ join_with_operator(terms, connector: ' OR ', operator: 'to')
84
+ end
85
+
86
+ def not_to(*terms)
87
+ is_not_to(*terms)
88
+ end
89
+
90
+ def is_not_to(*terms)
91
+ join_with_operator(terms, connector: ' ', operator: '-to')
92
+ end
93
+
94
+ def with_link(term)
95
+ with_url(term)
96
+ end
97
+
98
+ def with_url(term)
99
+ return "url:#{term}"
100
+ end
101
+
102
+ def with_links(*terms)
103
+ with_urls(*terms)
104
+ end
105
+
106
+ def with_urls(*terms)
107
+ join_with_operator(terms, connector: ' ', operator: 'url')
108
+ end
109
+
110
+ def with_one_of_links(*terms)
111
+ with_one_of_urls(*terms)
112
+ end
113
+
114
+ def with_one_of_urls(*terms)
115
+ join_with_operator(terms, connector: ' OR ', operator: 'url')
116
+ end
117
+
118
+ def without_link(term)
119
+ without_url(term)
120
+ end
121
+
122
+ def without_url(term)
123
+ return "-url:#{term}"
124
+ end
125
+
126
+ def without_links(*terms)
127
+ join_with_operator(terms, connector: ' ', operator: '-url')
128
+ end
129
+
130
+ def retweets_of(*terms)
131
+ join_with_operator(terms, connector: ' ', operator: 'retweets_of')
132
+ end
133
+
134
+ def retweets_of_one_of(*terms)
135
+ join_with_operator(terms, connector: ' OR ', operator: 'retweets_of')
136
+ end
137
+
138
+ def not_retweets_of(*terms)
139
+ are_not_retweets_of(*terms)
140
+ end
141
+
142
+ def are_not_retweets_of(*terms)
143
+ join_with_operator(terms, connector: ' ', operator: '-retweets_of')
144
+ end
145
+
146
+ def with_context(*terms)
147
+ context(*terms)
148
+ end
149
+
150
+ def context(*terms)
151
+ join_with_operator(terms, connector: ' ', operator: 'context')
152
+ end
153
+
154
+ def with_one_of_context(*terms)
155
+ one_of_context(*terms)
156
+ end
157
+
158
+ def one_of_context(*terms)
159
+ join_with_operator(terms, connector: ' OR ', operator: 'context')
160
+ end
161
+
162
+ def without_context(*terms)
163
+ not_with_context(*terms)
164
+ end
165
+
166
+ def not_with_context(*terms)
167
+ join_with_operator(terms, connector: ' ', operator: '-context')
168
+ end
169
+
170
+ def with_entity(*terms)
171
+ entities(*terms)
172
+ end
173
+
174
+ def with_entities(*terms)
175
+ entities(*terms)
176
+ end
177
+
178
+ def entity(*terms)
179
+ entities(*terms)
180
+ end
181
+
182
+ def entities(*terms)
183
+ join_with_operator(terms, connector: ' ', operator: 'entity')
184
+ end
185
+
186
+ def with_one_of_entity(*terms)
187
+ with_one_of_entities(*terms)
188
+ end
189
+
190
+ def with_one_of_entities(*terms)
191
+ join_with_operator(terms, connector: ' OR ', operator: 'entity')
192
+ end
193
+
194
+ def without_entity(*terms)
195
+ not_with_entities(*terms)
196
+ end
197
+
198
+ def without_entities(*terms)
199
+ not_with_entities(*terms)
200
+ end
201
+
202
+ def not_with_entity(*terms)
203
+ not_with_entities(*terms)
204
+ end
205
+
206
+ def not_with_entities(*terms)
207
+ join_with_operator(terms, connector: ' ', operator: '-entity')
208
+ end
209
+
210
+ def with_conversation_id(*terms)
211
+ conversation_ids(*terms)
212
+ end
213
+
214
+ def with_conversation_ids(*terms)
215
+ conversation_ids(*terms)
216
+ end
217
+
218
+ def conversation_id(*terms)
219
+ conversation_ids(*terms)
220
+ end
221
+
222
+ def conversation_ids(*terms)
223
+ join_with_operator(terms, connector: ' ', operator: 'conversation_id')
224
+ end
225
+
226
+ def with_one_of_conversation_id(*terms)
227
+ with_one_of_conversation_ids(*terms)
228
+ end
229
+
230
+ def with_one_of_conversation_ids(*terms)
231
+ join_with_operator(terms, connector: ' OR ', operator: 'conversation_id')
232
+ end
233
+
234
+ def without_conversation_id(*terms)
235
+ without_conversation_ids(*terms)
236
+ end
237
+
238
+ def without_conversation_ids(*terms)
239
+ join_with_operator(terms, connector: ' ', operator: '-conversation_id')
240
+ end
241
+
242
+ def location(*terms)
243
+ from_places(*terms)
244
+ end
245
+
246
+ def locations(*terms)
247
+ from_places(*terms)
248
+ end
249
+
250
+ def place(*terms)
251
+ from_places(*terms)
252
+ end
253
+
254
+ def places(*terms)
255
+ from_places(*terms)
256
+ end
257
+
258
+ def from_location(*terms)
259
+ from_places(*terms)
260
+ end
261
+
262
+ def from_locations(*terms)
263
+ from_places(*terms)
264
+ end
265
+
266
+ def from_place(*terms)
267
+ from_places(*terms)
268
+ end
269
+
270
+ def from_places(*terms)
271
+ join_with_operator(terms, connector: ' ', operator: 'place')
272
+ end
273
+
274
+ def from_one_of_location(*terms)
275
+ from_one_of_places(*terms)
276
+ end
277
+
278
+ def from_one_of_locations(*terms)
279
+ from_one_of_places(*terms)
280
+ end
281
+
282
+ def from_one_of_place(*terms)
283
+ from_one_of_places(*terms)
284
+ end
285
+
286
+ def from_one_of_places(*terms)
287
+ join_with_operator(terms, connector: ' OR ', operator: 'place')
288
+ end
289
+
290
+ def not_from_location(*terms)
291
+ not_from_places(*terms)
292
+ end
293
+
294
+ def not_from_place(*terms)
295
+ not_from_places(*terms)
296
+ end
297
+
298
+ def not_from_locations(*terms)
299
+ not_from_places(*terms)
300
+ end
301
+
302
+ def not_from_places(*terms)
303
+ join_with_operator(terms, connector: ' ', operator: '-place')
304
+ end
305
+
306
+ def country(*terms)
307
+ from_countries(*terms)
308
+ end
309
+
310
+ def countries(*terms)
311
+ from_countries(*terms)
312
+ end
313
+
314
+ def from_country(*terms)
315
+ from_countries(*terms)
316
+ end
317
+
318
+ def from_countries(*terms)
319
+ join_with_operator(terms, connector: ' ', operator: 'place_country')
320
+ end
321
+
322
+ def from_one_of_country(*terms)
323
+ from_one_of_countries(*terms)
324
+ end
325
+
326
+ def from_one_of_countries(*terms)
327
+ join_with_operator(terms, connector: ' OR ', operator: 'place_country')
328
+ end
329
+
330
+ def not_from_country(*terms)
331
+ not_from_countries(*terms)
332
+ end
333
+
334
+ def not_from_countries(*terms)
335
+ join_with_operator(terms, connector: ' ', operator: '-place_country')
336
+ end
337
+
338
+ def lang(*terms)
339
+ language(*terms)
340
+ end
341
+
342
+ def language(*terms)
343
+ join_with_operator(terms, connector: ' ', operator: 'lang')
344
+ end
345
+
346
+ def not_lang(*terms)
347
+ not_language(*terms)
348
+ end
349
+
350
+ def not_language(*terms)
351
+ join_with_operator(terms, connector: ' ', operator: '-lang')
352
+ end
353
+
354
+ def join(terms, connector:, **opts)
355
+ unless terms.empty?
356
+ terms = terms.collect do |term|
357
+ term = term.to_s
358
+ term = term.strip
359
+ if term.split.length > 1
360
+ "\"#{term}\""
361
+ else
362
+ term
363
+ end
364
+ end
365
+ terms = terms.join(connector)
366
+ append_to_current_query(terms)
367
+ terms
368
+ end
369
+ end
370
+
371
+ def join_with_operator(terms, connector:, operator:, **opts)
372
+ unless terms.empty?
373
+ terms = terms.collect do |term|
374
+ term = term.to_s
375
+ term = term.strip
376
+ if term.split.length > 1
377
+ "\"#{term}\""
378
+ else
379
+ term
380
+ end
381
+ end
382
+ terms = terms.collect { |term| "#{operator}:#{term}" }
383
+ terms = terms.join(connector)
384
+ append_to_current_query(terms)
385
+ terms
386
+ end
387
+ end
388
+
389
+ def join_with_negated_operator(terms, **opts)
390
+ unless terms.empty?
391
+ terms = terms.collect do |term|
392
+ term = term.to_s
393
+ term = term.strip
394
+ if term.split.length > 1
395
+ "\"#{term}\""
396
+ else
397
+ term
398
+ end
399
+ end
400
+ terms = terms.collect { |term| "-#{term}" }
401
+ terms = terms.join(' ')
402
+ append_to_current_query(terms)
403
+ terms
404
+ end
405
+ end
406
+
407
+ def append_to_current_query(term)
408
+ if @current_query
409
+ @current_query += " #{term}"
410
+ else
411
+ @current_query = term
412
+ end
413
+ end
414
+
415
+ def group(&block)
416
+ Conjunctions.alias_method :original_join, :join
417
+ Conjunctions.alias_method :original_join_with_operator, :join_with_operator
418
+ Conjunctions.alias_method :original_join_with_negated_operator, :join_with_negated_operator
419
+
420
+ def join(terms, connector:, **opts)
421
+ unless terms.empty?
422
+ terms = terms.collect do |term|
423
+ term = term.to_s
424
+ term = term.strip
425
+ if term.split.length > 1
426
+ "\"#{term}\""
427
+ else
428
+ term
429
+ end
430
+ end
431
+ terms = terms.join(connector)
432
+ group_terms(terms)
433
+ end
434
+ end
435
+
436
+ def join_with_operator(terms, connector:, operator:, **opts)
437
+ unless terms.empty?
438
+ terms = terms.collect do |term|
439
+ term = term.to_s
440
+ term = term.strip
441
+ if term.split.length > 1
442
+ "\"#{term}\""
443
+ else
444
+ term
445
+ end
446
+ end
447
+ terms = terms.collect { |term| "#{operator}:#{term}" }
448
+ terms = terms.join(connector)
449
+ group_terms(terms)
450
+ end
451
+ end
452
+
453
+ def join_with_negated_operator(terms, **opts)
454
+ unless terms.empty?
455
+ terms = terms.collect do |term|
456
+ term = term.to_s
457
+ term = term.strip
458
+ if term.split.length > 1
459
+ "\"#{term}\""
460
+ else
461
+ term
462
+ end
463
+ end
464
+ terms = terms.collect { |term| "-#{term}" }
465
+ terms = terms.join(' ')
466
+ group_terms(terms)
467
+ end
468
+ end
469
+
470
+ def group_terms(term)
471
+ if @grouped_terms
472
+ @grouped_terms << term
473
+ else
474
+ @grouped_terms = []
475
+ @grouped_terms << term
476
+ end
477
+ end
478
+
479
+ def combine_terms
480
+ @combined_grouped_terms = @grouped_terms.join(' ')
481
+ @combined_grouped_terms = "(#{@combined_grouped_terms})"
482
+ append_to_current_query(@combined_grouped_terms)
483
+ end
484
+
485
+ instance_eval(&block)
486
+ combine_terms
487
+
488
+ Conjunctions.alias_method :join, :original_join
489
+ Conjunctions.alias_method :join_with_operator, :original_join_with_operator
490
+ Conjunctions.alias_method :join_with_negated_operator, :original_join_with_negated_operator
491
+
492
+ @combined_grouped_terms
493
+ end
494
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'conjunctions';
4
+
5
+ module Tweetkit
6
+ class Search
7
+ include Conjunctions
8
+
9
+ attr_accessor :current_query
10
+
11
+ def initialize(term)
12
+ @current_query = term
13
+ end
14
+
15
+ def opts
16
+ @opts ||= {}
17
+ end
18
+
19
+ def evaluate(&block)
20
+ instance_eval(&block)
21
+ end
22
+ end
23
+ end
@@ -1,10 +1,10 @@
1
- require 'tweetkit/search'
1
+ require_relative 'search/search'
2
2
 
3
3
  module Tweetkit
4
4
  class Client
5
5
  module Tweets
6
6
  def tweet(id, **options)
7
- get "tweets/#{id}", options
7
+ get "tweets/#{id}", **options
8
8
  end
9
9
 
10
10
  def tweets(ids, **options)
@@ -13,13 +13,21 @@ module Tweetkit
13
13
  else
14
14
  ids = ids.delete(' ')
15
15
  end
16
- get 'tweets', options.merge!({ ids: ids })
16
+ get 'tweets', **options.merge!({ ids: ids })
17
17
  end
18
18
 
19
19
  def search(query = '', type: :tweet, **options, &block)
20
20
  search = Search.new(query)
21
- search.setup(&block) if block_given?
22
- get 'tweets/search/recent', options.merge!({ query: search.combined_query })
21
+ search.evaluate(&block) if block_given?
22
+ get 'tweets/search/recent', **options.merge!({ query: search.current_query })
23
+ end
24
+
25
+ def post_tweet(**options)
26
+ post "tweets", **options
27
+ end
28
+
29
+ def delete_tweet(id)
30
+ delete "tweets/#{id}"
23
31
  end
24
32
  end
25
33
  end
@@ -1,4 +1,3 @@
1
- require 'tweetkit/configurable'
2
1
  require 'tweetkit/connection'
3
2
  require 'tweetkit/client/tweets'
4
3