class Twingly::LiveFeed::Client
@attr [String] api_key
the API key @attr [String] user_agent
the user agent to be used for all API requests @attr [Integer] max_posts
the maximum number of posts that each request can return @attr [Time] timestamp the timestamp that will be used in the next request
Constants
- BASE_URL
- DEFAULT_MAX_POSTS
- DEFAULT_USER_AGENT
- LIVEFEED_API_VERSION
- LIVEFEED_PATH
- TIMESTAMP_FORMAT
Attributes
Public Class Methods
Creates a new Twingly
Search
API client
@param api_key
[String] the API key provided by Twingly
.
If nil, reads key from environment (TWINGLY_SEARCH_KEY).
@param options [Hash] @option options [String] :user_agent the user agent to be used
for all requests
@option options [String] :max_posts the maximum number of posts that can
be returned for each request
@option options [String] :timestamp the timestamp to start the client at @raise [AuthenticationError] if an API key is not set
# File lib/twingly/livefeed/client.rb, line 36 def initialize(api_key = nil, options = {}) @api_key = api_key @user_agent = options.fetch(:user_agent) { DEFAULT_USER_AGENT } @max_posts = options.fetch(:max_posts) { DEFAULT_MAX_POSTS } @timestamp = options.fetch(:timestamp) { Time.now } yield self if block_given? @api_key ||= env_api_key || api_key_missing end
Public Instance Methods
@return [String] the API endpoint URL.
# File lib/twingly/livefeed/client.rb, line 71 def endpoint_url "#{BASE_URL}#{LIVEFEED_PATH}" end
Get the next result from the API and updates the next timestamp
Sends a request to the API using the timestamp set with {#timestamp}, updates the timestamp that should be used in the next request and then returns the result.
@raise [QueryError] if the timestamp is invalid. @raise [AuthenticationError] if the API couldn't authenticate you. Make sure your API key is correct. @raise [ServerError] if the query could not be executed due to a server error. @return [Result] the result for this request.
# File lib/twingly/livefeed/client.rb, line 57 def next_result assert_valid_time(timestamp) get_and_parse_result.tap do |result| update_timestamp(result.next_timestamp) end end
@return [Hash] the request parameters.
# File lib/twingly/livefeed/client.rb, line 82 def request_parameters { apikey: api_key, timestamp: timestamp.to_time.utc.strftime(TIMESTAMP_FORMAT), maxPosts: max_posts, } end
@return [String] the request url for the next request.
# File lib/twingly/livefeed/client.rb, line 66 def url "#{endpoint_url}?#{url_parameters}" end
@see url
@return [String] the query part of the request url.
# File lib/twingly/livefeed/client.rb, line 77 def url_parameters Faraday::Utils.build_query(request_parameters) end
Private Instance Methods
# File lib/twingly/livefeed/client.rb, line 114 def api_key_missing fail AuthenticationError, "No API key has been provided." end
# File lib/twingly/livefeed/client.rb, line 118 def assert_valid_time(time) fail QueryError, "Not a Time object" unless time.respond_to?(:to_time) end
# File lib/twingly/livefeed/client.rb, line 110 def env_api_key ENV['TWINGLY_SEARCH_KEY'] end
# File lib/twingly/livefeed/client.rb, line 96 def get_and_parse_result response_body = get_response.body Parser.new.parse(response_body) end
# File lib/twingly/livefeed/client.rb, line 101 def get_response connection = Faraday.new(url: BASE_URL) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end connection.headers[:user_agent] = user_agent connection.get(LIVEFEED_PATH, request_parameters) end
# File lib/twingly/livefeed/client.rb, line 92 def update_timestamp(timestamp) @timestamp = timestamp end