Permalink
Browse files

Refactor common code into Twitter::Exceptable module

  • Loading branch information...
1 parent 221756a commit 02bf97e0cbc7f6dd0979901b57dd9530ffcca87f @sferik committed Nov 27, 2012
Showing with 34 additions and 13 deletions.
  1. +10 −0 lib/twitter/exceptable.rb
  2. +12 −12 lib/twitter/tweet.rb
  3. +1 −1 lib/twitter/user.rb
  4. +11 −0 spec/twitter/tweet_spec.rb
View
@@ -3,6 +3,16 @@ module Exceptable
# Return a hash that includes everything but the given keys.
#
+ # @param klass [Class]
+ # @param hash [Hash]
+ # @param key1 [Symbol]
+ # @param key2 [Symbol]
+ def fetch_or_new_without_self(klass, hash, key1, key2)
+ klass.fetch_or_new(hash.dup[key1].merge(key2 => except(hash, key1))) unless hash[key1].nil?
+ end
+
+ # Return a hash that includes everything but the given keys.
+ #
# @param hash [Hash]
# @param key [Symbol]
# @return [Hash]
View
@@ -27,6 +27,11 @@ class Tweet < Twitter::Identity
alias retweeted? retweeted
alias truncated? truncated
+ # @return [Boolean]
+ def entities?
+ !@attrs[:entities].nil?
+ end
+
# @return [Integer]
def favoriters_count
favoriters_count = @attrs[:favoriters_count]
@@ -122,7 +127,7 @@ def urls
# @return [Twitter::User]
def user
- @user ||= Twitter::User.fetch_or_new(@attrs.dup[:user].merge(:status => except(@attrs, :user))) if 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
@@ -131,30 +136,25 @@ def user_mentions
@user_mentions ||= entities(Twitter::Entity::UserMention, :user_mentions)
end
- # @return [Boolean]
- def entities?
- !@attrs[:entities].nil?
+ def user?
+ !@attrs[:user].nil?
end
private
# @param klass [Class]
- # @param method [Symbol]
- def entities(klass, method)
+ # @param key [Symbol]
+ def entities(klass, key)
if entities?
- Array(@attrs[:entities][method.to_sym]).map do |user_mention|
+ Array(@attrs[:entities][key.to_sym]).map do |user_mention|
klass.fetch_or_new(user_mention)
end
else
- warn "#{Kernel.caller.first}: To get #{method.to_s.tr('_', ' ')}, you must pass `:include_entities => true` when requesting the #{self.class.name}."
+ warn "#{Kernel.caller.first}: To get #{key.to_s.tr('_', ' ')}, you must pass `:include_entities => true` when requesting the #{self.class.name}."
[]
end
end
- def user?
- !@attrs[:user].nil?
- end
-
end
Status = Tweet
View
@@ -94,7 +94,7 @@ def profile_image_url?
# @return [Twitter::Tweet]
def status
- @status ||= Twitter::Tweet.fetch_or_new(@attrs.dup[:status].merge(:user => except(@attrs, :status))) if status?
+ @status ||= fetch_or_new_without_self(Twitter::Tweet, @attrs, :status, :user)
end
def status?
View
@@ -288,6 +288,17 @@
end
end
+ describe "#user?" do
+ it "returns true when status is set" do
+ user = Twitter::Tweet.new(:id => 28669546014, :user => {:id => 7505382}).user?
+ expect(user).to be_true
+ end
+ it "returns false when status is not set" do
+ user = Twitter::Tweet.new(:id => 28669546014).user?
+ expect(user).to be_false
+ end
+ end
+
describe "#user_mentions" do
it "returns an Array of Entity::UserMention when entities are set" do
user_mentions_hash = [

0 comments on commit 02bf97e

Please sign in to comment.