Sha256: 3c25f9b2e216bd5d71c52a79a7e6ce843893c29ef558b6ade85be76a9e00c23e

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

module Onebox
  module Engine
    class TwitterStatusOnebox
      include Engine
      include LayoutSupport
      include HTML

      matches do
        http
        maybe("www.")
        domain("twitter")
        tld("com")
        anything
        has("/status/")
      end

      private

      def match
        @match ||= @url.match(%r{twitter\.com/.+?/status/(?<id>\d+)})
      end

      def client
        Onebox.options.twitter_client
      end

      def raw
        if client
          @raw ||= OpenStruct.new(client.status(match[:id]).to_hash)
        else
          super
        end
      end

      def access(*keys)
        keys.reduce(raw) do |memo, key|
          memo[key] || memo[key.to_s]
        end
      end

      def tweet
        if raw.html?
          raw.css(".tweet-text")[0].inner_text
        else
          access(:text)
        end
      end

      def timestamp
        if raw.html?
          raw.css(".metadata span").inner_text
        else
          access(:created_at)
        end
      end

      def title
        if raw.html?
          raw.css(".stream-item-header .username").inner_text
        else
          access(:user, :screen_name)
        end
      end

      def avatar
        if raw.html?
          raw.css(".avatar")[2]["src"]
        else
          access(:user, :profile_image_url)
        end
      end

      def favorites
        if raw.html?
          raw.css(".stats li .request-favorited-popup").inner_text
        else
          access(:favorite_count)
        end
      end

      def retweets
        if raw.html?
          raw.css(".stats li .request-retweeted-popup").inner_text
        else
          access(:retweet_count)
        end
      end

      def data
        { link: link,
          tweet: tweet,
          timestamp: timestamp,
          title: title,
          avatar: avatar,
          favorites: favorites,
          retweets: retweets }
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
onebox-1.3.8 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.7 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.6 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.5 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.4 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.3 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.2 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.1 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.3.0 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.9 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.8 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.7 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.6 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.5 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.4 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.3 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.2 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.1 lib/onebox/engine/twitter_status_onebox.rb
onebox-1.2.0 lib/onebox/engine/twitter_status_onebox.rb