Class: DynamodbManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, table_name:, access_key_id: nil, secret_access_key: nil, profile_name: 'default', endpoint: nil) ⇒ DynamodbManager

Returns a new instance of DynamodbManager.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dynamodb_manager.rb', line 7

def initialize(region:, table_name:, access_key_id: nil, secret_access_key: nil, profile_name: 'default', endpoint: nil)
  if access_key_id.nil? && secret_access_key.nil?
    access_key_id = ENV['AWS_ACCESS_KEY_ID']
    secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']

    credentials = Aws::SharedCredentials.new(profile_name: profile_name).credentials if access_key_id.nil? && secret_access_key.nil?
  end
  credentials = Aws::Credentials.new(access_key_id, secret_access_key, ENV['AWS_SESSION_TOKEN'])

  @table_name      = table_name
  @hash_key        = 'hashkey'
  @range_key       = 'rangekey'
  @geohash_key     = 'geohash'
  @geojson         = 'geoJson'
  @geohash_index   = 'geohash-index'
  @hash_key_length = 4
  @local_area_size = 5
  @max_item_return = 10

  if endpoint
    @client = Aws::DynamoDB::Client.new(
      region:      region,
      credentials: credentials,
      endpoint:    endpoint
    )
  else
    @client = Aws::DynamoDB::Client.new(
      region:      region,
      credentials: credentials,
    )
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def client
  @client
end

#geohash_indexObject

Returns the value of attribute geohash_index.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def geohash_index
  @geohash_index
end

#geohash_keyObject

Returns the value of attribute geohash_key.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def geohash_key
  @geohash_key
end

#geojsonObject

Returns the value of attribute geojson.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def geojson
  @geojson
end

#hash_keyObject

Returns the value of attribute hash_key.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def hash_key
  @hash_key
end

#hash_key_lengthObject

Returns the value of attribute hash_key_length.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def hash_key_length
  @hash_key_length
end

#local_area_sizeObject

Returns the value of attribute local_area_size.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def local_area_size
  @local_area_size
end

#max_item_returnObject

Returns the value of attribute max_item_return.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def max_item_return
  @max_item_return
end

#range_keyObject

Returns the value of attribute range_key.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def range_key
  @range_key
end

#table_nameObject

Returns the value of attribute table_name.



6
7
8
# File 'lib/dynamodb_manager.rb', line 6

def table_name
  @table_name
end

Instance Method Details

#get_stores(lat, long) ⇒ Object



62
63
64
65
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dynamodb_manager.rb', line 62

def get_stores(lat, long)
  geohash    = Geohash.encode(lat, long, @local_area_size)
  hash       = geohash[0..(@hash_key_length - 1)]
  all_stores = []
  neighbours = Geohash.neighbours(hash)
  neighbours.unshift(hash)

  neighbours.each do |neighbour|
    resp = query(neighbour)
    resp.items.each do |item|
      latitude  = item[@geojson]['latitude']
      longitude = item[@geojson]['longitude']
      address   = item[@geojson]['address']
      city      = item[@geojson]['city']
      state     = item[@geojson]['state']
      zip       = item[@geojson]['zip']
      area_code = item[@geojson]['area_code']
      phone     = item[@geojson]['phone']
      name      = item[@geojson]['name']
      geohash   = item[@geohash_key]
      uuid      = item[@range_key]
      all_stores << Store.new(
        latitude: latitude,
        longitude: longitude,
        address: address,
        city: city,
        state: state,
        zip: zip,
        area_code: area_code,
        phone: phone,
        name: name,
        geohash: geohash,
        uuid: uuid
      )
    end
    break if all_stores.length >= max_item_return
  end

  # We got all the stores in the biggest possible area, we increase the hash by one and search around now
  neighbours = Geohash.neighbours(geohash)

  closest_stores     = all_stores.select { |store| store.geohash == geohash }
  surrounding_stores = (all_stores - closest_stores).select { |store| neighbours.include?(store.geohash) }
  remaining_stores   = all_stores - (closest_stores + surrounding_stores)

  return closest_stores + surrounding_stores + remaining_stores
end

#put_store(store) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynamodb_manager.rb', line 46

def put_store(store)
  hash = store.geohash[0..(@local_area_size - 1)]
  json = {
    latitude:  store.lat,
    longitude: store.long,
    address:   store.address,
    city:      store.city,
    state:     store.state,
    zip:       store.zip,
    area_code: store.area_code,
    phone:     store.phone,
    name:      store.name,
  }
  put_point(hash, json, store.uuid)
end

#tableObject



40
41
42
43
44
# File 'lib/dynamodb_manager.rb', line 40

def table
  create_table unless @client.list_tables.table_names.include?(@table_name)

  @client.describe_table(table_name: @table_name)
end