Class: Rest::Wrappers::NetHttpPersistentWrapper

Inherits:
BaseWrapper
  • Object
show all
Defined in:
lib/rest/wrappers/net_http_persistent_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseWrapper

#post_file, #to_json_parts

Constructor Details

#initialize(client) ⇒ NetHttpPersistentWrapper

Returns a new instance of NetHttpPersistentWrapper.



55
56
57
58
59
60
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 55

def initialize(client)
  @client = client
  @http = Net::HTTP::Persistent.new('rest_gem')
  @http.proxy = URI(@client.options[:http_proxy]) if @client.options[:http_proxy]
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



53
54
55
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 53

def http
  @http
end

Instance Method Details

#add_headers(post, req_hash, default_headers) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 69

def add_headers(post, req_hash, default_headers)
  headers = {}
  headers.merge!(default_headers)
  headers.merge!(req_hash[:headers]) if req_hash[:headers]
  headers.each_pair do |k, v|
    post[k] = v
  end
end

#append_query_params(req_hash, uri) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 123

def append_query_params(req_hash, uri)
  if req_hash[:params]
    new_q = URI.encode_www_form(req_hash[:params])
    if uri.query
      new_q = uri.query + "&" + new_q
    end
    #puts "new_q: " + new_q
    uri.query = new_q
  end
end

#closeObject



214
215
216
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 214

def close
  http.shutdown
end

#default_headersObject



62
63
64
65
66
67
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 62

def default_headers
  {
      #"Accept-Encoding" => "gzip, deflate",
      #"Accept" => "*/*; q=0.5, application/xml"
  }
end

#delete(url, req_hash = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 198

def delete(url, req_hash={})
  r = nil
  uri = URI(url)
  append_query_params(req_hash, uri)
  post = Net::HTTP::Delete.new fix_path(uri.request_uri)
  add_headers(post, req_hash, default_headers)
  post.body = stringed_body(req_hash[:body]) if req_hash[:body]
  response = http.request uri, post
  r = NetHttpPersistentResponseWrapper.new(response)
  case response
    when Net::HTTPClientError, Net::HTTPServerError
      raise Rest::HttpError.new(r, r.code.to_i)
  end
  r
end

#fix_path(path) ⇒ Object



134
135
136
137
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 134

def fix_path(path)
  return "/" if path.nil? || path == ""
  path
end

#get(url, req_hash = {}, options = {}) ⇒ Object



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
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 78

def get(url, req_hash={}, options={})
  @client.logger.debug "limit #{options[:limit]}"
  options[:limit] ||= 10
  r = nil
  begin

    uri = URI(url)
    #p uri
    #p uri.path
    #p uri.request_uri
    #puts "query: " + uri.query.inspect
    #puts "fragment: " + uri.fragment.inspect
    append_query_params(req_hash, uri)
    #p uri.request_uri
    post = Net::HTTP::Get.new fix_path(uri.request_uri)
    add_headers(post, req_hash, default_headers)
    response = http.request uri, post
    if @client.logger.debug?
      @client.logger.debug "Response class: #{response.class.name}"
      @client.logger.debug "Response headers: #{response.header.to_hash.inspect}"
    end

    r = NetHttpPersistentResponseWrapper.new(response)
    case response
      when Net::HTTPClientError, Net::HTTPServerError
        raise Rest::HttpError.new(r, r.code.to_i)
    end
    #  when Net::HTTPRedirection
    #    @client.logger.debug "moved to #{response['location']}"
    #    response = get(response['location'], req_hash, {limit: options[:limit]-1})
    #  when Net::HTTPMovedPermanently
    #    @client.logger.debug "moved to #{response['location']}"
    #    response = get(response['location'], req_hash, {limit: options[:limit]-1})
    #end
    #rescue Net::HTTPClientError, Net::HTTPServerError => ex
    #  raise NetHttpPersistentExceptionWrapper.new(ex)
    #rescue Net::HTTPServerError => ex
    #  if ex.code == 404
    #    return NetHttpPersistentResponseWrapper.new(ex)
    #  end
    #  raise NetHttpPersistentExceptionWrapper.new(ex)
  end
  r
end

#patch(url, req_hash = {}) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 182

def patch(url, req_hash={})
  r = nil
  uri = URI(url)
  append_query_params(req_hash, uri)
  post = Net::HTTP::Patch.new fix_path(uri.request_uri)
  add_headers(post, req_hash, default_headers)
  post.body = stringed_body(req_hash[:body]) if req_hash[:body]
  response = http.request uri, post
  r = NetHttpPersistentResponseWrapper.new(response)
  case response
    when Net::HTTPClientError, Net::HTTPServerError
      raise Rest::HttpError.new(r, r.code.to_i)
  end
  r
end

#post(url, req_hash = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 140

def post(url, req_hash={})
  r = nil
  uri = URI(url)
  append_query_params(req_hash, uri)
  post = Net::HTTP::Post.new fix_path(uri.request_uri)
  add_headers(post, req_hash, default_headers)
  post.body = stringed_body(req_hash[:body]) if req_hash[:body]
  post.set_form_data req_hash[:form_data] if req_hash[:form_data]
  Rest.logger.debug "POST request to #{uri}. body: #{post.body}"
  response = http.request uri, post
  r = NetHttpPersistentResponseWrapper.new(response)
  case response
    when Net::HTTPClientError, Net::HTTPServerError
      raise Rest::HttpError.new(r, r.code.to_i)
  end
  r
end

#put(url, req_hash = {}) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 166

def put(url, req_hash={})
  r = nil
  uri = URI(url)
  append_query_params(req_hash, uri)
  post = Net::HTTP::Put.new fix_path(uri.request_uri)
  add_headers(post, req_hash, default_headers)
  post.body = stringed_body(req_hash[:body]) if req_hash[:body]
  response = http.request uri, post
  r = NetHttpPersistentResponseWrapper.new(response)
  case response
    when Net::HTTPClientError, Net::HTTPServerError
      raise Rest::HttpError.new(r, r.code.to_i)
  end
  r
end

#stringed_body(body) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 158

def stringed_body(body)
  return nil unless body
  if body.is_a?(Hash)
    return body.to_json
  end
  body
end