class Thumbtack::Adapters::BasicAdapter
A basic adapter using Ruby's builtin HTTP and JSON parsing libraries
Constants
- BASE_URL
The base Pinboard API URL.
- RESPONSE_FORMAT
The response format requested from the Pinboard API
- SSL_CIPHERS
Secure cipher list
- SSL_VERSION
A secure version of SSL
- TOO_MANY_REQUESTS_CODE
The status code for rate limited responses from the Pinboard API
Public Class Methods
Initialize a BasicAdapter
@example
adapter = BasicAdapter.new(username, token)
@param [String] username
the user to authenticate with
@param [String] token
the API token for the user, found on the Pinboard settings page
@api public
# File lib/thumbtack/adapters/basic_adapter.rb, line 33 def initialize(username, token) @username = username @token = token end
Public Instance Methods
Retrieve JSON from the Pinboard API
@param [String] path
the path to fetch from, relative to the base Pinboard API URL
@param [Hash] params
query parameters to append to the URL
@return [Hash] the response parsed from the JSON
@raise [RateLimitError] if the response is rate-limited
@api private
# File lib/thumbtack/adapters/basic_adapter.rb, line 51 def get(path, params = EMPTY_HASH) uri = URI("#{BASE_URL}#{path}") uri.query = extend_parameters(params) JSON.parse(http_response(uri).body) end
Private Instance Methods
Construct a secure HTTP connection to the Pinboard API
@param [String] host
the Pinboard API host
@param [Fixnum] port
the Pinboard API port
@return [Net::HTTP]
the connection
@api private
# File lib/thumbtack/adapters/basic_adapter.rb, line 85 def connection(host, port) Net::HTTP.new(host, port).tap do |connection| connection.use_ssl = true connection.ssl_version = SSL_VERSION connection.ciphers = SSL_CIPHERS end end
Extend parameters with authentication and format parameters
@param [Hash{#to_s => to_s}] parameters
the parameters to extend
@return [Hash{#to_s => to_s}]
the extended parameters
@api private
# File lib/thumbtack/adapters/basic_adapter.rb, line 101 def extend_parameters(parameters) base_params = { auth_token: "#{@username}:#{@token}", format: RESPONSE_FORMAT } URI.encode_www_form(parameters.merge(base_params)) end
Retrieve HTTP response from a URI
@param [URI] uri
the uri to fetch from
@return [Net::HTTPResponse]
the response
@api private @raise [RateLimitError] if the response is rate-limited
# File lib/thumbtack/adapters/basic_adapter.rb, line 68 def http_response(uri) request = Net::HTTP::Get.new(uri) connection(uri.host, uri.port).request(request).tap do |response| raise RateLimitError if response.code == TOO_MANY_REQUESTS_CODE end end