Class: BingSearchClient::News

Inherits:
Object
  • Object
show all
Defined in:
lib/bing_search_client/news.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_results: 100, params: {}) ⇒ News

Returns a new instance of News.



6
7
8
9
# File 'lib/bing_search_client/news.rb', line 6

def initialize(num_results: 100, params: {})
  @num_results = num_results
  @params = params
end

Instance Attribute Details

#num_resultsObject (readonly)

Returns the value of attribute num_results.



4
5
6
# File 'lib/bing_search_client/news.rb', line 4

def num_results
  @num_results
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/bing_search_client/news.rb', line 4

def params
  @params
end

Instance Method Details

#search(q:, offset: 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/bing_search_client/news.rb', line 11

def search(q:, offset: 0)
  query_string = 'q='
  query_portion = URI.encode_www_form_component('\'' + q + '\'')
  local_params = "&count=#{num_results}&offset=#{offset}"
  @params.each do |k,v|
    local_params << "&#{k.to_s}=#{v.to_s}"
  end
  full_address = [BingSearchClient.config.base_url, 'news/search'].join('/') + '?' + query_string + query_portion + local_params
  Client.new(url: full_address).get
end