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

Instance Method Details

#delete(path, options = {}, raw = false) ⇒ Object

Perform an HTTP DELETE request

Returns:

  • response is returened



64
65
66
67
68
69
# File 'lib/wrapi/request.rb', line 64

def delete(path, options = {}, raw=false)
  response = request(:delete, path, options) 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, options = {}, raw=false)
  response = request(:get, path, options) 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

Raises:

  • (ArgumentError)


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, options = {}, 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, options.merge(pager.page_options)) 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

Returns:

  • (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

Returns:

  • response is returned in json if format is :json



46
47
48
49
50
51
# File 'lib/wrapi/request.rb', line 46

def post(path, options = {}, raw=true)
  response = request(:post, path, options) do |request|
    yield(request) if block_given?
  end
  entity_response(response, raw)
end

#put(path, options = {}, raw = true) ⇒ Object

Perform an HTTP PUT request

Returns:

  • response is returned in json if format is :json



55
56
57
58
59
60
# File 'lib/wrapi/request.rb', line 55

def put(path, options = {}, raw=true)
  response = request(:put, path, options) do |request|
    yield(request) if block_given?
  end
  entity_response(response, raw)
end