Skip to content

Commit 7a27096

Browse files
author
julianfoo
committed
WIP: Use Faraday middleware to use OAuth 1.0 and 2.0 auth. Refactor connection creation logic
1 parent 2ae2309 commit 7a27096

1 file changed

Lines changed: 11 additions & 25 deletions

File tree

lib/tweetkit/connection.rb

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'pry'
12
require 'faraday'
23
require 'tweetkit/auth'
34
require 'tweetkit/default'
@@ -7,23 +8,14 @@ module Tweetkit
78
module Connection
89
include Tweetkit::Auth
910

10-
CONVENIENCE_HEADERS = Set.new([:accept, :content_type])
1111
BASE_URL = 'https://api.twitter.com/2/'
1212

13-
def get(url, **options)
14-
request :get, url, parse_query_and_convenience_headers(options)
13+
def get(endpoint, **options)
14+
request :get, endpoint, parse_query_and_convenience_headers(options)
1515
end
1616

1717
def request(method, endpoint, data, **options)
18-
if data.is_a?(Hash)
19-
options[:headers] = data.delete(:headers) || {}
20-
if accept = data.delete(:accept)
21-
options[:headers][:accept] = accept
22-
end
23-
end
24-
25-
headers = options[:headers].merge(oauth_headers)
26-
18+
headers = data.delete(:headers)
2719
url = URI.parse("#{BASE_URL}#{endpoint}")
2820

2921
if method == :get
@@ -36,8 +28,9 @@ def request(method, endpoint, data, **options)
3628
raise e
3729
end
3830

39-
def oauth_headers
40-
if bearer_auth?
31+
def auth_headers(type = 'bearer')
32+
case type
33+
when 'bearer'
4134
{ 'Authorization': "Bearer #{@bearer_token}" }
4235
end
4336
end
@@ -83,19 +76,12 @@ def build_expansions(options)
8376

8477
def parse_query_and_convenience_headers(options)
8578
options = options.dup
86-
headers = options.delete(:headers) { Hash.new }
87-
CONVENIENCE_HEADERS.each do |h|
88-
if header = options.delete(h)
89-
headers[h] = header
90-
end
91-
end
79+
options[:headers] = auth_headers
9280
fields = build_fields(options)
93-
opts = options.merge!(fields)
81+
options.merge!(fields)
9482
expansions = build_expansions(options)
95-
opts.merge!(expansions)
96-
opts[:headers] = headers unless headers.empty?
97-
98-
opts
83+
options.merge!(expansions)
84+
options
9985
end
10086
end
10187
end

0 commit comments

Comments
 (0)