Module: Tweetkit::Connection

Includes:
Auth, Response
Included in:
Client
Defined in:
lib/tweetkit/connection.rb

Constant Summary collapse

BASE_URL =
'https://api.twitter.com/2/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auth

#bearer_auth?, #token_auth?

Instance Attribute Details

#previous_queryObject

Returns the value of attribute previous_query.



11
12
13
# File 'lib/tweetkit/connection.rb', line 11

def previous_query
  @previous_query
end

#previous_urlObject

Returns the value of attribute previous_url.



11
12
13
# File 'lib/tweetkit/connection.rb', line 11

def previous_url
  @previous_url
end

Instance Method Details

#build_expansions(options) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/tweetkit/connection.rb', line 98

def build_expansions(options)
  expansions = options.delete(:expansions)
  return unless expansions

  expansions = expansions.join(',') if expansions.is_a? Array
  { expansions: expansions }
end

#build_fields(options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tweetkit/connection.rb', line 63

def build_fields(options)
  fields = {}
  fields_ = options.delete(:fields)

  if fields_ && !fields_.empty?
    fields_.each do |field, value|
      if value.is_a? Array
        value = value.join(',')
      else
        value = value.delete(' ')
      end

      field = field.to_s.gsub('_', '.')
      fields.merge!({ "#{field}.fields" => value })
    end
  end

  options.each do |key, value|
    next unless key.match? '_fields'

    options.delete(key)

    if value.is_a? Array
      value = value.join(',')
    else
      value = value.delete(' ')
    end

    key = key.to_s.gsub('_', '.')
    fields.merge!({ key => value })
  end

  fields
end

#delete(endpoint, **options) ⇒ Object



27
28
29
# File 'lib/tweetkit/connection.rb', line 27

def delete(endpoint, **options)
  request :delete, endpoint, parse_query_and_convenience_headers(options)
end

#get(endpoint, **options) ⇒ Object



15
16
17
# File 'lib/tweetkit/connection.rb', line 15

def get(endpoint, **options)
  request :get, endpoint, parse_query_and_convenience_headers(options)
end

#parse_query_and_convenience_headers(options) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/tweetkit/connection.rb', line 106

def parse_query_and_convenience_headers(options)
  options = options.dup
  fields = build_fields(options)
  options.merge!(fields) if fields
  expansions = build_expansions(options)
  options.merge!(expansions) if expansions
  options
end

#post(endpoint, **options) ⇒ Object



19
20
21
# File 'lib/tweetkit/connection.rb', line 19

def post(endpoint, **options)
  request :post, endpoint, parse_query_and_convenience_headers(options)
end

#put(endpoint, **options) ⇒ Object



23
24
25
# File 'lib/tweetkit/connection.rb', line 23

def put(endpoint, **options)
  request :put, endpoint, parse_query_and_convenience_headers(options)
end

#request(method, endpoint, data, **options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tweetkit/connection.rb', line 31

def request(method, endpoint, data, **options)
  url = URI.parse("#{BASE_URL}#{endpoint}")
  @previous_url = url
  @previous_query = data

  connection = Faraday.new do |conn|
    if token_auth?
      conn.request :oauth, consumer_key: @consumer_key, consumer_secret: @consumer_secret,
                           token: @access_token, token_secret: @access_token_secret
    elsif bearer_auth?
      conn.request :authorization, 'Bearer', @bearer_token
    else
      raise NotImplementedError, 'No known authentication types were configured'
    end
  end

  response = case method
             when :get
               connection.get(url, data)
             when :post
               connection.post(url, data.to_json, 'Content-Type' => 'application/json')
             when :put
               connection.put(url, data.to_json, 'Content-Type' => 'application/json')
             when :delete
               connection.delete(url, data.to_json, 'Content-Type' => 'application/json')
             end

  Tweetkit::Response::Tweets.new response, connection: connection, twitter_request: { previous_url: @previous_url, previous_query: @previous_query }
rescue StandardError => e
  raise e
end