Class: SOF::MCM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sof/mcm/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV, http_getter: self.class.http_getter, http_putter: self.class.http_putter) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
# File 'lib/sof/mcm/client.rb', line 23

def initialize(
  env = ENV,
  http_getter: self.class.http_getter,
  http_putter: self.class.http_putter
)
  @env = env
  @http_getter = http_getter
  @http_putter = http_putter
end

Class Attribute Details

.http_getterObject

Allow the http_getter and http_putter to be overridden The getter will receive a URI object The putter will receive a URL string, a body string, and a headers hash



20
21
22
# File 'lib/sof/mcm/client.rb', line 20

def http_getter
  @http_getter
end

.http_putterObject

Allow the http_getter and http_putter to be overridden The getter will receive a URI object The putter will receive a URL string, a body string, and a headers hash



20
21
22
# File 'lib/sof/mcm/client.rb', line 20

def http_putter
  @http_putter
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



32
33
34
# File 'lib/sof/mcm/client.rb', line 32

def env
  @env
end

#http_getterObject (readonly)

Returns the value of attribute http_getter.



32
33
34
# File 'lib/sof/mcm/client.rb', line 32

def http_getter
  @http_getter
end

#http_putterObject (readonly)

Returns the value of attribute http_putter.



32
33
34
# File 'lib/sof/mcm/client.rb', line 32

def http_putter
  @http_putter
end

Instance Method Details

#applications(headers: {}) ⇒ Object

List all applications in the MCM



35
36
37
38
39
40
41
# File 'lib/sof/mcm/client.rb', line 35

def applications(headers: {})
  http_getter.call(
    URI(url_for_action(:list_applications)),
    nil,
    default_headers.merge(headers)
  )
end

#assign(edipi, application_uid:, comment: "Assigning user") ⇒ Object

Assign User to Application in a Site:

  • Responses:

  • 200: ‘status’:‘success’

  • 401: ‘reason’: ‘reason added’ # if credentials are bad

  • 403: ‘reason’: ‘reason added’ # if validation fails

  • 500: HTML if something catastrophic happens



49
50
51
# File 'lib/sof/mcm/client.rb', line 49

def assign(edipi, application_uid:, comment: "Assigning user")
  update_user(edipi, :assign_user_to_application, application_uid:, comment:)
end

#default_headersObject



84
85
86
87
88
89
90
# File 'lib/sof/mcm/client.rb', line 84

def default_headers
  {
    "Accept" => "application/json",
    "Content-Type" => "application/json",
    "Authorization" => "Basic #{Base64.strict_encode64("#{env.fetch("MCM_USERNAME")}:#{env.fetch("MCM_PASSWORD")}")}"
  }
end

#unassign(edipi, application_uid:, comment: "Unassigning user") ⇒ Object

Unassign User from Application in a Site



54
55
56
# File 'lib/sof/mcm/client.rb', line 54

def unassign(edipi, application_uid:, comment: "Unassigning user")
  update_user(edipi, :unassign_user_from_application, application_uid:, comment:)
end

#update_user(edipi, action, application_uid:, comment: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sof/mcm/client.rb', line 62

def update_user(edipi, action, application_uid:, comment: nil)
  response = http_putter.call(
    url_for_action(action),
    {
      user_id: edipi,
      application_uid:,
      comment:,
      role: "Pub Member"
    }.to_json,
    default_headers
  )

  body = JSON.parse(response.body)
  if body["status"] == "failure"
    raise MCM::Error, %(MCM returned status #{response.code}: #{body["reason"]})
  else
    response
  end
rescue *NetHttpTimeoutErrors.all => e
  raise MCM::Error, %(MCM request failed with a timeout: #{e.message})
end

#url_for_action(action) ⇒ Object



58
59
60
# File 'lib/sof/mcm/client.rb', line 58

def url_for_action(action)
  "#{env.fetch("MCM_URL")}/@#{action}"
end