class TwitterLabsAPI::Client
Attributes
api_response[RW]
bearer_token[RW]
debug[RW]
parsed_response[RW]
Public Class Methods
new(bearer_token:, debug: false)
click to toggle source
# File lib/twitter_labs_api/client.rb, line 14 def initialize(bearer_token:, debug: false) @bearer_token = bearer_token @debug = debug require 'httplog' if debug end
Private Instance Methods
error_response?()
click to toggle source
# File lib/twitter_labs_api/client.rb, line 52 def error_response? parsed_response.key?('errors') end
handle_api_error()
click to toggle source
# File lib/twitter_labs_api/client.rb, line 68 def handle_api_error error = parsed_response['errors'].first raise APIError.new("#{error['title']}: #{error['detail']} #{error['type']}", api_response) end
handle_collection()
click to toggle source
# File lib/twitter_labs_api/client.rb, line 60 def handle_collection parsed_response['data'].map(&:with_indifferent_access) end
handle_single()
click to toggle source
# File lib/twitter_labs_api/client.rb, line 56 def handle_single parsed_response['data'].with_indifferent_access end
http_adapter(method)
click to toggle source
# File lib/twitter_labs_api/client.rb, line 43 def http_adapter(method) case method when :put Net::HTTP::Put else Net::HTTP::Get end end
make_request(url:, params: {}, is_collection: false, method: :get)
click to toggle source
# File lib/twitter_labs_api/client.rb, line 22 def make_request(url:, params: {}, is_collection: false, method: :get) uri = URI.parse(url) uri.query = URI.encode_www_form(params) request = http_adapter(method).new(uri) request['Authorization'] = "Bearer #{bearer_token}" request['User-Agent'] = "twitter_labs_api gem #{TwitterLabsAPI::VERSION}" req_options = { use_ssl: uri.scheme == 'https' } self.api_response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end raise_http_error unless api_response.is_a?(Net::HTTPSuccess) self.parsed_response = JSON.parse(api_response.body) handle_api_error if error_response? is_collection ? handle_collection : handle_single end
raise_http_error()
click to toggle source
# File lib/twitter_labs_api/client.rb, line 64 def raise_http_error raise(APIError.new("#{api_response.code} #{api_response.msg}", api_response)) end