semrush 3.0.9 → 3.0.10

Sign up to get free protection for your applications and to get access to all the features.
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://fr.semrush.com/users/countapiunits.html?key=%API_KEY%"
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
@@ -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
- temp_url = "#{API_UNITS_URL}" #do not copy the constant as is or else the constant would be modified !!
67
- params = {:api_key => Semrush.api_key}.merge(params)
68
- params.each {|k, v|
69
- if v.blank?
70
- temp_url.gsub!(/&[^&=]+=%#{k.to_s}%/i, '')
71
- else
72
- temp_url.gsub!("%#{k.to_s.upcase}%", URI.escape(v.to_s).gsub('&', '%26'))
73
- end
74
- }
75
- puts "[Semrush query] URL: #{temp_url}" if Semrush.debug
76
- url = URI.parse(temp_url)
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
- }.body rescue "ERROR :: RESPONSE ERROR (-1)" # Make this error up
80
- response.force_encoding("utf-8")
81
- response.starts_with?("ERROR") ? error(response) : response.to_i
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.
@@ -1,3 +1,3 @@
1
1
  module Semrush
2
- VERSION = "3.0.9" # we want to follow the API version: for API 3.0, the gem will be version 3.0.x
2
+ VERSION = "3.0.10" # we want to follow the API version: for API 3.0, the gem will be version 3.0.x
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semrush
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.9
4
+ version: 3.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: