Skip to content

Commit

Permalink
start working on error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vasconsaurus committed Jul 28, 2023
1 parent bc7ac9b commit 1df8244
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions app/models/twitter_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'net/http'

class TwitterClient
# tweet_id = "1684310862842982400"
BASE_URI = "https://api.twitter.com/2/"

def self.tweet_lookup(tweet_id)
Expand Down Expand Up @@ -38,19 +37,25 @@ def self.user_lookup_by_id(id)
private

def self.get(path, params)
uri = URI(URI.join(BASE_URI, path))
uri.query = Rack::Utils.build_query(params)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

headers = {
"Authorization": "Bearer #{PenderConfig.get('twitter_bearer_token')}",
}

request = Net::HTTP::Get.new(uri.request_uri, headers)

response = http.request(request)
JSON.parse(response.body)
begin
uri = URI(URI.join(BASE_URI, path))
uri.query = Rack::Utils.build_query(params)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

headers = {
"Authorization": "Bearer #{PenderConfig.get('twitter_bearer_token')}",
}

request = Net::HTTP::Get.new(uri.request_uri, headers)

response = http.request(request)
JSON.parse(response.body)
rescue Net::HTTPExceptions => e
raise Pender::Exception::RetryLater, "(#{response.code}) #{response.message}"
rescue JSON::ParserError => e
Rails.logger.warn level: 'WARN', message: '[Parser] Could not get `twitter` data', twitter_url: uri, error_class: error.class, response_code: response.code, response_body: response.body
end
end
end

0 comments on commit 1df8244

Please sign in to comment.