Module: WrataApi::WrataApiRequest

Included in:
WrataApi
Defined in:
lib/wrata_api/wrata_api_request.rb

Overview

Module for describing base request actions

Instance Method Summary collapse

Instance Method Details

#default_headerHash

Returns default header for requests.

Returns:

  • (Hash)

    default header for requests



9
10
11
12
13
# File 'lib/wrata_api/wrata_api_request.rb', line 9

def default_header
  { 'Cookie' => "remember_token=#{@cookie}; \
                _runner_session=#{CGI.escape(@wrata_session)}",
    'X-CSRF-Token' => @csrf_token }
end

#http_object(uri) ⇒ Net:HTTP

Returns http instance.

Parameters:

  • uri (URI)

    url of server

Returns:

  • (Net:HTTP)

    http instance



47
48
49
50
51
# File 'lib/wrata_api/wrata_api_request.rb', line 47

def http_object(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  http
end

#perform_get(uri, body = {}) ⇒ JSON

Make get request

Parameters:

  • uri (URI)

    uri wiht server

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

    body of request

Returns:

  • (JSON)

    result of request



19
20
21
22
23
24
25
26
27
# File 'lib/wrata_api/wrata_api_request.rb', line 19

def perform_get(uri, body = {})
  request = Net::HTTP::Get.new(uri.request_uri, default_header)
  request.set_form_data(body)
  body = http_object(uri).request(request).body
  @logger.info("Get request: #{uri} answered: #{body}")
  return {} if body.empty?

  JSON.parse(body)
end

#perform_post(uri, body = {}) ⇒ JSON

Make post request

Parameters:

  • uri (URI)

    uri wiht server

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

    body of request

Returns:

  • (JSON)

    result of request



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wrata_api/wrata_api_request.rb', line 33

def perform_post(uri, body = {})
  http = http_object(uri)
  request = Net::HTTP::Post.new(uri.request_uri, default_header)
  request.set_form_data(body)
  http.read_timeout = 500
  body = http.request(request).body
  @logger.info("Post request: #{uri} answered: #{body}")
  return {} if body.empty?

  JSON.parse(body)
end