tweetkit 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env.example +5 -0
- data/.gitignore +2 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +21 -0
- data/Gemfile +6 -1
- data/Gemfile.lock +21 -4
- data/LICENSE +21 -0
- data/README.md +6 -4
- data/Rakefile +2 -2
- data/bin/console +4 -0
- data/lib/tweetkit/auth.rb +1 -5
- data/lib/tweetkit/client/response/annotations.rb +0 -0
- data/lib/tweetkit/client/response/attachments.rb +0 -0
- data/lib/tweetkit/client/response/expansions.rb +0 -0
- data/lib/tweetkit/client/response/fields.rb +0 -0
- data/lib/tweetkit/client/response/geo.rb +0 -0
- data/lib/tweetkit/client/response/meta.rb +0 -0
- data/lib/tweetkit/client/response/metrics.rb +0 -0
- data/lib/tweetkit/client/response/response.rb +0 -0
- data/lib/tweetkit/client/response/tweet.rb +0 -0
- data/lib/tweetkit/client/response/tweets.rb +0 -0
- data/lib/tweetkit/client/search/conjunctions.rb +494 -0
- data/lib/tweetkit/client/search/search.rb +23 -0
- data/lib/tweetkit/client/tweets.rb +13 -5
- data/lib/tweetkit/client.rb +0 -1
- data/lib/tweetkit/connection.rb +68 -49
- data/lib/tweetkit/pagination.rb +0 -0
- data/lib/tweetkit/response.rb +575 -96
- data/lib/tweetkit/version.rb +1 -1
- metadata +20 -4
- data/lib/tweetkit/search.rb +0 -121
data/lib/tweetkit/connection.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'faraday_middleware'
|
3
3
|
require 'tweetkit/auth'
|
4
|
-
require 'tweetkit/default'
|
5
4
|
require 'tweetkit/response'
|
6
5
|
|
7
6
|
module Tweetkit
|
8
7
|
module Connection
|
9
8
|
include Tweetkit::Auth
|
9
|
+
include Tweetkit::Response
|
10
|
+
|
11
|
+
attr_accessor :previous_query, :previous_url
|
10
12
|
|
11
13
|
BASE_URL = 'https://api.twitter.com/2/'
|
12
14
|
|
@@ -14,82 +16,99 @@ module Tweetkit
|
|
14
16
|
request :get, endpoint, parse_query_and_convenience_headers(options)
|
15
17
|
end
|
16
18
|
|
19
|
+
def post(endpoint, **options)
|
20
|
+
request :post, endpoint, parse_query_and_convenience_headers(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def put(endpoint, **options)
|
24
|
+
request :put, endpoint, parse_query_and_convenience_headers(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(endpoint, **options)
|
28
|
+
request :delete, endpoint, parse_query_and_convenience_headers(options)
|
29
|
+
end
|
30
|
+
|
17
31
|
def request(method, endpoint, data, **options)
|
18
|
-
auth_type = options.delete(:auth_type)
|
19
32
|
url = URI.parse("#{BASE_URL}#{endpoint}")
|
33
|
+
@previous_url = url
|
34
|
+
@previous_query = data
|
20
35
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
response = conn.get(url)
|
30
|
-
else
|
31
|
-
conn = Faraday.new do |f|
|
36
|
+
connection = Faraday.new do |conn|
|
37
|
+
if token_auth?
|
38
|
+
conn.request :oauth, consumer_key: @consumer_key, consumer_secret: @consumer_secret,
|
39
|
+
token: @access_token, token_secret: @access_token_secret
|
40
|
+
elsif bearer_auth?
|
41
|
+
conn.request :authorization, 'Bearer', @bearer_token
|
42
|
+
else
|
43
|
+
raise NotImplementedError, 'No known authentication types were configured'
|
32
44
|
end
|
33
|
-
response = conn.post(url)
|
34
45
|
end
|
35
|
-
|
36
|
-
|
46
|
+
|
47
|
+
response = case method
|
48
|
+
when :get
|
49
|
+
connection.get(url, data)
|
50
|
+
when :post
|
51
|
+
connection.post(url, data.to_json, 'Content-Type' => 'application/json')
|
52
|
+
when :put
|
53
|
+
connection.put(url, data.to_json, 'Content-Type' => 'application/json')
|
54
|
+
when :delete
|
55
|
+
connection.delete(url, data.to_json, 'Content-Type' => 'application/json')
|
56
|
+
end
|
57
|
+
|
58
|
+
Tweetkit::Response::Tweets.new response, connection: connection, twitter_request: { previous_url: @previous_url, previous_query: @previous_query }
|
37
59
|
rescue StandardError => e
|
38
60
|
raise e
|
39
61
|
end
|
40
62
|
|
41
|
-
def auth_token(type = 'bearer')
|
42
|
-
case type
|
43
|
-
when 'bearer'
|
44
|
-
@bearer_token
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
63
|
def build_fields(options)
|
49
64
|
fields = {}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
65
|
+
fields_ = options.delete(:fields)
|
66
|
+
|
67
|
+
if fields_ && !fields_.empty?
|
68
|
+
fields_.each do |field, value|
|
69
|
+
if value.is_a? Array
|
70
|
+
value = value.join(',')
|
55
71
|
else
|
56
|
-
|
72
|
+
value = value.delete(' ')
|
57
73
|
end
|
58
|
-
|
59
|
-
|
74
|
+
|
75
|
+
field = field.to_s.gsub('_', '.')
|
76
|
+
fields.merge!({ "#{field}.fields" => value })
|
60
77
|
end
|
61
78
|
end
|
79
|
+
|
62
80
|
options.each do |key, value|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
fields.merge!({ _key => _value })
|
81
|
+
next unless key.match? '_fields'
|
82
|
+
|
83
|
+
options.delete(key)
|
84
|
+
|
85
|
+
if value.is_a? Array
|
86
|
+
value = value.join(',')
|
87
|
+
else
|
88
|
+
value = value.delete(' ')
|
72
89
|
end
|
90
|
+
|
91
|
+
key = key.to_s.gsub('_', '.')
|
92
|
+
fields.merge!({ key => value })
|
73
93
|
end
|
94
|
+
|
74
95
|
fields
|
75
96
|
end
|
76
97
|
|
77
98
|
def build_expansions(options)
|
78
|
-
expansions =
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
expansions
|
99
|
+
expansions = options.delete(:expansions)
|
100
|
+
return unless expansions
|
101
|
+
|
102
|
+
expansions = expansions.join(',') if expansions.is_a? Array
|
103
|
+
{ expansions: expansions }
|
85
104
|
end
|
86
105
|
|
87
106
|
def parse_query_and_convenience_headers(options)
|
88
107
|
options = options.dup
|
89
108
|
fields = build_fields(options)
|
90
|
-
options.merge!(fields)
|
109
|
+
options.merge!(fields) if fields
|
91
110
|
expansions = build_expansions(options)
|
92
|
-
options.merge!(expansions)
|
111
|
+
options.merge!(expansions) if expansions
|
93
112
|
options
|
94
113
|
end
|
95
114
|
end
|
File without changes
|