Skip to content

Commit 3de2572

Browse files
author
julianfoo
committed
WIP: Refactor Response object to make it easier/faster to use
1 parent b578bd1 commit 3de2572

3 files changed

Lines changed: 113 additions & 118 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
tweetkit (0.1.3)
4+
tweetkit (0.1.4)
55
faraday (>= 0.9)
66
faraday_middleware (~> 1.0.0)
77
simple_oauth (~> 0.3.0)

lib/tweetkit/connection.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
module Tweetkit
88
module Connection
99
include Tweetkit::Auth
10+
include Tweetkit::Response
1011

1112
attr_accessor :saved_params, :saved_url
1213

@@ -36,7 +37,7 @@ def request(method, endpoint, data, **options)
3637
response = conn.post(url)
3738
end
3839
conn.close
39-
Tweetkit::Response.new(response)
40+
Tweetkit::Response::Tweets.new(response)
4041
rescue StandardError => e
4142
raise e
4243
end

lib/tweetkit/response.rb

Lines changed: 110 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,158 @@
11
require 'json'
22
require 'pry'
3-
require 'tweetkit/connection'
43

54
module Tweetkit
6-
class Response
7-
attr_accessor :resources, :meta, :original_response, :tweets
8-
9-
def initialize(response)
10-
@original_response = response.body
11-
parsed_response = JSON.parse(@original_response)
12-
@tweets = Tweetkit::Response::Tweets.new(parsed_response)
13-
@meta = Tweetkit::Response::Meta.new(@tweets.meta)
14-
@next_token = @meta.next_token
15-
@prev_token = @meta.prev_token
16-
@resources = Tweetkit::Response::Resources.new(@tweets.resources)
17-
end
18-
19-
def next
20-
binding.pry
21-
@meta.next_token
22-
end
23-
24-
def prev
25-
@meta.prev_token
26-
end
27-
28-
class Resources
5+
module Response
6+
class Tweets
297
include Enumerable
308

31-
VALID_RESOURCES = Set['users', 'tweets', 'media']
32-
33-
attr_accessor :resources
9+
attr_accessor :meta, :original_response, :resources, :tweets
3410

35-
def initialize(resources)
36-
@resources = resources
37-
build_and_normalize_resources(resources) unless resources.nil?
11+
def initialize(response)
12+
@original_response = response.body
13+
response = JSON.parse(@original_response)
14+
@tweets = response['data'] ? response['data'].collect { |tweet| Tweetkit::Response::Tweets::Tweet.new(tweet) } : []
15+
@meta = Tweetkit::Response::Tweets::Meta.new(response['meta'])
16+
@resources = response['includes']
3817
end
3918

40-
def build_and_normalize_resources(resources)
41-
resources.each_key do |resource_type|
42-
normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type)
43-
instance_variable_set(:"@#{resource_type}", normalized_resource)
44-
self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") }
45-
end
19+
def each(*args, &block)
20+
tweets.each(*args, &block)
4621
end
4722

48-
def build_and_normalize_resource(resource, resource_type)
49-
Tweetkit::Response::Resources::Resource.new(resource, resource_type)
23+
def last
24+
tweets.last
5025
end
5126

52-
def method_missing(method, **args)
53-
return nil if VALID_RESOURCES.include?(method.to_s)
54-
55-
super
27+
def next
5628
end
5729

58-
def respond_to_missing?(method, *args)
59-
VALID_RESOURCES.include?(method.to_s) || super
30+
def prev
6031
end
6132

62-
class Resource
63-
include Enumerable
33+
# def method_missing(method, **args)
34+
# tweets.public_send(method, **args)
35+
# end
6436

65-
attr_accessor :normalized_resource, :original_resource
37+
# def respond_to_missing?(method, *args)
38+
# tweets.respond_to?(method)
39+
# end
6640

67-
RESOURCE_NORMALIZATION_KEY = {
68-
'users': 'id'
69-
}.freeze
41+
class Tweet
42+
attr_accessor :data
7043

71-
def initialize(resource, resource_type)
72-
@original_resource = resource
73-
@normalized_resource = {}
74-
normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym]
75-
resource.each do |data|
76-
key = data[normalization_key]
77-
@normalized_resource[key.to_i] = data
78-
end
44+
def initialize(tweet)
45+
@data = tweet
7946
end
8047

81-
def each(*args, &block)
82-
@normalized_resource.each(*args, &block)
48+
def id
49+
data['id']
8350
end
8451

85-
def each_data(*args, &block)
86-
@normalized_resource.values.each(*args, &block)
52+
def text
53+
data['text']
8754
end
8855

89-
def find(key)
90-
@normalized_resource[key.to_i]
91-
end
56+
# def method_missing(attribute)
57+
# data = tweet[attribute.to_s]
58+
# data.empty? ? super : data
59+
# end
60+
61+
# def respond_to_missing?(method, *args)
62+
# tweet.respond_to?(method) || super
63+
# end
9264
end
93-
end
9465

95-
class Meta
96-
include Enumerable
66+
class Resources
67+
include Enumerable
9768

98-
attr_accessor :meta, :next_token, :prev_token
69+
VALID_RESOURCES = Set['users', 'tweets', 'media']
9970

100-
def initialize(meta)
101-
@meta = meta
102-
@next_token = @meta['next_token']
103-
@prev_token = @meta['prev_token']
104-
end
71+
attr_accessor :resources
10572

106-
def method_missing(attribute, **args)
107-
data = meta[attribute.to_s]
108-
data.empty? ? super : data
109-
end
73+
def initialize(resources)
74+
@resources = resources
75+
build_and_normalize_resources(resources) unless resources.nil?
76+
end
11077

111-
def respond_to_missing?(method, *args)
112-
meta.respond_to? method
113-
end
114-
end
78+
def build_and_normalize_resources(resources)
79+
resources.each_key do |resource_type|
80+
normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type)
81+
instance_variable_set(:"@#{resource_type}", normalized_resource)
82+
self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") }
83+
end
84+
end
11585

116-
class Tweets
117-
include Enumerable
86+
def build_and_normalize_resource(resource, resource_type)
87+
Tweetkit::Response::Resources::Resource.new(resource, resource_type)
88+
end
11889

119-
attr_accessor :tweets, :meta, :resources
90+
def method_missing(method, **args)
91+
return nil if VALID_RESOURCES.include?(method.to_s)
12092

121-
def initialize(response)
122-
@tweets = response['data'] ? response['data'].collect { |tweet| Tweetkit::Response::Tweet.new(tweet) } : []
123-
@meta = response['meta']
124-
@resources = response['includes']
125-
end
93+
super
94+
end
12695

127-
def each(*args, &block)
128-
tweets.each(*args, &block)
129-
end
96+
def respond_to_missing?(method, *args)
97+
VALID_RESOURCES.include?(method.to_s) || super
98+
end
13099

131-
def last
132-
tweets.last
133-
end
100+
class Resource
101+
include Enumerable
134102

135-
def to_s
136-
@tweets.join(' ')
137-
end
103+
attr_accessor :normalized_resource, :original_resource
138104

139-
def method_missing(method, **args)
140-
tweets.public_send(method, **args)
141-
end
105+
RESOURCE_NORMALIZATION_KEY = {
106+
'users': 'id'
107+
}.freeze
142108

143-
def respond_to_missing?(method, *args)
144-
tweets.respond_to?(method)
145-
end
146-
end
109+
def initialize(resource, resource_type)
110+
@original_resource = resource
111+
@normalized_resource = {}
112+
normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym]
113+
resource.each do |data|
114+
key = data[normalization_key]
115+
@normalized_resource[key.to_i] = data
116+
end
117+
end
147118

148-
class Tweet
149-
attr_accessor :tweet
119+
def each(*args, &block)
120+
@normalized_resource.each(*args, &block)
121+
end
150122

151-
def initialize(tweet)
152-
@tweet = tweet
153-
end
123+
def each_data(*args, &block)
124+
@normalized_resource.values.each(*args, &block)
125+
end
154126

155-
def method_missing(attribute)
156-
data = tweet[attribute.to_s]
157-
data.empty? ? super : data
127+
def find(key)
128+
@normalized_resource[key.to_i]
129+
end
130+
end
158131
end
159132

160-
def respond_to_missing?(method, *args)
161-
tweet.respond_to?(method) || super
133+
class Meta
134+
attr_accessor :data
135+
136+
def initialize(meta)
137+
@data = meta
138+
end
139+
140+
def next_token
141+
@data['next_token']
142+
end
143+
144+
def prev_token
145+
@data['prev_token']
146+
end
147+
148+
# def method_missing(attribute, **args)
149+
# data = meta[attribute.to_s]
150+
# data.empty? ? super : data
151+
# end
152+
153+
# def respond_to_missing?(method, *args)
154+
# meta.respond_to? method
155+
# end
162156
end
163157
end
164158
end

0 commit comments

Comments
 (0)