Class: SearchDataAPISearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchdata_sdk/searchdata_api_search.rb

Overview

Abstract HTTP client for api.searchdata.io

Constant Summary collapse

MAIN_API =
"api.searchdata.io"
MAIN_API_PATH =
"/v1"
LOCATIONS_API =
"locations.searchdata.io"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, engine = nil) ⇒ SearchDataAPISearch

Returns a new instance of SearchDataAPISearch.



30
31
32
33
34
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 30

def initialize(params, engine = nil)
  @params = params
  @params[:engine] ||= engine
  raise "`engine` must be defined in params or a second argument" if @params[:engine].nil?
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



28
29
30
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 28

def params
  @params
end

Class Method Details

.api_key=(api_key) ⇒ Object



97
98
99
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 97

def self.api_key=(api_key)
  $searchdata_api_key = api_key
end

.searchdata_api_key=(api_key) ⇒ Object



93
94
95
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 93

def self.searchdata_api_key=(api_key)
  self.api_key = api_key
end

Instance Method Details

#api_keyObject



101
102
103
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 101

def api_key
  @params[:api_key] || @params[:searchdata_api_key] || $searchdata_api_key
end

#check_params(keys = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 36

def check_params(keys = [])
  return if @params.keys == [:engine]
  raise "keys must be a list of String or Symbol" unless keys.instance_of?(Array)

  missing = []
  keys.each do |key|
    case key.class.to_s
    when "String"
      missing << key.to_s if @params[key].nil? && @params[key.to_sym].nil?
    when "Symbol"
      missing << key.to_s if @params[key].nil? && @params[key.to_s].nil?
    else
      raise "keys must contains Symbol or String"
    end
  end

  raise "missing required keys in params.\n #{missing.join(",")}" unless missing.empty?
end

#construct_url(api, path) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 85

def construct_url(api, path)
  @params[:api_key] = $searchdata_api_key unless $searchdata_api_key.nil?

  @params.delete_if { |_, value| value.nil? }

  URI::HTTPS.build(host: api, path: path, query: URI.encode_www_form(@params))
end

#get_hashObject



65
66
67
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 65

def get_hash
  JSON.parse(get_json)
end

#get_jsonObject



61
62
63
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 61

def get_json
  get_results(MAIN_API, MAIN_API_PATH)
end

#get_locationObject



55
56
57
58
59
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 55

def get_location
  @params.delete(:engine)
  @params.delete(:api_key)
  JSON.parse(get_results(LOCATIONS_API, ""))
end

#get_results(api, path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/searchdata_sdk/searchdata_api_search.rb', line 69

def get_results(api, path)
  url = construct_url(api, path)
  URI(url).open(read_timeout: 600).read
rescue OpenURI::HTTPError => e
  error = JSON.parse(e.io.read)["error"]
  if error
    puts "server returns error from url : #{url}"
    raise error
  else
    puts "fail: fetch url: #{url}"
  end
rescue StandardError => e
  puts "fail: fetch url: #{url}"
  raise e
end