Class: SettingsService::Repository

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepository

Returns a new instance of Repository.



11
12
13
14
15
16
17
18
19
# File 'app/services/settings_service/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

.use_test_client!Object



55
56
57
# File 'app/services/settings_service/repository.rb', line 55

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/repository.rb', line 21

def create_table(name:)
  begin
    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/repository.rb', line 28

def get(table_name:, id:)
  begin
    dynamodb.query(
      table_name: table_name,
      key_condition_expression: "#id = :id",
      expression_attribute_names: { "#id" => "id" },
      expression_attribute_values: { ":id" => id.to_i }
    ).items.inject({}) do |newhash, setting|
      newhash[setting['setting']] = setting['value']
      newhash
    end
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    nil
  end
end

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



44
45
46
47
48
49
50
51
52
53
# File 'app/services/settings_service/repository.rb', line 44

def put(table_name:, id:, setting:, value:)
  dynamodb.put_item(
    table_name: table_name,
    item: {
      id: id.to_i,
      setting: setting,
      value: value
    }
  )
end

#use_test_client!Object



59
60
61
62
# File 'app/services/settings_service/repository.rb', line 59

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