Module: WrAPI::Request
- Included in:
- API
- Defined in:
- lib/wrapi/entity.rb,
lib/wrapi/request.rb
Overview
Defines HTTP request methods required attributes format
Defined Under Namespace
Classes: Entity
Constant Summary collapse
- CONTENT_TYPE_HDR =
'Content-Type'.freeze
Instance Method Summary collapse
-
#delete(path, options = {}, raw = false) ⇒ Object
Perform an HTTP DELETE request.
-
#get(path, options = {}, raw = false) ⇒ Object
Perform an HTTP GET request and return entity incase format is :json @return if format is :json and !raw an [Entity] is returned, otherwhise the response body.
-
#get_paged(path, options = {}, request_labda = nil) ⇒ Object
Perform an HTTP GET request for paged date sets response @return nil if block given, otherwise complete concatenated json result set.
- #is_json? ⇒ Boolean
-
#post(path, options = {}, raw = true) ⇒ Object
Perform an HTTP POST request.
-
#put(path, options = {}, raw = true) ⇒ Object
Perform an HTTP PUT request.
Instance Method Details
#delete(path, options = {}, raw = false) ⇒ Object
Perform an HTTP DELETE request
64 65 66 67 68 69 |
# File 'lib/wrapi/request.rb', line 64 def delete(path, = {}, raw=false) response = request(:delete, path, ) do |request| yield(request) if block_given? end entity_response(response, raw) end |
#get(path, options = {}, raw = false) ⇒ Object
Perform an HTTP GET request and return entity incase format is :json
@return if format is :json and !raw an [Entity] is returned, otherwhise the response body
11 12 13 14 15 16 17 |
# File 'lib/wrapi/request.rb', line 11 def get(path, = {}, raw=false) response = request(:get, path, ) do |request| # inject headers... yield(request) if block_given? end entity_response(response, raw) end |
#get_paged(path, options = {}, request_labda = nil) ⇒ Object
Perform an HTTP GET request for paged date sets response
@return nil if block given, otherwise complete concatenated json result set
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wrapi/request.rb', line 21 def get_paged(path, = {}, request_labda = nil) raise ArgumentError, "Pages requests should be json formatted (given format '#{format}')" unless is_json? result = [] pager = create_pager while pager.more_pages? response = request(:get, path, .merge(pager.)) do |req| # inject headers... request_labda.call(req) if request_labda end handle_data(response.body,pager) do |d| if block_given? yield(d) else result = add_data(result,d) end end pager.next_page!(response.body) end result unless block_given? end |
#is_json? ⇒ Boolean
71 72 73 |
# File 'lib/wrapi/request.rb', line 71 def is_json? format && 'json'.eql?(format.to_s) end |
#post(path, options = {}, raw = true) ⇒ Object
Perform an HTTP POST request
46 47 48 49 50 51 |
# File 'lib/wrapi/request.rb', line 46 def post(path, = {}, raw=true) response = request(:post, path, ) do |request| yield(request) if block_given? end entity_response(response, raw) end |
#put(path, options = {}, raw = true) ⇒ Object
Perform an HTTP PUT request
55 56 57 58 59 60 |
# File 'lib/wrapi/request.rb', line 55 def put(path, = {}, raw=true) response = request(:put, path, ) do |request| yield(request) if block_given? end entity_response(response, raw) end |