Class: SL::TwitterSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/searches/twitter.rb

Class Method Summary collapse

Class Method Details

.search(_search_type, search_terms, link_text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/searchlink/searches/twitter.rb', line 15

def search(_search_type, search_terms, link_text)
  if SL::URL.url?(search_terms) && search_terms =~ %r{^https://twitter.com/}
    url, title = twitter_embed(search_terms)
  else
    SL.add_error('Invalid Tweet URL', "#{search_terms} is not a valid link to a tweet or timeline")
    url = false
    title = false
  end

  [url, title, link_text]
end

.settingsObject



6
7
8
9
10
11
12
13
# File 'lib/searchlink/searches/twitter.rb', line 6

def settings
  {
    trigger: 'te',
    searches: [
      ['te', 'Twitter Embed']
    ]
  }
end

.twitter_embed(tweet) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/searchlink/searches/twitter.rb', line 27

def twitter_embed(tweet)
  res = `curl -sSL 'https://publish.twitter.com/oembed?url=#{tweet.url_encode}'`.strip
  if res
    begin
      json = JSON.parse(res)
      url = 'embed'
      title = json['html']
    rescue StandardError
      SL.add_error('Tweet Error', 'Error retrieving tweet')
      url = false
      title = tweet
    end
  else
    return [false, 'Error retrieving tweet']
  end
  [url, title]
end