Class: XporterOnDemand::Registration

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/xporter_on_demand/registration.rb

Defined Under Namespace

Classes: School

Constant Summary collapse

SCHOOL_ATTRIBUTES =
i(
  lea_code
  dfes_code
  school_name
  school_contact_first_name
  school_contact_last_name
  school_contact_email
  school_contact_phone
  school_contact
  school_technical_contact_name
  school_technical_contact_email
  school_technical_contact_phone
  partner_application_id
  partner_name
  partner_registered_email
  scopes_to_authorise
  we_accept_groupcall_usage_policy
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#assign_attributes, #configure_request, #create_result, #dont_raise_exception, #handle_exceptions, #parameterize, #unwrap

Constructor Details

#initialize(args = {}) ⇒ Registration



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xporter_on_demand/registration.rb', line 46

def initialize(args = {})
  @schools = Set.new

  if block_given?
    yield self
  else
    @registration_type     = args[:registration_type] || 'Live'
    @partner_id            = args[:partner_id]
    @app_management_secret = args[:app_management_secret]
  end
end

Instance Attribute Details

#app_management_secretObject

Returns the value of attribute app_management_secret.



10
11
12
# File 'lib/xporter_on_demand/registration.rb', line 10

def app_management_secret
  @app_management_secret
end

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/xporter_on_demand/registration.rb', line 9

def message
  @message
end

#partner_idObject

Returns the value of attribute partner_id.



10
11
12
# File 'lib/xporter_on_demand/registration.rb', line 10

def partner_id
  @partner_id
end

#registration_typeObject

Returns the value of attribute registration_type.



10
11
12
# File 'lib/xporter_on_demand/registration.rb', line 10

def registration_type
  @registration_type
end

#schoolsObject (readonly)

Returns the value of attribute schools.



9
10
11
# File 'lib/xporter_on_demand/registration.rb', line 9

def schools
  @schools
end

Instance Method Details

#add_school(param_hash = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/xporter_on_demand/registration.rb', line 58

def add_school(param_hash = {})
  attributes = SCHOOL_ATTRIBUTES.each_with_object({}) do |key, hash|
    hash[key] = param_hash[key]
  end

  @schools << School.new(attributes)
end

#generate_hash(secret: @app_management_secret, timestamp:, content: to_json) ⇒ Object

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
# File 'lib/xporter_on_demand/registration.rb', line 104

def generate_hash(secret: @app_management_secret, timestamp:, content: to_json)
  raise ArgumentError, 'Must supply your App Management Secret' unless secret
  raise ArgumentError, 'Must supply at least one school' if @schools.empty?

  b64 = Base64.strict_encode64(content + timestamp + secret)

  sha256 = OpenSSL::Digest.new('sha256')
  OpenSSL::HMAC.hexdigest(sha256, secret, b64).upcase
end

#get_school(lea, dfes) ⇒ Object



96
97
98
# File 'lib/xporter_on_demand/registration.rb', line 96

def get_school(lea, dfes)
  @schools.find{ |sch| sch.lea_code == lea && sch.dfes_code == dfes }
end

#registerObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/xporter_on_demand/registration.rb', line 66

def register
  timestamp = DateTime.current.strftime("%FT%T%z")
  auth = generate_hash(timestamp: timestamp)

  args = {
    url: REGISTRATION_PATH,
    headers: {
      'ApplicationId': @partner_id,
      'DateTime': timestamp,
      'Authorization': "Groupcall #{auth}",
    },
    body: to_json,
  }

  response = post(args)

  if response['Schools']
    response['Schools'].all? do |school|
      s = get_school(school['LeaCode'], school['DfesCode'])
      s.status = school['Status']
      s.message = school['Message']

      school['Status'] == 'OK'
    end
  else
    @message = response['Message']
    false
  end
end

#to_jsonObject



100
101
102
# File 'lib/xporter_on_demand/registration.rb', line 100

def to_json
  { Schools: @schools.map(&:camelize), RegistrationType: @registration_type }.to_json
end