Skip to content

Commit acb5246

Browse files
author
julianfoo
committed
WIP: Refactor Expansions and Fields objects
1 parent 7e28170 commit acb5246

2 files changed

Lines changed: 50 additions & 56 deletions

File tree

lib/tweetkit/connection.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ def request(method, endpoint, data, **options)
2323
@previous_url = url
2424
@previous_query = data
2525

26-
# if method == :get
27-
# connection = Faraday.new(params: data) do |conn|
28-
# if auth_type == 'oauth1'
29-
# conn.request :oauth, consumer_key: @consumer_key, consumer_secret: @consumer_secret
30-
# else
31-
# conn.authorization :Bearer, @bearer_token
32-
# end
33-
# end
34-
# response = connection.get(url)
35-
# else
36-
# connection = Faraday.new do |f|
37-
# end
38-
# response = connection.post(url)
39-
# end
40-
# Tweetkit::Response::Tweets.new response, connection: connection, twitter_request: { previous_url: @previous_url, previous_query: @previous_query }
26+
if method == :get
27+
connection = Faraday.new(params: data) do |conn|
28+
if auth_type == 'oauth1'
29+
conn.request :oauth, consumer_key: @consumer_key, consumer_secret: @consumer_secret
30+
else
31+
conn.authorization :Bearer, @bearer_token
32+
end
33+
end
34+
response = connection.get(url)
35+
else
36+
connection = Faraday.new do |f|
37+
end
38+
response = connection.post(url)
39+
end
40+
Tweetkit::Response::Tweets.new response, connection: connection, twitter_request: { previous_url: @previous_url, previous_query: @previous_query }
4141
rescue StandardError => e
4242
raise e
4343
end
@@ -89,7 +89,6 @@ def build_expansions(options)
8989
return unless expansions
9090

9191
expansions = expansions.join(',') if expansions.is_a? Array
92-
binding.pry
9392
{ expansions: expansions }
9493
end
9594

lib/tweetkit/response.rb

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Response
66
class Tweets
77
include Enumerable
88

9-
attr_accessor :connection, :meta, :options, :original_response, :resources, :response, :tweets, :twitter_request
9+
attr_accessor :connection, :expansions, :fields, :meta, :options, :original_response, :response, :tweets, :twitter_request
1010

1111
def initialize(response, **options)
1212
parse! response, **options
@@ -16,7 +16,7 @@ def parse!(response, **options)
1616
parse_response response
1717
extract_and_save_tweets
1818
extract_and_save_meta
19-
extract_and_save_resources
19+
extract_and_save_expansions
2020
extract_and_save_options(**options)
2121
extract_and_save_request
2222
end
@@ -38,8 +38,8 @@ def extract_and_save_meta
3838
@meta = Tweetkit::Response::Tweets::Meta.new(@response['meta'])
3939
end
4040

41-
def extract_and_save_resources
42-
@resources = @response['includes']
41+
def extract_and_save_expansions
42+
@expansions = @response['includes']
4343
end
4444

4545
def extract_and_save_options(**options)
@@ -83,14 +83,6 @@ def prev_page
8383
self
8484
end
8585

86-
# def method_missing(method, **args)
87-
# tweets.public_send(method, **args)
88-
# end
89-
90-
# def respond_to_missing?(method, *args)
91-
# tweets.respond_to?(method)
92-
# end
93-
9486
class Tweet
9587
attr_accessor :data
9688

@@ -116,69 +108,72 @@ def text
116108
# end
117109
end
118110

119-
class Resources
120-
include Enumerable
121-
122-
VALID_RESOURCES = Set['users', 'tweets', 'media']
111+
class Expansions
112+
def initialize(expansions)
113+
binding.pry
114+
end
115+
end
123116

124-
attr_accessor :resources
117+
class Fields
118+
attr_accessor :fields, :media_fields, :place_fields, :poll_fields, :tweet_fields, :user_fields
125119

126-
def initialize(resources)
127-
@resources = resources
128-
build_and_normalize_resources(resources) unless resources.nil?
120+
def initialize(fields)
121+
@fields = fields
122+
binding.pry
123+
build_and_normalize_fields(fields) unless fields.nil?
129124
end
130125

131-
def build_and_normalize_resources(resources)
132-
resources.each_key do |resource_type|
133-
normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type)
134-
instance_variable_set(:"@#{resource_type}", normalized_resource)
135-
self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") }
126+
def build_and_normalize_fields(fields)
127+
fields.each_key do |field_type|
128+
normalized_field = build_and_normalize_field(@fields[field_type], field_type)
129+
instance_variable_set(:"@#{field_type}", normalized_field)
130+
self.class.define_method(field_type) { instance_variable_get("@#{field_type}") }
136131
end
137132
end
138133

139-
def build_and_normalize_resource(resource, resource_type)
140-
Tweetkit::Response::Resources::Resource.new(resource, resource_type)
134+
def build_and_normalize_field(field, field_type)
135+
Tweetkit::Response::Resources::Resource.new(field, field_type)
141136
end
142137

143138
def method_missing(method, **args)
144-
return nil if VALID_RESOURCES.include?(method.to_s)
139+
return nil if VALID_FIELDS.include?(method.to_s)
145140

146141
super
147142
end
148143

149144
def respond_to_missing?(method, *args)
150-
VALID_RESOURCES.include?(method.to_s) || super
145+
VALID_FIELDS.include?(method.to_s) || super
151146
end
152147

153148
class Resource
154149
include Enumerable
155150

156-
attr_accessor :normalized_resource, :original_resource
151+
attr_accessor :normalized_field, :original_field
157152

158-
RESOURCE_NORMALIZATION_KEY = {
153+
FIELD_NORMALIZATION_KEY = {
159154
'users': 'id'
160155
}.freeze
161156

162-
def initialize(resource, resource_type)
163-
@original_resource = resource
164-
@normalized_resource = {}
165-
normalization_key = RESOURCE_NORMALIZATION_KEY[resource_type.to_sym]
166-
resource.each do |data|
157+
def initialize(field, field_type)
158+
@original_field = field
159+
@normalized_field = {}
160+
normalization_key = FIELD_NORMALIZATION_KEY[field_type.to_sym]
161+
field.each do |data|
167162
key = data[normalization_key]
168-
@normalized_resource[key.to_i] = data
163+
@normalized_field[key.to_i] = data
169164
end
170165
end
171166

172167
def each(*args, &block)
173-
@normalized_resource.each(*args, &block)
168+
@normalized_field.each(*args, &block)
174169
end
175170

176171
def each_data(*args, &block)
177-
@normalized_resource.values.each(*args, &block)
172+
@normalized_field.values.each(*args, &block)
178173
end
179174

180175
def find(key)
181-
@normalized_resource[key.to_i]
176+
@normalized_field[key.to_i]
182177
end
183178
end
184179
end

0 commit comments

Comments
 (0)