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



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

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



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

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

.twitter_embed(tweet) ⇒ Object



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

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
  return [url, title]
end