Class: SettingsService::StudentAssignmentRepository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
app/services/settings_service/student_assignment_repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStudentAssignmentRepository

Returns a new instance of StudentAssignmentRepository.



11
12
13
14
15
16
17
18
19
# File 'app/services/settings_service/student_assignment_repository.rb', line 11

def initialize
  raise "missing canvas domain!" if SettingsService.canvas_domain.nil?
  @secret_key = ENV['S3_ACCESS_KEY']
  @id_key = ENV['S3_ACCESS_KEY_ID']
  Aws.config.update(
    region: 'us-west-2',
    credentials: creds
  )
end

Class Method Details

.dynamodbObject



87
88
89
# File 'app/services/settings_service/student_assignment_repository.rb', line 87

def self.dynamodb
  @dynamodb || Aws::DynamoDB::Client.new
end

.use_test_client!Object



78
79
80
# File 'app/services/settings_service/student_assignment_repository.rb', line 78

def self.use_test_client!
  instance.use_test_client!
end

Instance Method Details

#create_table(name:) ⇒ Object



21
22
23
24
25
26
# File 'app/services/settings_service/student_assignment_repository.rb', line 21

def create_table(name:)
  begin
    self.class.dynamodb.create_table(table_params(name)).successful?
  rescue
  end
end

#get(table_name:, id:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/settings_service/student_assignment_repository.rb', line 28

def get(table_name:, id:)
  assignment = ::Assignment.find(id[:assignment_id])
  migration_id = assignment.migration_id
  student_assignment_id = "#{migration_id}:#{id[:student_id]}"

  self.class.dynamodb.query(
    table_name: table_name,
    key_condition_expression: "#id = :id",
    expression_attribute_names: { "#id" => "id" },
    expression_attribute_values: { ":id" => student_assignment_id }
  ).items.inject({}) do |newhash, setting|
    newhash[setting['setting']] = setting['value']
    newhash
  end
end

#put(table_name:, id:, setting:, value:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/settings_service/student_assignment_repository.rb', line 44

def put(table_name:, id:, setting:, value:)
  return unless value == 'increment'


  assignment = ::Assignment.find(id[:assignment_id])
  return unless assignment.migration_id
  migration_id = assignment.migration_id
  student_assignment_id = "#{migration_id}:#{id[:student_id]}"


  value = SettingsService.get_settings(
    object: 'assignment',
    id: migration_id
  )["max_attempts"]

  return unless value

  student_attempts = SettingsService.get_settings(
    object: 'student_assignment',
    id: id
  )['max_attempts']

  value = student_attempts if student_attempts

  self.class.dynamodb.put_item(
    table_name: table_name,
    item: {
      id: student_assignment_id,
      setting: setting,
      value: value.to_i + 1
    }
  )
end

#use_test_client!Object



82
83
84
85
# File 'app/services/settings_service/student_assignment_repository.rb', line 82

def use_test_client!
  Aws.config = {}
  @dynamodb = Aws::DynamoDB::Client.new(endpoint: 'http://localhost:8000')
end