Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 150 lines (127 sloc) 4.215 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
require 'twitter/creatable'
require 'twitter/exceptable'
require 'twitter/identity'

module Twitter
  class Tweet < Twitter::Identity
    include Twitter::Creatable
    include Twitter::Exceptable
    attr_reader :favorited, :favoriters, :from_user_id, :from_user_name,
      :in_reply_to_screen_name, :in_reply_to_attrs_id, :in_reply_to_status_id,
      :in_reply_to_user_id, :iso_language_code, :profile_image_url,
      :profile_image_url_https, :repliers, :retweeted, :retweeters, :source,
      :text, :to_user, :to_user_id, :to_user_name, :truncated
    alias in_reply_to_tweet_id in_reply_to_status_id
    alias favourited favorited
    alias favourited? favorited?
    alias favouriters favoriters

    # @return [Boolean]
    def entities?
      !@attrs[:entities].nil?
    end

    # @return [Integer]
    def favoriters_count
      favoriters_count = @attrs[:favoriters_count]
      favoriters_count.to_i if favoriters_count
    end
    alias favorite_count favoriters_count
    alias favorites_count favoriters_count
    alias favourite_count favoriters_count
    alias favourites_count favoriters_count
    alias favouriters_count favoriters_count

    # @return [String]
    def from_user
      @attrs[:from_user] || user && user.screen_name
    end

    # @return [String]
    # @note May be > 140 characters.
    def full_text
      if retweeted_status
        prefix = text[/\A(RT @[a-z0-9_]{1,20}: )/i, 1]
        [prefix, retweeted_status.text].compact.join
      else
        text
      end
    end

    # @return [Twitter::Geo]
    def geo
      @geo ||= Twitter::GeoFactory.fetch_or_new(@attrs[:geo])
    end

    # @note Must include entities in your request for this method to work
    # @return [Array<Twitter::Entity::Hashtag>]
    def hashtags
      @hashtags ||= entities(Twitter::Entity::Hashtag, :hashtags)
    end

    # @note Must include entities in your request for this method to work
    # @return [Array<Twitter::Media>]
    def media
      @media ||= entities(Twitter::MediaFactory, :media)
    end

    # @return [Twitter::Metadata]
    def metadata
      @metadata ||= Twitter::Metadata.fetch_or_new(@attrs[:metadata])
    end

    # @return [Twitter::Place]
    def place
      @place ||= Twitter::Place.fetch_or_new(@attrs[:place])
    end

    # @return [Integer]
    def repliers_count
      repliers_count = @attrs[:repliers_count]
      repliers_count.to_i if repliers_count
    end
    alias reply_count repliers_count

    # @return [Boolean]
    def reply?
      !!in_reply_to_status_id
    end

    # @return [Boolean]
    def retweet?
      !!retweeted_status
    end

    # If this Tweet is a retweet, the original Tweet is available here.
    #
    # @return [Twitter::Tweet]
    def retweeted_status
      @retweeted_status ||= self.class.fetch_or_new(@attrs[:retweeted_status])
    end
    alias retweeted_tweet retweeted_status
    alias retweet retweeted_status

    # @return [String]
    def retweeters_count
      retweeters_count = (@attrs[:retweet_count] || @attrs[:retweeters_count])
      retweeters_count.to_i if retweeters_count
    end
    alias retweet_count retweeters_count

    # @note Must include entities in your request for this method to work
    # @return [Array<Twitter::Entity::Url>]
    def urls
      @urls ||= entities(Twitter::Entity::Url, :urls)
    end

    # @return [Twitter::User]
    def user
      @user ||= fetch_or_new_without_self(Twitter::User, @attrs, :user, :status)
    end

    # @note Must include entities in your request for this method to work
    # @return [Array<Twitter::Entity::UserMention>]
    def user_mentions
      @user_mentions ||= entities(Twitter::Entity::UserMention, :user_mentions)
    end

    def user?
      !@attrs[:user].nil?
    end

  private

    # @param klass [Class]
    # @param key [Symbol]
    def entities(klass, key)
      if entities?
        Array(@attrs[:entities][key.to_sym]).map do |user_mention|
          klass.fetch_or_new(user_mention)
        end
      else
        warn "#{Kernel.caller.first}: To get #{key.to_s.tr('_', ' ')}, you must pass `:include_entities => true` when requesting the #{self.class.name}."
        []
      end
    end

  end

  Status = Tweet
end
Something went wrong with that request. Please try again.