class Twingly::Search::Client
Constants
- BASE_URL
- DEFAULT_USER_AGENT
- SEARCH_API_VERSION
- SEARCH_PATH
Attributes
api_key[RW]
user_agent[RW]
Public Class Methods
new(api_key = nil, options = {}) { |self| ... }
click to toggle source
Creates a new Twingly
Search
API client
@param api_key
[optional, 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
@raise [AuthenticationError] if an API key is not set.
# File lib/twingly/search/client.rb, line 25 def initialize(api_key = nil, options = {}) @api_key = api_key @user_agent = options.fetch(:user_agent) { DEFAULT_USER_AGENT } yield self if block_given? @api_key ||= env_api_key || api_key_missing end
Public Instance Methods
endpoint_url()
click to toggle source
@return [String] the API endpoint URL
# File lib/twingly/search/client.rb, line 55 def endpoint_url "#{BASE_URL}#{SEARCH_PATH}" end
execute_query(query)
click to toggle source
Executes the given Query
and returns the result
This method should not be called manually, as that is handled by {Query#execute}.
@param query [Query] the query to be executed. @return [Result]
# File lib/twingly/search/client.rb, line 49 def execute_query(query) response_body = get_response(query).body Parser.new.parse(response_body) end
query(&block)
click to toggle source
Returns a new Query
object connected to this client
@yield [Query] @return [Query]
# File lib/twingly/search/client.rb, line 38 def query(&block) Query.new(self, &block) end
Private Instance Methods
api_key_missing()
click to toggle source
# File lib/twingly/search/client.rb, line 74 def api_key_missing fail AuthenticationError, "No API key has been provided." end
env_api_key()
click to toggle source
# File lib/twingly/search/client.rb, line 61 def env_api_key ENV['TWINGLY_SEARCH_KEY'] end
get_response(query)
click to toggle source
# File lib/twingly/search/client.rb, line 65 def get_response(query) 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(SEARCH_PATH, query.request_parameters) end