Class: SimilarwebRapidapi::ApiClient

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

Instance Method Summary collapse

Constructor Details

#initialize(rapidapi_key, logger = nil) ⇒ ApiClient

Returns a new instance of ApiClient.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/similarweb_rapidapi.rb', line 17

def initialize(rapidapi_key, logger = nil)
  "  To get rapid_api_key you just have to register here https://bit.ly/3Mz1U9n\n  :param rapidapi_host: X-RapidAPI-Host header & Base URL\n  :param rapidapi_key: X-RapidAPI-Key header\n  DESC\n\n  @rapidapi_host = 'similarweb-working-api.p.rapidapi.com'\n  @logger = logger.nil? ? SimilarwebRapidapi::LoggerMock.new : logger\n  @headers = {\n    'X-RapidAPI-Key': rapidapi_key,\n    'X-RapidAPI-Host': @rapidapi_host\n  }\nend\n"

Instance Method Details

#cancel_task(task_id) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/similarweb_rapidapi.rb', line 152

def cancel_task(task_id)
  "  Cancels created task.\n  DESC\n  path = \"/similarweb/CancelTask?task_id=\#{task_id}\"\n  response = get_request(path)\n  response\nend\n"

#get_additional_data_from_domain(domain) ⇒ Object



74
75
76
77
78
# File 'lib/similarweb_rapidapi.rb', line 74

def get_additional_data_from_domain(domain)
  path = "/similarweb/GetAdditionalDomainData?domain=#{domain}"
  response = get_request(path)
  response
end

#get_basic_data_from_domain(domain) ⇒ Object



68
69
70
71
72
# File 'lib/similarweb_rapidapi.rb', line 68

def get_basic_data_from_domain(domain)
  path = "/similarweb/getdata?domain=#{domain}"
  response = get_request(path)
  response
end

#get_complete_data_for_domain(domain, callback_url = "") ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/similarweb_rapidapi.rb', line 80

def get_complete_data_for_domain(domain, callback_url = "")
  "  Instead of using get_complete_data_task and then downloading get task result \n  use this method. It'll do everything for you.\n  DESC\n  user_task = get_complete_data_task(domain, callback_url)\n  task_result = nil\n  \n  while true\n    wait\n    task_result = get_task_result(user_task.data.task_id)\n  \n    if task_result.is_finished\n      if [email protected]?\n        @logger.info(\"Task=\#{user_task.data.task_id} domain=\#{domain} has been completed.\")\n      end\n      break\n    end\n  \n    if [email protected]?\n      @logger.info(\"Waiting for the task=\#{user_task.data.task_id} domain=\#{domain} to be completed.\")\n    end\n  end\n  \n  task_result\nend\n"

#get_complete_data_for_domains(domains, callback_url = "", callback_success = nil, callback_error = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/similarweb_rapidapi.rb', line 107

def get_complete_data_for_domains(domains, callback_url = "", callback_success = nil, callback_error = nil)
  result = []
  max_tries = 3

  domains.each do |domain|
    last_exception = nil
    max_tries.times do |i|
      begin
        r = get_complete_data_for_domain(domain, callback_url)
        last_exception = nil
        result.append(r)
        if !callback_success.nil?
          callback_success.call(r)
        end
        break
      rescue => ex
        last_exception = ex
      end
    end

    if !last_exception.nil?
      error_message = "Cannot get data for domain=#{domain}. Try again or contact us [email protected]"
      if !@logger.nil?
        @logger.error(error_message, last_exception)
      end
      if !callback_error.nil?
        callback_error.call(last_exception)
      end
    end
  end

  result
end

#get_complete_data_task(domain, callback_url = "") ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/similarweb_rapidapi.rb', line 170

def get_complete_data_task(domain, callback_url = "")
  "  Creates task in the system. The task takes a while to complete, so you can check its\n  status by calling get_task_result or set a callback_url where the result will be sent.\n  About callbacks you can read more here https://rapidapi.com/letsscrape/api/similarweb-working-api/tutorials/use-callbacks-when-using-async-endpoints!\n  :param domain: domain\n  :param callback_url: url where to send response (domain data)\n  DESC\n  path = \"/similarweb/GetCompleteDataAsync?domain=\#{domain}&callback_url=\#{callback_url}\"\n  response = get_request(path)\n  response\nend\n"

#get_my_tasks(task_status) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/similarweb_rapidapi.rb', line 142

def get_my_tasks(task_status)
  "  Returns the list of you all tasks. \n  DESC\n  ts = task_status == TaskStatus::ALL ? \"\" : task_status.to_s\n  path = \"/similarweb/GetMyTasks?task_status=\#{ts}\"\n  response = get_request(path)\n  response\nend\n"

#get_request(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/similarweb_rapidapi.rb', line 32

def get_request(path)

  url = URI.join("https://#{@rapidapi_host}", path).to_s

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  
  session_timeout = 45.0
  is_ok = false

  while true
    request = Net::HTTP::Get.new(uri.request_uri)
  
    request['X-RapidAPI-Key'] = @headers[:'X-RapidAPI-Key']
    request['X-RapidAPI-Host'] = @headers[:'X-RapidAPI-Host']

    response = http.request(request)
    json_response = JSON.parse(response.body, object_class: OpenStruct)

    if response.code == "200"
      is_ok = true
    end

    break if is_ok

    sleep(0.3)
  end

  json_response
end

#get_task_result(task_id) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/similarweb_rapidapi.rb', line 161

def get_task_result(task_id)
  "  After calling get_complete_data_task just use this method to get the result.\n  DESC\n  path = \"/similarweb/GetTaskResult?task_id=\#{task_id}\"\n  response = get_request(path)\n  response\nend\n"

#waitObject



64
65
66
# File 'lib/similarweb_rapidapi.rb', line 64

def wait
  sleep(1)
end