Class: Twitter::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Memoizable, Utils
Defined in:
lib/twitter/base.rb

Direct Known Subclasses

Configuration, Entity, Geo, Identity, Language, Metadata, OEmbed, ProfileBanner, RateLimit, Relationship, Settings, Size, Streaming::StallWarning, Suggestion, Token, Trend

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils

flat_pmap, pmap

Constructor Details

- (Twitter::Base) initialize(attrs = {})

Initializes a new object



128
129
130
# File 'lib/twitter/base.rb', line 128

def initialize(attrs = {})
  @attrs = attrs || {}
end

Instance Attribute Details

- (Object) attrs (readonly) Also known as: to_h

Returns the value of attribute attrs



12
13
14
# File 'lib/twitter/base.rb', line 12

def attrs
  @attrs
end

Class Method Details

+ (Object) attr_reader(*attrs)

Define methods that retrieve the value from attributes



21
22
23
24
25
26
# File 'lib/twitter/base.rb', line 21

def attr_reader(*attrs)
  attrs.each do |attr|
    define_attribute_method(attr)
    define_predicate_method(attr)
  end
end

+ (Object) define_attribute_method(key1, klass = nil, key2 = nil)

Dynamically define a method for an attribute



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twitter/base.rb', line 85

def define_attribute_method(key1, klass = nil, key2 = nil)
  define_method(key1) do ||
    if @attrs[key1].nil? || @attrs[key1].respond_to?(:empty?) && @attrs[key1].empty?
      NullObject.new
    else
      if klass.nil?
        @attrs[key1]
      else
        attrs = attrs_for_object(key1, key2)
        Twitter.const_get(klass).new(attrs)
      end
    end
  end
  memoize(key1)
end

+ (Object) define_predicate_method(key1, key2 = key1)

Dynamically define a predicate method for an attribute



116
117
118
119
120
121
# File 'lib/twitter/base.rb', line 116

def define_predicate_method(key1, key2 = key1)
  define_method(:#{key1}?") do ||
    !@attrs[key2].nil? && @attrs[key2] != false && !(@attrs[key2].respond_to?(:empty?) && @attrs[key2].empty?)
  end
  memoize(:#{key1}?")
end

+ (Object) define_uri_method(key1, key2)

Dynamically define a method for a URI



73
74
75
76
77
78
# File 'lib/twitter/base.rb', line 73

def define_uri_method(key1, key2)
  define_method(key1) do ||
    Addressable::URI.parse(@attrs[key2]) unless @attrs[key2].nil?
  end
  memoize(key1)
end

+ (Object) deprecate_attribute_method(key)

Dynamically define a method for an attribute



104
105
106
107
108
109
110
# File 'lib/twitter/base.rb', line 104

def deprecate_attribute_method(key)
  define_method(key) do ||
    warn "#{Kernel.caller.first}: [DEPRECATION] ##{key} is deprecated. Use ##{key}? instead."
    @attrs[key]
  end
  memoize(key)
end

+ (Object) display_uri_attr_reader

Define display_uri attribute methods



62
63
64
65
66
67
# File 'lib/twitter/base.rb', line 62

def display_uri_attr_reader
  define_attribute_method(:display_url)
  alias_method(:display_uri, :display_url)
  define_predicate_method(:display_uri, :display_url)
  alias_method(:display_url?, :display_uri?)
end

+ (Object) object_attr_reader(klass, key1, key2 = nil)

Define object methods from attributes



40
41
42
43
# File 'lib/twitter/base.rb', line 40

def object_attr_reader(klass, key1, key2 = nil)
  define_attribute_method(key1, klass, key2)
  define_predicate_method(key1)
end

+ (Object) predicate_attr_reader(*attrs)



28
29
30
31
32
33
# File 'lib/twitter/base.rb', line 28

def predicate_attr_reader(*attrs)
  attrs.each do |attr|
    define_predicate_method(attr)
    deprecate_attribute_method(attr)
  end
end

+ (Object) uri_attr_reader(*attrs)

Define URI methods from attributes



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/twitter/base.rb', line 48

def uri_attr_reader(*attrs)
  attrs.each do |uri_key|
    array = uri_key.to_s.split('_')
    index = array.index('uri')
    array[index] = 'url'
    url_key = array.join('_').to_sym
    define_uri_method(uri_key, url_key)
    alias_method(url_key, uri_key)
    define_predicate_method(uri_key, url_key)
    alias_method(:#{url_key}?", :#{uri_key}?")
  end
end

Instance Method Details

- (Object) [](method)

Fetches an attribute of an object using hash notation



135
136
137
138
139
140
# File 'lib/twitter/base.rb', line 135

def [](method)
  warn "#{Kernel.caller.first}: [DEPRECATION] #[#{method.inspect}] is deprecated. Use ##{method} to fetch the value."
  send(method.to_sym)
rescue NoMethodError
  nil
end