Module: Tweetkit::Connection
Constant Summary collapse
- BASE_URL =
'https://api.twitter.com/2/'
Instance Attribute Summary collapse
-
#previous_query ⇒ Object
Returns the value of attribute previous_query.
-
#previous_url ⇒ Object
Returns the value of attribute previous_url.
Instance Method Summary collapse
- #build_expansions(options) ⇒ Object
- #build_fields(options) ⇒ Object
- #delete(endpoint, **options) ⇒ Object
- #get(endpoint, **options) ⇒ Object
- #parse_query_and_convenience_headers(options) ⇒ Object
- #post(endpoint, **options) ⇒ Object
- #put(endpoint, **options) ⇒ Object
- #request(method, endpoint, data, **options) ⇒ Object
Methods included from Auth
Instance Attribute Details
#previous_query ⇒ Object
Returns the value of attribute previous_query.
11 12 13 |
# File 'lib/tweetkit/connection.rb', line 11 def previous_query @previous_query end |
#previous_url ⇒ Object
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() expansions = .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() fields = {} fields_ = .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 .each do |key, value| next unless key.match? '_fields' .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, **) request :delete, endpoint, parse_query_and_convenience_headers() end |
#get(endpoint, **options) ⇒ Object
15 16 17 |
# File 'lib/tweetkit/connection.rb', line 15 def get(endpoint, **) request :get, endpoint, parse_query_and_convenience_headers() 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() = .dup fields = build_fields() .merge!(fields) if fields expansions = build_expansions() .merge!(expansions) if expansions end |
#post(endpoint, **options) ⇒ Object
19 20 21 |
# File 'lib/tweetkit/connection.rb', line 19 def post(endpoint, **) request :post, endpoint, parse_query_and_convenience_headers() end |
#put(endpoint, **options) ⇒ Object
23 24 25 |
# File 'lib/tweetkit/connection.rb', line 23 def put(endpoint, **) request :put, endpoint, parse_query_and_convenience_headers() 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, **) 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 |