Module: StoreApi::Request
- Included in:
- AppStore::Apps::Details, AppStore::Apps::Ranking, GooglePlay::Apps::Details, GooglePlay::Apps::Developer, GooglePlay::Apps::Ranking, GooglePlay::Apps::Search
- Defined in:
- lib/store_api/request.rb
Constant Summary collapse
- TIME_OUT =
20
- @@redirect_count =
0
Instance Method Summary collapse
-
#get(host, path, params = nil, https = false, proxy = nil, header = nil) ⇒ String
http get.
-
#post(host, path, params = nil, https = false, proxy = nil, header = nil) ⇒ String
http post.
-
#request(host, path, method = 'get', params = nil, https = false, proxy = nil, header = nil) ⇒ Object
http request.
Instance Method Details
#get(host, path, params = nil, https = false, proxy = nil, header = nil) ⇒ String
http get
17 18 19 |
# File 'lib/store_api/request.rb', line 17 def get(host,path,params=nil,https=false,proxy=nil,header=nil) request(host,path,'get',params,https,proxy,header).body end |
#post(host, path, params = nil, https = false, proxy = nil, header = nil) ⇒ String
http post
29 30 31 |
# File 'lib/store_api/request.rb', line 29 def post(host,path,params=nil,https=false,proxy=nil,header=nil) request(host,path,'post',params,https,proxy,header).body end |
#request(host, path, method = 'get', params = nil, https = false, proxy = nil, header = nil) ⇒ Object
http request
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 106 107 108 109 110 |
# File 'lib/store_api/request.rb', line 43 def request(host,path,method='get',params=nil,https=false,proxy=nil,header=nil) if !params.nil? query = params.map{|k,v| "#{k}=#{v}"}.join('&') query_escaped = URI.escape(query) end if !proxy.nil? && !proxy.empty? if https http = Net::HTTP::Proxy(proxy[:host],proxy[:port]).new(host,443) else http = Net::HTTP::Proxy(proxy[:host],proxy[:port]).new(host) end else if https http = Net::HTTP.new(host,443) http.use_ssl = true else http = Net::HTTP.new(host) end end http.read_timeout = TIME_OUT if !header.nil? && !header.empty? if method == 'get' if !query_escaped.nil? path = path + '?' + query_escaped end response = http.get(path,header) elsif method == 'post' if !query_escaped.nil? response = http.post(path,query_escaped,header) else response = http.post(path,nil,header) end end else if method == 'get' if !query_escaped.nil? path = path + '?' + query_escaped end response = http.get(path) elsif method == 'post' if !query_escaped.nil? response = http.post(path,query_escaped) else response = http.post(path) end end end if response.code.to_i > 500 @@redirect_count = 0 raise "Server Error ! responce code = #{response.code} response = #{response.body}" elsif response.code.to_i >= 400 && response.code.to_i < 500 @@redirect_count = 0 raise "Not Found ! responce codee = #{response.code} response = #{response.body}" elsif response.code.to_i >= 300 && response.code.to_i < 400 redirect_url = URI.parse(response.header['location']) if redirect_url.scheme == "https" https = true end @@redirect_count = @@redirect_count + 1 if @@redirect_count > 5 raise "Exception Redirect Loop" end response = request(redirect_url.host,redirect_url.path,method,params,https,proxy,header) else @redirect_count = 0 response end end |