Class: Mechmarket::Post

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Post

Returns a new instance of Post.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mechmarket/post.rb', line 7

def initialize(raw)
  @id          = raw['id']
  @reddit_id   = raw['reddit_id']
  @created_utc = raw['created_utc']
  @url         = raw['url']
  @author      = raw['author']
  @title       = raw['title']
  @body        = raw['body']
  @type        = raw['type']

  infer_type if @type.nil?
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def author
  @author
end

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def body
  @body
end

#created_utcObject (readonly)

Returns the value of attribute created_utc.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def created_utc
  @created_utc
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def id
  @id
end

#reddit_idObject (readonly)

Returns the value of attribute reddit_id.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def reddit_id
  @reddit_id
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/mechmarket/post.rb', line 5

def url
  @url
end

Class Method Details

.create(post) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/mechmarket/post.rb', line 61

def create(post)
  # @TODO: Use SCHEMA instead of hard-coding columns.
  result = execute(
    'INSERT INTO posts(reddit_id, created_utc, url, author, title, body, type) VALUES(?, ?, ?, ?, ?, ?, ?)',
    [post.reddit_id, post.created_utc, post.url, post.author, post.title, post.body, post.type]
  )
  puts "+ #{post.title}"
  result
end

.databaseObject



37
38
39
# File 'lib/mechmarket/post.rb', line 37

def database
  @database ||= SQLite3::Database.new(File.expand_path('../data/mechmarket.db', File.dirname(__FILE__)))
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mechmarket/post.rb', line 41

def exists?(id)
  !find(id).nil?
end

.exists_by?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mechmarket/post.rb', line 45

def exists_by?(conditions)
  !find_by(conditions).nil?
end

.find(id) ⇒ Object



49
50
51
52
53
# File 'lib/mechmarket/post.rb', line 49

def find(id)
  if (row = execute('SELECT * FROM posts WHERE id = ?', id).first)
    new(build(row))
  end
end

.find_by(conditions) ⇒ Object



55
56
57
58
59
# File 'lib/mechmarket/post.rb', line 55

def find_by(conditions)
  if (row = execute("SELECT * FROM posts WHERE #{conditions.keys.map { |key| "#{key} = ?" }.join(' AND ')}", conditions.values).first)
    new(build(row))
  end
end

.max_idObject



71
72
73
# File 'lib/mechmarket/post.rb', line 71

def max_id
  execute('SELECT MAX(id) FROM posts').flatten.first || 0
end

.search(query:, type: 'Selling', min_id: 0) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/mechmarket/post.rb', line 75

def search(query:, type: 'Selling', min_id: 0)
  ids = execute("SELECT rowid FROM posts_fts5 WHERE title MATCH '\"#{query}\"' AND rowid > #{min_id}")
  return [] if ids.empty?

  rows = execute("SELECT * FROM posts WHERE id IN(#{ids.flatten.join(', ')}) AND type = ?", type)
  rows.map { |row| new(build(row)) }
end

Instance Method Details

#ageObject



24
25
26
# File 'lib/mechmarket/post.rb', line 24

def age
  ((Time.now.to_i - @created_utc) / 60.0).round(1)
end

#new_record?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mechmarket/post.rb', line 20

def new_record?
  !self.class.exists_by?(reddit_id: reddit_id)
end

#saveObject



28
29
30
31
32
33
34
# File 'lib/mechmarket/post.rb', line 28

def save
  if new_record?
    self.class.create(self)
  else
    self.class.update(self)
  end
end