Class: Twitter::Base

### Home Classes Methods

### In Files

- lib/twitter/base.rb

### Parent

Object

### Methods

- ::attr_reader
- ::fetch
- ::fetch_or_new
- ::from_response
- ::identity_map
- ::new
- ::store
- #[]
- #attr_equal
- #attrs
- #attrs_equal
- #to_hash
- #update

### Class/Module Index

Quicksearch

- Twitter
- Twitter::API
- Twitter::API::Arguments
- Twitter::API::DirectMessages
- Twitter::API::Favorites
- Twitter::API::FriendsAndFollowers
- Twitter::API::Help
- Twitter::API::Lists
- Twitter::API::PlacesAndGeo
- Twitter::API::SavedSearches
- Twitter::API::Search
- Twitter::API::SpamReporting
- Twitter::API::SuggestedUsers
- Twitter::API::Timelines
- Twitter::API::Trends
- Twitter::API::Tweets
- Twitter::API::Undocumented
- Twitter::API::Users
- Twitter::API::Utils
- Twitter::Action
- Twitter::Action::Favorite
- Twitter::Action::Follow
- Twitter::Action::ListMemberAdded
- Twitter::Action::Mention
- Twitter::Action::Reply
- Twitter::Action::Retweet
- Twitter::Action::Status
- Twitter::Action::Tweet
- Twitter::ActionFactory
- Twitter::Base
- Twitter::BasicUser
- Twitter::Client
- Twitter::Configurable
- Twitter::Configuration
- Twitter::Creatable
- Twitter::Cursor
- Twitter::Default
- Twitter::DirectMessage
- Twitter::Entity
- Twitter::Entity::Hashtag
- Twitter::Entity::Url
- Twitter::Entity::UserMention
- Twitter::Error
- Twitter::Error::AlreadyFavorited
- Twitter::Error::AlreadyRetweeted
- Twitter::Error::BadGateway
- Twitter::Error::BadRequest
- Twitter::Error::ClientError
- Twitter::Error::ConfigurationError
- Twitter::Error::DecodeError
- Twitter::Error::EnhanceYourCalm
- Twitter::Error::Forbidden
- Twitter::Error::GatewayTimeout
- Twitter::Error::IdentityMapKeyError
- Twitter::Error::InternalServerError
- Twitter::Error::NotAcceptable
- Twitter::Error::NotFound
- Twitter::Error::RateLimited
- Twitter::Error::ServerError
- Twitter::Error::ServiceUnavailable
- Twitter::Error::TooManyRequests
- Twitter::Error::Unauthorized
- Twitter::Error::UnprocessableEntity
- Twitter::Exceptable
- Twitter::Factory
- Twitter::Geo
- Twitter::Geo::Point
- Twitter::Geo::Polygon
- Twitter::GeoFactory
- Twitter::Identity
- Twitter::IdentityMap
- Twitter::Language
- Twitter::List
- Twitter::Media
- Twitter::Media::Photo
- Twitter::MediaFactory
- Twitter::Metadata
- Twitter::OEmbed
- Twitter::Place
- Twitter::ProfileBanner
- Twitter::RateLimit
- Twitter::Relationship
- Twitter::Request
- Twitter::Request::MultipartWithFile
- Twitter::Response
- Twitter::Response::ParseJson
- Twitter::Response::RaiseError
- Twitter::SavedSearch
- Twitter::SearchResults
- Twitter::Settings
- Twitter::Size
- Twitter::SourceUser
- Twitter::Status
- Twitter::Suggestion
- Twitter::TargetUser
- Twitter::Trend
- Twitter::Tweet
- Twitter::User
- Twitter::Version
- Enumerable
- Kernel

No matching classes.

# Twitter::Base

### Public Class Methods

attr_reader(*attrs) click to toggle source

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

@param attrs [Array, Set, Symbol]

    # File lib/twitter/base.rb, line 8
    def self.attr_reader(*attrs)
      mod = Module.new do
        attrs.each do |attribute|
          define_method attribute do
            @attrs[attribute.to_sym]
          end
          define_method "#{attribute}?" do
            !!@attrs[attribute.to_sym]
          end
        end
      end
      const_set(:Attributes, mod)
      include mod
    end

fetch(attrs) click to toggle source

Retrieves an object from the identity map.

@param attrs [Hash] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 34
    def self.fetch(attrs)
      return unless identity_map
      if object = identity_map.fetch(Marshal.dump(attrs))
        return object
      end
      return yield if block_given?
      raise Twitter::Error::IdentityMapKeyError, "key not found"
    end

fetch_or_new(attrs={}) click to toggle source

Retrieves an object from the identity map, or stores it in the identity map if it doesn't already exist.

@param attrs [Hash] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 65
    def self.fetch_or_new(attrs={})
      return unless attrs
      return new(attrs) unless identity_map

      fetch(attrs) do
        object = new(attrs)
        store(object)
      end
    end

from_response(response={}) click to toggle source

Returns a new object based on the response hash

@param response [Hash] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 56
    def self.from_response(response={})
      fetch_or_new(response[:body])
    end

identity_map() click to toggle source

return [Twitter::IdentityMap]

    # File lib/twitter/base.rb, line 24
    def self.identity_map
      return unless Twitter.identity_map
      @identity_map = Twitter.identity_map.new unless defined?(@identity_map) && @identity_map.class == Twitter.identity_map
      @identity_map
    end

new(attrs={}) click to toggle source

Initializes a new object

@param attrs [Hash] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 79
    def initialize(attrs={})
      @attrs = attrs
    end

store(object) click to toggle source

Stores an object in the identity map.

@param object [Object] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 47
    def self.store(object)
      return object unless identity_map
      identity_map.store(Marshal.dump(object.attrs), object)
    end

### Public Instance Methods

[](method) click to toggle source

Fetches an attribute of an object using hash notation

@param method [String, Symbol] Message to send to the object

    # File lib/twitter/base.rb, line 86
    def [](method)
      send(method.to_sym)
    rescue NoMethodError
      nil
    end

attrs() click to toggle source

Retrieve the attributes of an object

@return [Hash]

    # File lib/twitter/base.rb, line 95
    def attrs
      @attrs
    end

Also aliased as: to_hash
to_hash() click to toggle source
Alias for: attrs
update(attrs) click to toggle source

Update the attributes of an object

@param attrs [Hash] @return [Twitter::Base]

    # File lib/twitter/base.rb, line 104
    def update(attrs)
      @attrs.update(attrs)
      self
    end

### Protected Instance Methods

attr_equal(attr, other) click to toggle source

@param attr [Symbol] @param other [Twitter::Base] @return [Boolean]

    # File lib/twitter/base.rb, line 114
    def attr_equal(attr, other)
      self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
    end

attrs_equal(other) click to toggle source

@param other [Twitter::Base] @return [Boolean]

    # File lib/twitter/base.rb, line 120
    def attrs_equal(other)
      self.class == other.class && !other.attrs.empty? && attrs == other.attrs
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.