Skip to content

Commit

Permalink
Tweaks to #245
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 27, 2012
1 parent 32375d9 commit 995d8ac
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 113 deletions.
2 changes: 1 addition & 1 deletion lib/twitter/entity/user_mention.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Twitter
class Entity::UserMention < Twitter::Entity
lazy_attr_reader :screen_name, :name, :id, :id_str
lazy_attr_reader :id, :name, :screen_name
end
end
91 changes: 43 additions & 48 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
require 'twitter/base'
require 'twitter/client'
require 'twitter/creatable'
require 'twitter/entity/hashtag'
require 'twitter/entity/url'
require 'twitter/entity/user_mention'
require 'twitter/geo_factory'
require 'twitter/media_factory'
require 'twitter/metadata'
require 'twitter/oembed'
require 'twitter/place'
require 'twitter/user'
require 'twitter/entity/url'
require 'twitter/entity/user_mention'
require 'twitter/entity/hashtag'

module Twitter
class Status < Twitter::Base
class NoEntityError < StandardError
def initialize
super("Entities are not available. Pass :include_entities => true on fetching the Twitter::Status object")
end
end

include Twitter::Creatable
lazy_attr_reader :favorited, :from_user, :from_user_id, :from_user_name, :id,
Expand All @@ -36,91 +31,91 @@ def ==(other)
super || (other.class == self.class && other.id == self.id)
end

# @note Must :include_entities in your request for this method to work
# @note Must include entities in your request for this method to work
# @return [Array<String>]
def expanded_urls
@expanded_urls ||= Array(@attrs['entities']['urls']).map do |url|
url['expanded_url']
end unless @attrs['entities'].nil?
warn "#{Kernel.caller.first}: [DEPRECATION] Twitter::Status#expanded_urls it deprecated. Use Twitter::Status#urls instead."
urls.map(&:expanded_url) unless urls.nil?
end

# @return [Twitter::Point, Twitter::Polygon]
def geo
@geo ||= Twitter::GeoFactory.new(@attrs['geo']) unless @attrs['geo'].nil?
end

# @note Must :include_entities in your request for this method to work
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Hashtag>]
def hashtags
@hashtags ||= unless @attrs['entities'].nil?
Array(@attrs['entities']['hashtags']).map do |hashtag|
Twitter::Entity::Hashtag.new(hashtag)
end
else
warn "#{Kernel.caller.first}: To get hashtags, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end

# @note Must include entities in your request for this method to work
# @return [Array]
def media
@media ||= Array(@attrs['entities']['media']).map do |media|
Twitter::MediaFactory.new(media)
end unless @attrs['entities'].nil?
@media ||= unless @attrs['entities'].nil?
Array(@attrs['entities']['media']).map do |media|
Twitter::MediaFactory.new(media)
end
else
warn "#{Kernel.caller.first}: To get media, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end

# @return [Twitter::Metadata]
def metadata
@metadata ||= Twitter::Metadata.new(@attrs['metadata']) unless @attrs['metadata'].nil?
end

# @return [Twitter::OEmbed]
def oembed(options={})
@client ||= Twitter::Client.new
@client.oembed(@attrs['id'], options) unless @attrs['id'].nil?
end

# @return [Twitter::Place]
def place
@place ||= Twitter::Place.new(@attrs['place']) unless @attrs['place'].nil?
end

# @return [Twitter::User]
def user
@user ||= Twitter::User.new(@attrs.dup['user'].merge('status' => @attrs.except('user'))) unless @attrs['user'].nil?
end

# If this status is itself a retweet, the original tweet is available here.
#
# @return [Twitter::Status]
def retweeted_status
@retweeted_status ||= self.class.new(@attrs['retweeted_status']) unless @attrs['retweeted_status'].nil?
end

# @return [Twitter::OEmbed]
def oembed(options={})
@client ||= Twitter::Client.new
@client.oembed(@attrs['id'], options) unless @attrs['id'].nil?
end

# @note Must :include_entities in your request for this method to work
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Url>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def urls
@urls ||= if @attrs['entities'].nil?
raise NoEntityError
else
@urls ||= unless @attrs['entities'].nil?
Array(@attrs['entities']['urls']).map do |url|
Twitter::Entity::Url.new(url)
end
else
warn "#{Kernel.caller.first}: To get URLs, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end

# @note Must :include_entities in your request for this method to work
# @return [Array<Twitter::Entity::Hashtag>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def hashtags
@hashtags ||= if @attrs['entities'].nil?
raise NoEntityError
else
Array(@attrs['entities']['hashtags']).map do |hashtag|
Twitter::Entity::Hashtag.new(hashtag)
end
end
# @return [Twitter::User]
def user
@user ||= Twitter::User.new(@attrs.dup['user'].merge('status' => @attrs.except('user'))) unless @attrs['user'].nil?
end

# @note Must :include_entities in your request for this method to work
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::UserMention>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def user_mentions
@user_mentions ||= if @attrs['entities'].nil?
raise NoEntityError
else
@user_mentions ||= unless @attrs['entities'].nil?
Array(@attrs['entities']['user_mentions']).map do |user_mention|
Twitter::Entity::UserMention.new(user_mention)
end
else
warn "#{Kernel.caller.first}: To get user mentions, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end

Expand Down
Loading

0 comments on commit 995d8ac

Please sign in to comment.