Class: Twingly::Search::Query

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

Overview

Twingly Search API query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) {|_self| ... } ⇒ Query

No need to call this method manually, instead use Client#query.

Parameters:

  • client (Client)

    the client that this query should be connected to.

Yields:

  • (_self)

Yield Parameters:



52
53
54
55
56
57
58
59
# File 'lib/twingly/search/query.rb', line 52

def initialize(client)
  @client     = client
  @start_time = nil
  @end_time   = nil
  @language   = nil

  yield self if block_given?
end

Instance Attribute Details

#clientClient

the client that this query is connected to.

Returns:

  • (Client)

    the current value of client



12
13
14
# File 'lib/twingly/search/query.rb', line 12

def client
  @client
end

#search_queryString

the search query.

Returns:

  • (String)

    the current value of search_query



12
13
14
# File 'lib/twingly/search/query.rb', line 12

def search_query
  @search_query
end

Instance Method Details

#end_timeTime

Returns the time that was set with #end_time=.

Returns:

  • (Time)

    the time that was set with #end_time=.



45
46
47
# File 'lib/twingly/search/query.rb', line 45

def end_time
  @end_time
end

#end_time=(time) ⇒ Object

Search for posts published before this time (inclusive).

Parameters:

  • time (Time, #to_time)

    an instance of the Time class or an object responding to #to_time.

Raises:

  • (QueryError)

    if the object cannot be converted to a Time object.



117
118
119
120
121
# File 'lib/twingly/search/query.rb', line 117

def end_time=(time)
  assert_valid_time(time)

  @end_time = time
end

#executeResult

Executes the query and returns the result.

Returns:

  • (Result)

    the result for this query.

Raises:



73
74
75
# File 'lib/twingly/search/query.rb', line 73

def execute
  @client.execute_query(self)
end

#languageObject

Deprecated.

Please use #search_query instead



28
29
30
31
# File 'lib/twingly/search/query.rb', line 28

def language
  warn "[DEPRECATION] `language` is deprecated. Please use `search_query` instead."
  @language
end

#language=(language_code) ⇒ Object

Deprecated.

Please use #search_query= instead



34
35
36
37
# File 'lib/twingly/search/query.rb', line 34

def language=(language_code)
  warn "[DEPRECATION] `language=` is deprecated. Please use `search_query=` instead."
  @language = language_code
end

#patternObject

Deprecated.

Please use #search_query instead



16
17
18
19
# File 'lib/twingly/search/query.rb', line 16

def pattern
  warn "[DEPRECATION] `pattern` is deprecated. Please use `search_query` instead."
  @search_query
end

#pattern=(search_query) ⇒ Object

Deprecated.

Please use #search_query= instead



22
23
24
25
# File 'lib/twingly/search/query.rb', line 22

def pattern=(search_query)
  warn "[DEPRECATION] `pattern=` is deprecated. Please use `search_query=` instead."
  @search_query = search_query
end

#request_parametersHash

Returns the request parameters.

Returns:

  • (Hash)

    the request parameters.

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twingly/search/query.rb', line 85

def request_parameters
  full_search_query = search_query.to_s.dup
  full_search_query << " lang:#{@language}" unless @language.to_s.empty?
  full_search_query << " start-date:#{formatted_start_date}" if start_time
  full_search_query << " end-date:#{formatted_end_date}"     if end_time

  if full_search_query.to_s.empty?
    fail QueryError, "Search query cannot be empty"
  end

  {
    apikey: client.api_key,
    q: full_search_query
  }
end

#start_timeTime

Returns the time that was set with #start_time=.

Returns:



40
41
42
# File 'lib/twingly/search/query.rb', line 40

def start_time
  @start_time
end

#start_time=(time) ⇒ Object

Search for posts published after this time (inclusive).

Parameters:

  • time (Time, #to_time)

    an instance of the Time class or an object responding to #to_time.

Raises:

  • (QueryError)

    if the object cannot be converted to a Time object.



106
107
108
109
110
# File 'lib/twingly/search/query.rb', line 106

def start_time=(time)
  assert_valid_time(time)

  @start_time = time
end

#urlString

Returns the request url for the query.

Returns:

  • (String)

    the request url for the query.



62
63
64
# File 'lib/twingly/search/query.rb', line 62

def url
  "#{client.endpoint_url}?#{url_parameters}"
end

#url_parametersString

Returns the query part of the request url.

Returns:

  • (String)

    the query part of the request url.

See Also:



79
80
81
# File 'lib/twingly/search/query.rb', line 79

def url_parameters
  Faraday::Utils.build_query(request_parameters)
end