Class: MintsLink

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

Instance Method Summary collapse

Constructor Details

#initializeMintsLink

Returns a new instance of MintsLink.

[View source]

2
3
4
5
6
7
8
9
10
# File 'lib/generators/mints_link.rb', line 2

def initialize
  @host = ENV['MONGO_HOST'] || '127.0.0.1'
  @database = ENV['MONGO_DATABASE'] || 'mints'
  @port = ENV['MONGO_PORT'] || '27017'
  @client = Mongo::Client.new([ "#{@host}:#{@port}" ], :database => @database)
  @short_links = @client[:short_links]
  @sl_visits = @client[:sl_visits]
  generate_indexes
end

Instance Method Details

#generate(url) ⇒ Object

[View source]

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/mints_link.rb', line 12

def generate(url)
  code = random_string.upcase
  collection = @short_links
  doc = {
    url: url,
    code: code
  }

  result = collection.insert_one(doc)
  if result.n === 1
    code
  else
    false
  end
end

#get_url(code) ⇒ Object

[View source]

28
29
30
31
32
# File 'lib/generators/mints_link.rb', line 28

def get_url(code)
  collection = @short_links
  record = collection.find( { 'code' => code } ).first
  record["url"]
end

#get_visits(filter, page = 1, pageSize = 1000) ⇒ Object

[View source]

47
48
49
50
# File 'lib/generators/mints_link.rb', line 47

def get_visits(filter, page = 1, pageSize = 1000)
  collection = @sl_visits
  collection.find(filter).sort( {_id: 1}).skip(page * pageSize - pageSize).limit(pageSize)
end

#visit(code, url, contact_id, user_agent, ip) ⇒ Object

[View source]

34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/mints_link.rb', line 34

def visit(code, url, contact_id, user_agent, ip)
  collection = @sl_visits
  doc = {
    code: code,
    url: url,
    contact_id: contact_id,
    user_agent: user_agent,
    ip: ip
  }
  result = collection.insert_one(doc)
  result.n === 1
end