Class: Twingly::Search::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/search/client.rb

Overview

Twingly Search API client

Constant Summary collapse

BASE_URL =
"https://api.twingly.com"
SEARCH_API_VERSION =
"v3"
SEARCH_PATH =
"/blog/search/api/#{SEARCH_API_VERSION}/search"
DEFAULT_USER_AGENT =
"Twingly Search Ruby Client/#{VERSION}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, options = {}) {|_self| ... } ⇒ Client

Creates a new Twingly Search API client

Parameters:

  • api_key (optional, String) (defaults to: nil)

    the API key provided by Twingly. If nil, reads key from environment (TWINGLY_SEARCH_KEY).

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :user_agent (String)

    the user agent to be used for all requests

Yields:

  • (_self)

Yield Parameters:

Raises:



25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/twingly/search/client.rb', line 9

def api_key
  @api_key
end

#user_agentObject

Returns the value of attribute user_agent.



9
10
11
# File 'lib/twingly/search/client.rb', line 9

def user_agent
  @user_agent
end

Instance Method Details

#endpoint_urlString

Returns the API endpoint URL.

Returns:

  • (String)

    the API endpoint URL



55
56
57
# File 'lib/twingly/search/client.rb', line 55

def endpoint_url
  "#{BASE_URL}#{SEARCH_PATH}"
end

#execute_query(query) ⇒ Result

Executes the given Query and returns the result

This method should not be called manually, as that is handled by Query#execute.

Parameters:

  • query (Query)

    the query to be executed.

Returns:



49
50
51
52
# 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 {|Query| ... } ⇒ Query

Returns a new Query object connected to this client

Yields:

Returns:



38
39
40
# File 'lib/twingly/search/client.rb', line 38

def query(&block)
  Query.new(self, &block)
end