class BingSearchClient::Client

Attributes

url[R]

Public Class Methods

new(url:) click to toggle source
# File lib/bing_search_client/client.rb, line 5
def initialize(url:)
  @url = url
end

Public Instance Methods

get() click to toggle source
# File lib/bing_search_client/client.rb, line 9
def get
  uri = URI(url)
  req = Net::HTTP::Get.new(uri.request_uri)
  req.add_field('Ocp-Apim-Subscription-Key', BingSearchClient.config.account_key)
  response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
  end
  parse_response(response)
end

Private Instance Methods

parse_response(response) click to toggle source
# File lib/bing_search_client/client.rb, line 21
def parse_response(response)
  body = JSON.parse(response.body, symbolize_names: true)
  if (200..299).include?(response.code.to_i)
    return { results: BingSearchClient::Result.build_from_array_response(raw_response: body[:value]), extra: body.reject { |k| k != :value } }
  else
    raise BingSearchClient::BadResponse.new("#{response.status}: #{body}")
  end
end