Class: Apadmi::Grout::NetworkService

Inherits:
Object
  • Object
show all
Defined in:
lib/apadmi/grout/utils/network_service.rb

Overview

Utility class for running REST network calls

Constant Summary collapse

PATCH_CONTENT_TYPE =
"application/json-patch+json"
FORM_CONTENT_TYPE =
"application/x-www-form-urlencoded"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_service, base_url, timeout = 30) ⇒ NetworkService

Returns a new instance of NetworkService.

Parameters:

  • auth_service (Object)

    An object that responds to auth_header

  • base_url (String)


20
21
22
23
24
# File 'lib/apadmi/grout/utils/network_service.rb', line 20

def initialize(auth_service, base_url, timeout = 30)
  @base_url = base_url
  @auth_service = auth_service
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/apadmi/grout/utils/network_service.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#do_delete(path) ⇒ Faraday::Response

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


91
92
93
94
95
96
# File 'lib/apadmi/grout/utils/network_service.rb', line 91

def do_delete(path)
  conn = setup_con
  response = conn.delete("#{@base_url}#{path}", CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_file_post(path, file_path, type) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/apadmi/grout/utils/network_service.rb', line 65

def do_file_post(path, file_path, type)
  conn = Faraday.new do |f|
    f.request :multipart
    f.adapter :net_http
  end
  conn.options.timeout = @timeout
  conn.headers["Authorization"] = @auth_service.auth_header

  payload = { file: Faraday::Multipart::FilePart.new(file_path, type) }
  response = conn.post("#{@base_url}#{path}", payload)
  throw_if_error(response)
  response
end

#do_form_encoded_post(path, payload) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/apadmi/grout/utils/network_service.rb', line 55

def do_form_encoded_post(path, payload)
  conn = setup_con
  response = conn.post("#{@base_url}#{path}") do |req|
    req.headers[CONTENT_TYPE] = FORM_CONTENT_TYPE
    req.body = URI.encode_www_form(payload)
  end
  throw_if_error(response)
  response
end

#do_get(path) ⇒ Faraday::Response

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


28
29
30
31
32
33
# File 'lib/apadmi/grout/utils/network_service.rb', line 28

def do_get(path)
  conn = setup_con
  response = conn.get("#{@base_url}#{path}", CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_patch(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


82
83
84
85
86
87
# File 'lib/apadmi/grout/utils/network_service.rb', line 82

def do_patch(path, payload)
  conn = setup_con
  response = conn.patch("#{@base_url}#{path}", payload, CONTENT_TYPE => PATCH_CONTENT_TYPE)
  throw_if_error(response)
  response
end

#do_post(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


48
49
50
51
52
53
# File 'lib/apadmi/grout/utils/network_service.rb', line 48

def do_post(path, payload)
  conn = setup_con
  response = conn.post("#{@base_url}#{path}", payload, CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_put(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


38
39
40
41
42
43
# File 'lib/apadmi/grout/utils/network_service.rb', line 38

def do_put(path, payload)
  conn = setup_con
  response = conn.put("#{@base_url}#{path}", payload, CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end