Class: Mechmarket::Post
- Inherits:
-
Object
- Object
- Mechmarket::Post
- Defined in:
- lib/mechmarket/post.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#created_utc ⇒ Object
readonly
Returns the value of attribute created_utc.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#reddit_id ⇒ Object
readonly
Returns the value of attribute reddit_id.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .create(post) ⇒ Object
- .database ⇒ Object
- .exists?(id) ⇒ Boolean
- .exists_by?(conditions) ⇒ Boolean
- .find(id) ⇒ Object
- .find_by(conditions) ⇒ Object
- .max_id ⇒ Object
- .search(query:, type: 'Selling', min_id: 0) ⇒ Object
Instance Method Summary collapse
- #age ⇒ Object
-
#initialize(raw) ⇒ Post
constructor
A new instance of Post.
- #new_record? ⇒ Boolean
- #save ⇒ Object
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
#author ⇒ Object (readonly)
Returns the value of attribute author.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def @author end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def body @body end |
#created_utc ⇒ Object (readonly)
Returns the value of attribute created_utc.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def created_utc @created_utc end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def id @id end |
#reddit_id ⇒ Object (readonly)
Returns the value of attribute reddit_id.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def reddit_id @reddit_id end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/mechmarket/post.rb', line 5 def type @type end |
#url ⇒ Object (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., post.title, post.body, post.type] ) puts "+ #{post.title}" result end |
.database ⇒ Object
37 38 39 |
# File 'lib/mechmarket/post.rb', line 37 def database @database ||= SQLite3::Database.new(File.('../data/mechmarket.db', File.dirname(__FILE__))) end |
.exists?(id) ⇒ Boolean
41 42 43 |
# File 'lib/mechmarket/post.rb', line 41 def exists?(id) !find(id).nil? end |
.exists_by?(conditions) ⇒ 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_id ⇒ Object
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
#age ⇒ Object
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
20 21 22 |
# File 'lib/mechmarket/post.rb', line 20 def new_record? !self.class.exists_by?(reddit_id: reddit_id) end |
#save ⇒ Object
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 |