Module: Webmaster::Api::Request
- Included in:
- Base
- Defined in:
- lib/webmaster/api/request.rb
Overview
Defines HTTP verbs
Constant Summary collapse
- METHODS =
[:get, :post, :put, :delete]
- METHODS_WITH_BODIES =
[:post, :put]
Instance Method Summary collapse
- #delete_request(path, params = {}, options = {}) ⇒ Object
- #get_request(path, params = {}, options = {}) ⇒ Object
- #post_request(path, params = {}, options = {}) ⇒ Object
- #put_request(path, params = {}, options = {}) ⇒ Object
- #request(method, path, params = {}, options = {}) ⇒ Object
Instance Method Details
#delete_request(path, params = {}, options = {}) ⇒ Object
24 25 26 |
# File 'lib/webmaster/api/request.rb', line 24 def delete_request(path, params={}, ={}) request(:delete, path, params, ) end |
#get_request(path, params = {}, options = {}) ⇒ Object
12 13 14 |
# File 'lib/webmaster/api/request.rb', line 12 def get_request(path, params={}, ={}) request(:get, path, params, ) end |
#post_request(path, params = {}, options = {}) ⇒ Object
16 17 18 |
# File 'lib/webmaster/api/request.rb', line 16 def post_request(path, params={}, ={}) request(:post, path, params, ) end |
#put_request(path, params = {}, options = {}) ⇒ Object
20 21 22 |
# File 'lib/webmaster/api/request.rb', line 20 def put_request(path, params={}, ={}) request(:put, path, params, ) end |
#request(method, path, params = {}, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/webmaster/api/request.rb', line 28 def request(method, path, params = {}, = {}) if !METHODS.include?(method) raise ArgumentError, "unkown http method: #{method}" end puts "EXECUTED: #{method} - #{path} with #{params} and #{}" if ENV['DEBUG'] conn = connection() if conn.path_prefix != '/' && path.index(conn.path_prefix) != 0 && !path.start_with?('https://', 'http://') path = (conn.path_prefix + path).gsub(/\/(\/)*/, '/') end response = conn.send(method) do |request| case method.to_sym when *(METHODS - METHODS_WITH_BODIES) request.body = params.delete('data') if params.has_key?('data') request.url(path, params) when *METHODS_WITH_BODIES request.url(path) request.body = self.extract_data_from_params(params) unless params.empty? request.headers['Content-Length'] = request.body.size.to_s end end end |