Skip to content

Commit

Permalink
Refactor Twitter::Exceptable into Twitter::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 30, 2013
1 parent ec7c2df commit 8cfb5e9
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/twitter/action_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
module Twitter
class ActionFactory < Twitter::Factory

# Instantiates a new action object
# Construct a new action object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
Expand Down
43 changes: 24 additions & 19 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
require 'forwardable'

module Twitter
class Base
extend Forwardable
attr_reader :attrs
alias to_h attrs
alias to_hash attrs
alias to_hsh attrs
def_delegators :attrs, :delete, :update

# Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
#
# @param attrs [Array, Set, Symbol]
Expand All @@ -18,14 +27,27 @@ def self.attr_reader(*attrs)
include mod
end

# Returns a new object based on the response hash
# Construct an object from the response hash
#
# @param response [Hash]
# @return [Twitter::Base]
def self.from_response(response={})
new(response[:body])
end

# Construct an object from another object
#
# @param attrs [Hash]
# @param key1 [Symbol]
# @param key2 [Symbol]
def self.from_attrs(attrs, key1, key2)
unless attrs[key1].nil?
attrs = attrs.dup
value = attrs.delete(key1)
new(value.update(key2 => attrs))
end
end

# Initializes a new object
#
# @param attrs [Hash]
Expand All @@ -43,24 +65,7 @@ def [](method)
nil
end

# Retrieve the attributes of an object
#
# @return [Hash]
def attrs
@attrs
end
alias to_hash attrs

# Update the attributes of an object
#
# @param attrs [Hash]
# @return [Twitter::Base]
def update(attrs)
@attrs.update(attrs)
self
end

protected
private

# @param attr [Symbol]
# @param other [Twitter::Base]
Expand Down
36 changes: 0 additions & 36 deletions lib/twitter/exceptable.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/twitter/factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twitter
class Factory

# Instantiates a new action object
# Construct a new action object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/geo_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Twitter
class GeoFactory < Twitter::Factory

# Instantiates a new geo object
# Construct a new geo object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/media_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Twitter
class MediaFactory < Twitter::Factory

# Instantiates a new media object
# Construct a new media object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def ==(other)
super || size_equal(other) || attrs_equal(other)
end

protected
private

# @param other [Twitter::Size]
# @return [Boolean]
Expand Down
4 changes: 1 addition & 3 deletions lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require 'forwardable'
require 'twitter/creatable'
require 'twitter/exceptable'
require 'twitter/identity'

module Twitter
class Tweet < Twitter::Identity
extend Forwardable
include Twitter::Creatable
include Twitter::Exceptable
attr_reader :favorite_count, :favorited, :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, :lang, :retweet_count, :retweeted, :source, :text,
Expand Down Expand Up @@ -108,7 +106,7 @@ def urls

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

# @note Must include entities in your request for this method to work
Expand Down
4 changes: 1 addition & 3 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require 'twitter/basic_user'
require 'twitter/creatable'
require 'twitter/exceptable'

module Twitter
class User < Twitter::BasicUser
PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i
include Twitter::Creatable
include Twitter::Exceptable
attr_reader :connections, :contributors_enabled, :default_profile,
:default_profile_image, :description, :favourites_count,
:follow_request_sent, :followers_count, :friends_count, :geo_enabled,
Expand Down Expand Up @@ -89,7 +87,7 @@ def profile_image_url?

# @return [Twitter::Tweet]
def status
@status ||= new_without_self(Twitter::Tweet, @attrs, :status, :user)
@status ||= Twitter::Tweet.from_attrs(@attrs, :status, :user)
end

def status?
Expand Down
8 changes: 8 additions & 0 deletions spec/twitter/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
end
end

describe "#delete" do
it 'deletes an attribute and returns its value' do
base = Twitter::Base.new(id: 1)
expect(base.delete(:id)).to eq(1)
expect(base.attrs[:id]).to be_nil
end
end

describe "#update" do
it 'returns a hash of attributes' do
base = Twitter::Base.new(id: 1)
Expand Down

0 comments on commit 8cfb5e9

Please sign in to comment.