Class: TwitterApi

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_api.rb

Overview

lightweight Twitter api calls

Constant Summary collapse

BASE_URL =
"https://api.twitter.com"
URL_PARAMS =
%w[
  tweet.fields=id,author_id,text,created_at,entities,referenced_tweets,public_metrics
  user.fields=id,name,username,profile_image_url
  media.fields=type,height,width,variants,preview_image_url,url
  expansions=attachments.media_keys,referenced_tweets.id.author_id
]

Class Method Summary collapse

Class Method Details

.prettify_tweet(tweet) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/twitter_api.rb', line 14

def prettify_tweet(tweet)
  text = tweet[:data][:text].dup.to_s
  if (entities = tweet[:data][:entities]) && (urls = entities[:urls])
    urls.each do |url|
      if !url[:display_url].start_with?("pic.twitter.com")
        text.gsub!(
          url[:url],
          "<a target='_blank' href='#{url[:expanded_url]}'>#{url[:display_url]}</a>",
        )
      else
        text.gsub!(url[:url], "")
      end
    end
  end
  text = link_hashtags_in link_handles_in text
  result = Rinku.auto_link(text, :all, 'target="_blank"').to_s

  if tweet[:includes] && media = tweet[:includes][:media]
    media.each do |m|
      if m[:type] == "photo"
        result << "<div class='tweet-images'><img class='tweet-image' src='#{m[:url]}' width='#{m[:width]}' height='#{m[:height]}'></div>"
      elsif m[:type] == "video" || m[:type] == "animated_gif"
        video_to_display =
          m[:variants]
            .select { |v| v[:content_type] == "video/mp4" }
            .sort { |v| v[:bit_rate] }
            .last # choose highest bitrate

        if video_to_display && url = video_to_display[:url]
          width = m[:width]
          height = m[:height]

          attributes =
            if m[:type] == "animated_gif"
              %w[playsinline loop muted autoplay disableRemotePlayback disablePictureInPicture]
            else
              %w[controls playsinline]
            end.join(" ")

          result << <<~HTML
            <div class='tweet-images'>
              <div class='aspect-image-full-size' style='--aspect-ratio:#{width}/#{height};'>
                <video class='tweet-video' #{attributes}
                  width='#{width}'
                  height='#{height}'
                  poster='#{m[:preview_image_url]}'>
                  <source src='#{url}' type="video/mp4">
                </video>
              </div>
            </div>
          HTML
        end
      end
    end
  end

  result
end

.tweet_for(id) ⇒ Object Also known as: status



73
74
75
# File 'lib/twitter_api.rb', line 73

def tweet_for(id)
  JSON.parse(twitter_get(tweet_uri_for(id)))
end

.twitter_credentials_missing?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/twitter_api.rb', line 78

def twitter_credentials_missing?
  consumer_key.blank? || consumer_secret.blank?
end