Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 147 lines (131 sloc) 3.753 kB
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
1 require 'forwardable'
17880f4 @sferik Respond with NullObject if Tweet has no User
authored
2 require 'twitter/null_object'
56c2a58 @sferik Dynamically define URI methods
authored
3 require 'uri'
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
4
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
5 module Twitter
6 class Base
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
7 extend Forwardable
8 attr_reader :attrs
9 alias to_h attrs
10 alias to_hash attrs
11 alias to_hsh attrs
12 def_delegators :attrs, :delete, :update
13
56c2a58 @sferik Dynamically define URI methods
authored
14 # Define methods that retrieve the value from attributes
9e032aa @sferik Flesh out YARD documentation
authored
15 #
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
16 # @param attrs [Array, Symbol]
9c29181 @sferik Rename lazy_attr_reader to attr_reader since it's not actually lazy
authored
17 def self.attr_reader(*attrs)
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
18 attrs.each do |attr|
19 define_attribute_method(attr)
20 define_predicate_method(attr)
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
21 end
22 end
23
56c2a58 @sferik Dynamically define URI methods
authored
24 # Define object methods from attributes
ca8e285 @stve separate object initialization from identity map for #285
stve authored
25 #
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
26 # @param klass [Symbol]
27 # @param key1 [Symbol]
28 # @param key2 [Symbol]
29 def self.object_attr_reader(klass, key1, key2=nil)
30 define_attribute_method(key1, klass, key2)
31 define_predicate_method(key1)
218479f @sferik Implement identity map!
authored
32 end
33
56c2a58 @sferik Dynamically define URI methods
authored
34 # Define URI methods from attributes
35 #
36 # @param attrs [Array, Symbol]
37 def self.uri_attr_reader(*attrs)
38 attrs.each do |uri_key|
39 array = uri_key.to_s.split("_")
40 index = array.index("uri")
41 array[index] = "url"
42 url_key = array.join("_").to_sym
43 define_uri_method(uri_key, url_key)
44 define_predicate_method(uri_key, url_key)
45 alias_method(url_key, uri_key)
46 alias_method("#{url_key}?", "#{uri_key}?")
47 end
48 end
49
50 # Dynamically define a method for a URI
51 #
52 # @param key1 [Symbol]
53 # @param key2 [Symbol]
54 def self.define_uri_method(key1, key2)
55 define_method(key1) do
56 memoize(key1) do
57 ::URI.parse(@attrs[key2]) if @attrs[key2]
58 end
59 end
60 end
61
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
62 # Dynamically define a method for an attribute
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
63 #
64 # @param key1 [Symbol]
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
65 # @param klass [Symbol]
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
66 # @param key2 [Symbol]
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
67 def self.define_attribute_method(key1, klass=nil, key2=nil)
68 define_method(key1) do
69 memoize(key1) do
70 if klass.nil?
71 @attrs[key1]
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
72 else
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
73 if @attrs[key1]
74 if key2.nil?
75 Twitter.const_get(klass).new(@attrs[key1])
76 else
77 attrs = @attrs.dup
78 value = attrs.delete(key1)
79 Twitter.const_get(klass).new(value.update(key2 => attrs))
80 end
81 else
b5ba138 @sferik Singleton NullObject
authored
82 Twitter::NullObject.instance
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
83 end
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
84 end
85 end
86 end
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
87 end
88
89 # Dynamically define a predicate method for an attribute
90 #
91 # @param key [Symbol]
56c2a58 @sferik Dynamically define URI methods
authored
92 def self.define_predicate_method(key1, key2=key1)
93 define_method(:"#{key1}?") do
94 !!@attrs[key2]
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
95 end
96 end
97
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
98 # Construct an object from a response hash
99 #
100 # @param response [Hash]
101 # @return [Twitter::Base]
102 def self.from_response(response={})
103 new(response[:body])
104 end
105
99efde0 @sferik Add update method
authored
106 # Initializes a new object
9e032aa @sferik Flesh out YARD documentation
authored
107 #
108 # @param attrs [Hash]
109 # @return [Twitter::Base]
241d158 @sferik Update RateLimit via Faraday middleware
authored
110 def initialize(attrs={})
ec7c2df @sferik Remove identity map
authored
111 @attrs = attrs || {}
d63d2e5 @sferik Allow [] access to methods for greater backwards compatibility
authored
112 end
113
99efde0 @sferik Add update method
authored
114 # Fetches an attribute of an object using hash notation
9e032aa @sferik Flesh out YARD documentation
authored
115 #
116 # @param method [String, Symbol] Message to send to the object
6fb7b3f @sferik Implement lazy attribute setting
authored
117 def [](method)
d360f80 @sferik Rename oauth_token to access_token
authored
118 send(method.to_sym)
acc1c79 @sferik Make Twitter::Base#[] behave more like a hash
authored
119 rescue NoMethodError
120 nil
cc1e2e4 @sferik Add #user method to Status class
authored
121 end
1dd37f2 @sferik Method spacing
authored
122
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
123 def memoize(key, &block)
124 ivar = :"@#{key}"
125 return instance_variable_get(ivar) if instance_variable_defined?(ivar)
126 result = block.call
127 instance_variable_set(ivar, result)
128 end
129
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
130 private
77d9283 @sferik Make object equality more strict
authored
131
aaa1856 @sferik Refactor _equal methods into Twitter::Base#attr_equal
authored
132 # @param attr [Symbol]
133 # @param other [Twitter::Base]
134 # @return [Boolean]
135 def attr_equal(attr, other)
d360f80 @sferik Rename oauth_token to access_token
authored
136 self.class == other.class && !other.send(attr.to_sym).nil? && send(attr.to_sym) == other.send(attr.to_sym)
aaa1856 @sferik Refactor _equal methods into Twitter::Base#attr_equal
authored
137 end
138
77d9283 @sferik Make object equality more strict
authored
139 # @param other [Twitter::Base]
140 # @return [Boolean]
141 def attrs_equal(other)
4bcd678 @sferik Don't cache dynamic attributes
authored
142 self.class == other.class && !other.attrs.empty? && attrs == other.attrs
77d9283 @sferik Make object equality more strict
authored
143 end
144
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
145 end
146 end
Something went wrong with that request. Please try again.