Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 146 lines (128 sloc) 3.639 kb
7ea2f53 @sferik Replace URI with Addressable
authored
1 require 'addressable/uri'
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
2 require 'forwardable'
4a67d25 @sferik Use memoizable
authored
3 require 'memoizable'
17880f4 @sferik Respond with NullObject if Tweet has no User
authored
4 require 'twitter/null_object'
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
5
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
6 module Twitter
7 class Base
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
8 extend Forwardable
4a67d25 @sferik Use memoizable
authored
9 include Memoizable
ae91e0c @sferik Move Twitter::Exceptable#new_without_self into Twitter::Base
authored
10 attr_reader :attrs
11 alias to_h attrs
12 alias to_hash attrs
13 alias to_hsh attrs
14
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
15 class << self
16
17 # Construct an object from a response hash
18 #
19 # @param response [Hash]
20 # @return [Twitter::Base]
ce6ac07 @sferik Add surrounding space in default value assignment
authored
21 def from_response(response = {})
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
22 new(response[:body])
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
23 end
24
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
25 # Define methods that retrieve the value from attributes
26 #
27 # @param attrs [Array, Symbol]
28 def attr_reader(*attrs)
a5aaf32 @sferik Revert "Replace internal uses of each with for"
authored
29 attrs.each do |attr|
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
30 define_attribute_method(attr)
31 define_predicate_method(attr)
32 end
33 end
218479f @sferik Implement identity map!
authored
34
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
35 # Define object methods from attributes
36 #
37 # @param klass [Symbol]
38 # @param key1 [Symbol]
39 # @param key2 [Symbol]
ce6ac07 @sferik Add surrounding space in default value assignment
authored
40 def object_attr_reader(klass, key1, key2 = nil)
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
41 define_attribute_method(key1, klass, key2)
42 define_predicate_method(key1)
56c2a58 @sferik Dynamically define URI methods
authored
43 end
44
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
45 # Define URI methods from attributes
46 #
47 # @param attrs [Array, Symbol]
48 def uri_attr_reader(*attrs)
a5aaf32 @sferik Revert "Replace internal uses of each with for"
authored
49 attrs.each do |uri_key|
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
50 array = uri_key.to_s.split("_")
51 index = array.index("uri")
52 array[index] = "url"
53 url_key = array.join("_").to_sym
f1f9087 @sferik Cleanup #459
authored
54 define_uri_method(uri_key, url_key)
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
55 alias_method(url_key, uri_key)
f1f9087 @sferik Cleanup #459
authored
56 define_predicate_method(uri_key, url_key)
57 alias_method(:"#{url_key}?", :"#{uri_key}?")
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
58 end
56c2a58 @sferik Dynamically define URI methods
authored
59 end
60
5a3c296 @sferik Update documentation [ci skip]
authored
61 # Define display_uri attribute methods
f1f9087 @sferik Cleanup #459
authored
62 def display_uri_attr_reader
63 define_attribute_method(:display_url)
64 alias_method(:display_uri, :display_url)
65 define_predicate_method(:display_uri, :display_url)
66 alias_method(:display_url?, :display_uri?)
1701764 @michaelherold Reclassify display_uris as Strings.
michaelherold authored
67 end
68
f1f9087 @sferik Cleanup #459
authored
69 private
70
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
71 # Dynamically define a method for a URI
72 #
73 # @param key1 [Symbol]
74 # @param key2 [Symbol]
75 def define_uri_method(key1, key2)
5377cc9 @sferik Oh, the things I do for Ruby 1.8 compatibility...
authored
76 define_method(key1) do ||
97d7c68 @matthewrudy use Addressable::URI everywhere
matthewrudy authored
77 Addressable::URI.parse(@attrs[key2]) if @attrs[key2]
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
78 end
79 memoize(key1)
80 end
81
82 # Dynamically define a method for an attribute
83 #
84 # @param key1 [Symbol]
85 # @param klass [Symbol]
86 # @param key2 [Symbol]
ce6ac07 @sferik Add surrounding space in default value assignment
authored
87 def define_attribute_method(key1, klass = nil, key2 = nil)
5377cc9 @sferik Oh, the things I do for Ruby 1.8 compatibility...
authored
88 define_method(key1) do ||
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
89 if klass.nil?
90 @attrs[key1]
91 else
92 if @attrs[key1]
93 attrs = attrs_for_object(key1, key2)
94 Twitter.const_get(klass).new(attrs)
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
95 else
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
96 NullObject.new
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
97 end
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
98 end
99 end
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
100 memoize(key1)
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
101 end
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
102
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
103 # Dynamically define a predicate method for an attribute
104 #
5a3c296 @sferik Update documentation [ci skip]
authored
105 # @param key1 [Symbol]
106 # @param key2 [Symbol]
ce6ac07 @sferik Add surrounding space in default value assignment
authored
107 def define_predicate_method(key1, key2 = key1)
5377cc9 @sferik Oh, the things I do for Ruby 1.8 compatibility...
authored
108 define_method(:"#{key1}?") do ||
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
109 !!@attrs[key2]
110 end
f1f9087 @sferik Cleanup #459
authored
111 memoize(:"#{key1}?")
11fa58a @sferik Refactor into Twitter::Base#object_attr_reader
authored
112 end
113
3f05440 @sferik Refactor code common to Twitter::Base#attr_reader and object_attr_reader
authored
114 end
115
99efde0 @sferik Add update method
authored
116 # Initializes a new object
9e032aa @sferik Flesh out YARD documentation
authored
117 #
118 # @param attrs [Hash]
119 # @return [Twitter::Base]
ce6ac07 @sferik Add surrounding space in default value assignment
authored
120 def initialize(attrs = {})
ec7c2df @sferik Remove identity map
authored
121 @attrs = attrs || {}
d63d2e5 @sferik Allow [] access to methods for greater backwards compatibility
authored
122 end
123
99efde0 @sferik Add update method
authored
124 # Fetches an attribute of an object using hash notation
9e032aa @sferik Flesh out YARD documentation
authored
125 #
126 # @param method [String, Symbol] Message to send to the object
6fb7b3f @sferik Implement lazy attribute setting
authored
127 def [](method)
d360f80 @sferik Rename oauth_token to access_token
authored
128 send(method.to_sym)
acc1c79 @sferik Make Twitter::Base#[] behave more like a hash
authored
129 rescue NoMethodError
130 nil
cc1e2e4 @sferik Add #user method to Status class
authored
131 end
1dd37f2 @sferik Method spacing
authored
132
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
133 private
134
ce6ac07 @sferik Add surrounding space in default value assignment
authored
135 def attrs_for_object(key1, key2 = nil)
cbc02a7 @sferik Refactor Twitter::Base.define_attribute_method
authored
136 if key2.nil?
137 @attrs[key1]
138 else
139 attrs = @attrs.dup
140 attrs.delete(key1).merge(key2 => attrs)
141 end
142 end
143
b92b0ca @sferik Add User, Status, Place, Point, and Polygon classes
authored
144 end
145 end
Something went wrong with that request. Please try again.