Class: Twitter::Base
- Inherits:
-
Object
- Object
- Twitter::Base
- Extended by:
- Forwardable
- Includes:
- Memoizable, Utils
- Defined in:
- lib/twitter/base.rb
Overview
Base class for Twitter objects
Direct Known Subclasses
Entity, Geo, Identity, Language, Media::VideoInfo, Metadata, OEmbed, ProfileBanner, RateLimit, Relationship, Settings, Size, Streaming::StallWarning, Suggestion, Trend, Variant
Instance Attribute Summary collapse
-
#attrs ⇒ Hash
(also: #to_h)
readonly
The raw attributes hash.
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ void
private
Define methods that retrieve the value from attributes.
-
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ void
private
Dynamically define a method for an attribute.
-
.define_predicate_method(key1, key2 = key1) ⇒ void
private
Dynamically define a predicate method for an attribute.
-
.define_uri_method(key1, key2) ⇒ void
private
Dynamically define a method for a URI.
-
.display_uri_attr_reader ⇒ void
private
Define display_uri attribute methods.
-
.object_attr_reader(klass, key1, key2 = nil) ⇒ void
private
Define object methods from attributes.
-
.predicate_attr_reader(*attrs) ⇒ void
private
Define predicate methods for attributes.
-
.uri_attr_reader(*attrs) ⇒ void
private
Define URI methods from attributes.
Instance Method Summary collapse
-
#[](method) ⇒ Object?
deprecated
Deprecated.
Use attribute methods instead
-
#initialize(attrs = nil) ⇒ Twitter::Base
constructor
Initializes a new object.
Methods included from Utils
Constructor Details
#initialize(attrs = nil) ⇒ Twitter::Base
Initializes a new object
155 156 157 |
# File 'lib/twitter/base.rb', line 155 def initialize(attrs = nil) @attrs = attrs || {} end |
Instance Attribute Details
#attrs ⇒ Hash (readonly) Also known as: to_h
The raw attributes hash
20 21 22 |
# File 'lib/twitter/base.rb', line 20 def attrs @attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Define methods that retrieve the value from attributes
44 45 46 47 48 49 |
# File 'lib/twitter/base.rb', line 44 def attr_reader(*attrs) attrs.each do |attr| define_attribute_method(attr) define_predicate_method(attr) end end |
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dynamically define a method for an attribute
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/twitter/base.rb', line 123 def define_attribute_method(key1, klass = nil, key2 = nil) define_method(key1) do if attr_falsey_or_empty?(key1) # steep:ignore NoMethod NullObject.new else klass.nil? ? @attrs[key1] : Twitter.const_get(klass).new(attrs_for_object(key1, key2)) # steep:ignore NoMethod,FallbackAny end end memoize(key1) end |
.define_predicate_method(key1, key2 = key1) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dynamically define a predicate method for an attribute
140 141 142 143 144 145 |
# File 'lib/twitter/base.rb', line 140 def define_predicate_method(key1, key2 = key1) define_method(:"#{key1}?") do !attr_falsey_or_empty?(key2) # steep:ignore NoMethod end memoize(:"#{key1}?") end |
.define_uri_method(key1, key2) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dynamically define a method for a URI
109 110 111 112 113 114 |
# File 'lib/twitter/base.rb', line 109 def define_uri_method(key1, key2) define_method(key1) do URI.parse(@attrs[key2].chomp("#").gsub(/[^\x00-\x7F]/) { |c| c.bytes.map { |b| format("%%%02X", b) }.join }) unless @attrs[key2].nil? # steep:ignore FallbackAny,NoMethod end memoize(key1) end |
.display_uri_attr_reader ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Define display_uri attribute methods
96 97 98 99 100 101 |
# File 'lib/twitter/base.rb', line 96 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_attr_reader(klass, key1, key2 = nil) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Define object methods from attributes
69 70 71 72 |
# File 'lib/twitter/base.rb', line 69 def object_attr_reader(klass, key1, key2 = nil) define_attribute_method(key1, klass, key2) define_predicate_method(key1) end |
.predicate_attr_reader(*attrs) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Define predicate methods for attributes
56 57 58 59 60 |
# File 'lib/twitter/base.rb', line 56 def predicate_attr_reader(*attrs) attrs.each do |attr| define_predicate_method(attr) end end |
.uri_attr_reader(*attrs) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Define URI methods from attributes
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twitter/base.rb', line 79 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
#[](method) ⇒ Object?
Use attribute methods instead
Fetches an attribute of an object using hash notation
167 168 169 170 171 172 173 |
# File 'lib/twitter/base.rb', line 167 def [](method) location = caller_locations(1, 1)&.first warn "#{location&.path}:#{location&.lineno}: [DEPRECATION] #[#{method.inspect}] is deprecated. Use ##{method} to fetch the value." public_send(method.to_sym) rescue NoMethodError nil end |