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)


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

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.



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

def base_url
  @base_url
end

Instance Method Details

#do_delete(path) ⇒ Faraday::Response

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


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

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



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

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::UploadIO.new(file_path, type) }
  response = conn.post("#{@base_url}#{path}", payload)
  throw_if_error(response)
  response
end

#do_form_encoded_post(path, payload) ⇒ Object



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

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)


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

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)


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

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)


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

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)


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

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