semrush 3.0.9 → 3.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/semrush.rb +1 -1
- data/lib/semrush/report.rb +23 -14
- data/lib/semrush/version.rb +1 -1
- metadata +1 -1
data/lib/semrush.rb
CHANGED
@@ -9,7 +9,7 @@ require 'semrush/report'
|
|
9
9
|
|
10
10
|
module Semrush
|
11
11
|
API_REPORT_URL = "http://%DB%.api.semrush.com/?action=report&type=%REPORT_TYPE%&%REQUEST_TYPE%=%REQUEST%&key=%API_KEY%&display_limit=%LIMIT%&display_offset=%OFFSET%&export=api&export_columns=%EXPORT_COLUMNS%"
|
12
|
-
API_UNITS_URL = "http://
|
12
|
+
API_UNITS_URL = "http://www.semrush.com/users/countapiunits.html?key=%API_KEY%"
|
13
13
|
mattr_accessor :api_key
|
14
14
|
@@api_key = ""
|
15
15
|
mattr_accessor :debug
|
data/lib/semrush/report.rb
CHANGED
@@ -63,22 +63,31 @@ module Semrush
|
|
63
63
|
# Takes a hash parameter that may contain the following keys :
|
64
64
|
# * :api_key (ex: :api_key => 'gt97s6d4a6w')
|
65
65
|
def self.remaining_quota params = {}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
66
|
+
@remaining_quota_url ||= begin
|
67
|
+
temp_url = "#{API_UNITS_URL}" #do not copy the constant as is or else the constant would be modified !!
|
68
|
+
params = {:api_key => Semrush.api_key}.merge(params)
|
69
|
+
params.each {|k, v|
|
70
|
+
if v.blank?
|
71
|
+
temp_url.gsub!(/&[^&=]+=%#{k.to_s}%/i, '')
|
72
|
+
else
|
73
|
+
temp_url.gsub!("%#{k.to_s.upcase}%", URI.escape(v.to_s).gsub('&', '%26'))
|
74
|
+
end
|
75
|
+
}
|
76
|
+
temp_url
|
77
|
+
end
|
78
|
+
puts "[Semrush query] URL: #{@remaining_quota_url}" if Semrush.debug
|
79
|
+
url = URI.parse(@remaining_quota_url)
|
77
80
|
response = Net::HTTP.start(url.host, url.port) {|http|
|
78
81
|
http.get(url.path+"?"+url.query)
|
79
|
-
}
|
80
|
-
response.
|
81
|
-
|
82
|
+
}
|
83
|
+
body = response.body
|
84
|
+
if body.blank? && response['location'].present? && response['location']!=@remaining_quota_url
|
85
|
+
@remaining_quota_url = URI.join("http://#{url.host}", response['location']).to_s
|
86
|
+
self.remaining_quota params
|
87
|
+
else
|
88
|
+
body.force_encoding("utf-8")
|
89
|
+
body.starts_with?("ERROR") ? error(body) : body.to_i
|
90
|
+
end
|
82
91
|
end
|
83
92
|
|
84
93
|
# Main report.
|
data/lib/semrush/version.rb
CHANGED