Module: WrAPI::Request

Included in:
API
Defined in:
lib/wrapi/entity.rb,
lib/wrapi/request.rb

Overview

Defines HTTP request methods

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) ⇒ Entity, String

Perform an HTTP DELETE request

Parameters:

  • path (String)

    the request path

  • options (Hash) (defaults to: {})

    the request options

  • raw (Boolean) (defaults to: false)

    whether to return raw response

Returns:

  • (Entity, String)

    the response entity or raw response body



87
88
89
90
91
92
# File 'lib/wrapi/request.rb', line 87

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) ⇒ Entity, String

Perform an HTTP GET request and return entity in case format is :json

Parameters:

  • path (String)

    the request path

  • options (Hash) (defaults to: {})

    the request options

  • raw (Boolean) (defaults to: false)

    whether to return raw response

Returns:

  • (Entity, String)

    the response entity or raw response body



17
18
19
20
21
22
23
# File 'lib/wrapi/request.rb', line 17

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) ⇒ Array<Entity>?

Perform an HTTP GET request for paged data sets response

Parameters:

  • path (String)

    the request path

  • options (Hash) (defaults to: {})

    the request options

  • request_labda (Proc) (defaults to: nil)

    an optional lambda to modify the request

Returns:

  • (Array<Entity>, nil)

    the concatenated result set or nil if block given



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wrapi/request.rb', line 31

def get_paged(path, options = {}, request_labda = nil)
  if 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)
      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?
  else
    raise ArgumentError, "Pages requests should be json formatted (given format '#{format}')"
  end
end

#is_json?Boolean

Checks if the response format is JSON

Returns:

  • (Boolean)

    true if the format is JSON, false otherwise



97
98
99
# File 'lib/wrapi/request.rb', line 97

def is_json?
  'json'.eql?(format&.to_s)
end

#post(path, options = {}, raw = true) ⇒ Entity, String

Perform an HTTP POST request

Parameters:

  • path (String)

    the request path

  • options (Hash) (defaults to: {})

    the request options

  • raw (Boolean) (defaults to: true)

    whether to return raw response

Returns:

  • (Entity, String)

    the response entity or raw response body



61
62
63
64
65
66
# File 'lib/wrapi/request.rb', line 61

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) ⇒ Entity, String

Perform an HTTP PUT request

Parameters:

  • path (String)

    the request path

  • options (Hash) (defaults to: {})

    the request options

  • raw (Boolean) (defaults to: true)

    whether to return raw response

Returns:

  • (Entity, String)

    the response entity or raw response body



74
75
76
77
78
79
# File 'lib/wrapi/request.rb', line 74

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