Module: StoreApi::Request

Constant Summary collapse

TIME_OUT =
20
@@redirect_count =
0

Instance Method Summary collapse

Instance Method Details

#get(host, path, params = nil, https = false, proxy = nil, header = nil) ⇒ String

http get

Parameters:

  • host (String)
  • path (String)
  • proxy (Hash) (defaults to: nil)
    :host=>‘host’,:port=>port
  • header (Hash) (defaults to: nil)

    http header

  • params (Hash) (defaults to: nil)

Returns:

  • (String)

    response.body



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

Parameters:

  • host (String)
  • path (String)
  • params (Hash) (defaults to: nil)
  • proxy (Hash) (defaults to: nil)
    :host=>‘host’,:port=>port
  • header (Hash) (defaults to: nil)

    http header

Returns:

  • (String)

    response.body



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

Parameters:

  • host (String)
  • path (String)
  • method (String) (defaults to: 'get')

    get/post

  • https (Boolean) (defaults to: false)
  • proxy (Hash) (defaults to: nil)
    :host=>‘host’,:port=>port
  • header (Hash) (defaults to: nil)

    http header

  • params (Hash) (defaults to: nil)

Returns:

  • (Object)

    response



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