rrant 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.hound.yml +2 -0
- data/.rspec +2 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rrant +32 -0
- data/bin/setup +8 -0
- data/files/bill.jpg +0 -0
- data/files/devrant.png +0 -0
- data/lib/rrant.rb +8 -0
- data/lib/rrant/error.rb +7 -0
- data/lib/rrant/handler.rb +65 -0
- data/lib/rrant/helper.rb +20 -0
- data/lib/rrant/local.rb +49 -0
- data/lib/rrant/output.rb +56 -0
- data/lib/rrant/remote.rb +132 -0
- data/lib/rrant/store.rb +108 -0
- data/lib/rrant/version.rb +3 -0
- data/rrant.gemspec +33 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f3093a3c154628dccee65189d4900fc057510a4
|
4
|
+
data.tar.gz: 7b4a2b7bcb2610247c3dec85f3892b64b58b61cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7652b9dd7c74394c11701689743d3c5838ca8372ad04b24980d24b1952c928328a966739d7e7152506efe2fd6270035b382348d6cd73eb1de4f6f14cdddbaf2
|
7
|
+
data.tar.gz: cf2048f476d33032489e5aaeff3fa58d06d697fdbcff390a0217efbb20ade976dfcb5a23043d93295e9377f6ce99135fa80bbd6a7e9f0c4e4d47f748eb78af98
|
data/.gitignore
ADDED
data/.hound.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Tomas Koutsky
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Rrant
|
2
|
+
[![Build Status](https://travis-ci.org/stepnivlk/rrant.svg?branch=master)](https://travis-ci.org/stepnivlk/rrant)
|
3
|
+
|
4
|
+
```
|
5
|
+
➜ ~ rrant
|
6
|
+
It would be really FUCKING great if NodeJS or mysql could give me a FUCKING shout or ERROR MESSAGE when one of the parameters I'm giving is not the RIGHT FUCKING DATA TYPE INSTEAD OF THROWING A RANDOM ERROR THAT DOESN'T INDICATE A WRONG MOTHERFUCKING DATA TYPE. Five FUCKING hours of debugging later.
|
7
|
+
|
8
|
+
[linuxxx][55][https://www.devrant.io/rants/403040]
|
9
|
+
```
|
10
|
+
|
11
|
+
Have you ever wanted to read some rants in your terminal ? Now you can and it will look like you work, since terminal output looks serious!
|
12
|
+
You can display random rant as a [fortune](https://wiki.archlinux.org/index.php/Fortune) when logging into terminal, or get some rants in your Ruby code.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'rrant'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install rrant
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Standalone
|
33
|
+
```
|
34
|
+
Usage: rrant [options]
|
35
|
+
-i, --images renders images to the console
|
36
|
+
-u, --unseen shows only unseen rants
|
37
|
+
-f, --fetch AMOUNT fetches new rants in background
|
38
|
+
-h, --help Display this screen
|
39
|
+
```
|
40
|
+
|
41
|
+
### Ruby
|
42
|
+
|
43
|
+
Get random rant from local store as a hash:
|
44
|
+
```ruby
|
45
|
+
Rrant.and.rave.in
|
46
|
+
```
|
47
|
+
|
48
|
+
Get random unseen rant from local store and print it to STDOUT:
|
49
|
+
```ruby
|
50
|
+
Rrant.and.unseen(true).and.rave.out
|
51
|
+
```
|
52
|
+
|
53
|
+
Get random unseen rant from local store and print it to STDOUT with image:
|
54
|
+
```ruby
|
55
|
+
Rrant.and.unseen(true).and.with_images(true).and.rave.out
|
56
|
+
```
|
57
|
+
|
58
|
+
Fetch 20 rants from devRant, store them and get random unseen rant from local store and print it to STDOUT with image:
|
59
|
+
```ruby
|
60
|
+
Rrant.and.dos(20).and.unseen(true).and.with_images(true).and.rave.out
|
61
|
+
```
|
62
|
+
|
63
|
+
You can chain methods in different order, or omitt any of them.
|
64
|
+
Only first ```#and``` method is necessary.
|
65
|
+
Chain is terminated with ```#rave.in``` (returns hash) or ```#rave.out``` (outputs to STDOUT).
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
70
|
+
|
71
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stepnivlk/rrant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
76
|
+
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rrant"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start
|
data/bin/rrant
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), *%w{ .. lib })
|
4
|
+
|
5
|
+
require 'optparse'
|
6
|
+
require 'rrant'
|
7
|
+
|
8
|
+
handler = Rrant.and
|
9
|
+
|
10
|
+
option_parser = OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: rrant [options]"
|
12
|
+
opts.on('-i', '--images', 'renders images to the console') do
|
13
|
+
handler.with_images(true)
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('-u', '--unseen', 'shows only unseen rants') do
|
17
|
+
handler.unseen
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on('-f', '--fetch AMOUNT', 'fetches new rants in background') do |amount|
|
21
|
+
handler.dos(amount.to_i)
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.on("-h", "--help", "Display this screen") do
|
25
|
+
puts opts
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
option_parser.parse!
|
31
|
+
|
32
|
+
handler.rave.out
|
data/bin/setup
ADDED
data/files/bill.jpg
ADDED
Binary file
|
data/files/devrant.png
ADDED
Binary file
|
data/lib/rrant.rb
ADDED
data/lib/rrant/error.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rrant/local'
|
2
|
+
require 'rrant/remote'
|
3
|
+
require 'rrant/store'
|
4
|
+
require 'rrant/output'
|
5
|
+
require 'rrant/helper'
|
6
|
+
|
7
|
+
module Rrant
|
8
|
+
# Public: Initializes all the necessary objects and contains
|
9
|
+
# configuration methods.
|
10
|
+
class Handler
|
11
|
+
include Helper
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@store = Store.new
|
15
|
+
@unseen = false
|
16
|
+
@show_images = false
|
17
|
+
@bill = false
|
18
|
+
end
|
19
|
+
|
20
|
+
# Public: Finds random rant or bill and initializes output with it.
|
21
|
+
#
|
22
|
+
# Returns instance of Rrant::Output.
|
23
|
+
def rave
|
24
|
+
rant = @bill ? bill : local.unseen(@unseen).random
|
25
|
+
Output.new(rant, @show_images)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Public: Fetches rants from remote API. Returns bill if amount
|
29
|
+
# is too high.
|
30
|
+
#
|
31
|
+
# min_amount - Integer, how many rants we want to fetch.
|
32
|
+
#
|
33
|
+
# Returns self.
|
34
|
+
def dos(min_amount = 10)
|
35
|
+
return @bill = true if min_amount > 80
|
36
|
+
|
37
|
+
remote.save(min_amount)
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def and
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def with_images(set = true)
|
46
|
+
@show_images = set
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def unseen(set = true)
|
51
|
+
@unseen = set
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def remote
|
58
|
+
@remote ||= Remote.new(@store)
|
59
|
+
end
|
60
|
+
|
61
|
+
def local
|
62
|
+
@local ||= Local.new(@store)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/rrant/helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rrant
|
2
|
+
module Helper
|
3
|
+
def image_blank?(rant)
|
4
|
+
rant['attached_image'].nil? || rant['attached_image'] == ''
|
5
|
+
end
|
6
|
+
|
7
|
+
def files_path
|
8
|
+
File.expand_path('../../files/', File.dirname(__FILE__))
|
9
|
+
end
|
10
|
+
|
11
|
+
def bill
|
12
|
+
{
|
13
|
+
'text' => '80 rants should be enough.',
|
14
|
+
'image' => "#{files_path}/bill.jpg",
|
15
|
+
'score' => 80,
|
16
|
+
'user_username' => 'Bill'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/rrant/local.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rrant/helper'
|
2
|
+
require 'rrant/error'
|
3
|
+
|
4
|
+
module Rrant
|
5
|
+
# Public: Contains local storage handling methods, operates on top of
|
6
|
+
# store object.
|
7
|
+
class Local
|
8
|
+
include Helper
|
9
|
+
|
10
|
+
def initialize(store)
|
11
|
+
raise Error::InvalidStore unless store.is_a?(Store)
|
12
|
+
|
13
|
+
@store = store
|
14
|
+
@unseen = false
|
15
|
+
end
|
16
|
+
|
17
|
+
# Public: Returns random rant from the store. Returns placeholder if there
|
18
|
+
# are no available rants. Updates rant's 'viewed_at' parameter.
|
19
|
+
def random
|
20
|
+
return placeholder if @store.empty?
|
21
|
+
rant = pick_random
|
22
|
+
return placeholder unless rant
|
23
|
+
|
24
|
+
rant.tap { |r| @store.touch(r['id']) }
|
25
|
+
end
|
26
|
+
|
27
|
+
# Public: Sets 'unseen' instance variable and returns self. With 'unseen'
|
28
|
+
# set to true, we fetch only rants with 'viewed_at' set to nil.
|
29
|
+
def unseen(set)
|
30
|
+
@unseen = set
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
# Private: Grabs random rant from the store according to 'unseen' instance
|
37
|
+
# variable.
|
38
|
+
def pick_random
|
39
|
+
return @store.entities.sample unless @unseen
|
40
|
+
|
41
|
+
@store.entities.reject { |rant| !rant['viewed_at'].nil? }.sample
|
42
|
+
end
|
43
|
+
|
44
|
+
def placeholder
|
45
|
+
{ 'text' => 'No rants available :/',
|
46
|
+
'image' => "#{files_path}/devrant.png" }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/rrant/output.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'catpix'
|
2
|
+
|
3
|
+
module Rrant
|
4
|
+
# Public: Outputs rant hash, or serialized rant with image to the STDOUT.
|
5
|
+
#
|
6
|
+
# rant - Hash, rant to be outputed.
|
7
|
+
# show_images - Boolean, when set we output attached image also.
|
8
|
+
class Output
|
9
|
+
def initialize(rant, show_images)
|
10
|
+
@rant = rant
|
11
|
+
@show_images = show_images
|
12
|
+
end
|
13
|
+
|
14
|
+
# Public: Returns rant as a hash.
|
15
|
+
def in
|
16
|
+
@rant
|
17
|
+
end
|
18
|
+
|
19
|
+
# Public: Prints serialzied rant to the STDOUT.
|
20
|
+
def out
|
21
|
+
puts_image
|
22
|
+
puts @rant['text']
|
23
|
+
puts footer
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# Private: Generates footer with coloring in this format:
|
29
|
+
# [username][score][rant_url]
|
30
|
+
def footer
|
31
|
+
''.tap do |text|
|
32
|
+
text << "\n"
|
33
|
+
text << "\e[37m[\e[0m#{@rant['user_username']}\e[37m][\e[0m"
|
34
|
+
text << "#{@rant['score']}\e[37m][\e[0m"
|
35
|
+
text << "#{build_address}\e[37m]\e[0m"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Wrapper around Catpix gem, prints image to terminal.
|
40
|
+
def puts_image
|
41
|
+
return unless @show_images
|
42
|
+
return unless @rant['image']
|
43
|
+
|
44
|
+
Catpix.print_image(@rant['image'],
|
45
|
+
resolution: 'low',
|
46
|
+
limit_x: 0.4,
|
47
|
+
limit_y: 0.4)
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_address
|
51
|
+
'https://www.devrant.io/'.tap do |url|
|
52
|
+
url << "rants/#{@rant['id']}" if @rant['id']
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/rrant/remote.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'rrant/helper'
|
4
|
+
require 'rrant/error'
|
5
|
+
|
6
|
+
module Rrant
|
7
|
+
# Public: Contains necessary logic to contanct remote API, fetch rants and
|
8
|
+
# images. Needs to be initialized with store object, since we add fetched
|
9
|
+
# rants to it.
|
10
|
+
class Remote
|
11
|
+
include Helper
|
12
|
+
|
13
|
+
attr_reader :rants
|
14
|
+
|
15
|
+
BASE_URL = 'https://www.devrant.io/api/devrant/rants?app=3'
|
16
|
+
MAX_CYCLE = 10
|
17
|
+
SLEEP = 0.4
|
18
|
+
|
19
|
+
# Public: Constructor.
|
20
|
+
#
|
21
|
+
# store - Instance of Rrant::Store.
|
22
|
+
# skip_images - Boolean, when set we won't download images.
|
23
|
+
def initialize(store, skip_images = true)
|
24
|
+
raise Error::InvalidStore unless store.is_a?(Store)
|
25
|
+
|
26
|
+
@rants = []
|
27
|
+
@store = store
|
28
|
+
@skip_images = skip_images
|
29
|
+
end
|
30
|
+
|
31
|
+
# Public: Calls rant fetching methods, adds fetched rants to store and
|
32
|
+
# puts basic info to STDOUT. Since rants are fetched in batches of 20,
|
33
|
+
# we dont want to discard some of them, thus we accept minimum amount
|
34
|
+
# and are ok with more rants fetched.
|
35
|
+
#
|
36
|
+
# min_amount - Integer, fetch at least this amount of rants.
|
37
|
+
#
|
38
|
+
# Returns an array of rants.
|
39
|
+
def save(min_amount)
|
40
|
+
@min_amount = min_amount
|
41
|
+
log_start
|
42
|
+
build_rants
|
43
|
+
fetch_images
|
44
|
+
@store.add(@rants)
|
45
|
+
log_finish
|
46
|
+
|
47
|
+
@rants
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def log_start
|
53
|
+
puts '>> Download started!'
|
54
|
+
end
|
55
|
+
|
56
|
+
def log_finish
|
57
|
+
puts ">> #{@rants.size} Rants downloaded and stored!"
|
58
|
+
end
|
59
|
+
|
60
|
+
# Private: Fetches rants from remote API. Filters out rants whic are
|
61
|
+
# already stored, waits some time so we won't endanger the API.
|
62
|
+
# Then it recursivelly calls itself 'build_allowed?' condition is true.
|
63
|
+
#
|
64
|
+
# cycle - Integer.
|
65
|
+
#
|
66
|
+
# Returns self.
|
67
|
+
def build_rants(cycle = 0)
|
68
|
+
fetch_rants(cycle)
|
69
|
+
filter_rants
|
70
|
+
sleep SLEEP
|
71
|
+
return unless build_allowed?.call(cycle)
|
72
|
+
|
73
|
+
build_rants(cycle + 1)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Private: Wrapper method around lambda. Checks whether we already fetched
|
77
|
+
# necessary amount of rants, or if we were requesting API to many times.
|
78
|
+
def build_allowed?
|
79
|
+
->(cycle) { @rants.size < @min_amount && cycle < MAX_CYCLE }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Private: Grabs rant IDs from store and checks them agains '@rants',
|
83
|
+
# which is then mutated to reject rants with same IDs as stored ones.
|
84
|
+
def filter_rants
|
85
|
+
stored_ids = @store.ids
|
86
|
+
|
87
|
+
@rants = @rants.reject do |rant|
|
88
|
+
stored_ids.include?(rant['id'])
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Private: Contacts API and gets array of rants from the response.
|
93
|
+
def fetch_rants(skip = 0, sort = :algo)
|
94
|
+
response = HTTParty.get(build_url(skip, sort))
|
95
|
+
@rants += response['rants']
|
96
|
+
end
|
97
|
+
|
98
|
+
# Private: Iterates over '@rants' and calls 'fetch_image' for each of them
|
99
|
+
# only when it has some image attached. Sleeps some time after fetch.
|
100
|
+
def fetch_images
|
101
|
+
return unless @skip_images
|
102
|
+
|
103
|
+
@rants.each do |rant|
|
104
|
+
next if image_blank?(rant)
|
105
|
+
fetch_image(rant)
|
106
|
+
sleep SLEEP
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Private: Grabs image URL from rant, downloads it and stores it to path
|
111
|
+
# defined in '@store.images'.
|
112
|
+
def fetch_image(rant)
|
113
|
+
url = rant['attached_image']['url']
|
114
|
+
download = open(url)
|
115
|
+
path = @store.images + url.split('/')[-1]
|
116
|
+
IO.copy_stream(download, path)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Private: taps into 'BASE_URL' and adds parameters to it.
|
120
|
+
#
|
121
|
+
# skip - Integer, defines how many rants we want to skip.
|
122
|
+
# sort - Symbol, defines rant sorting mechanism.
|
123
|
+
#
|
124
|
+
# Returns string.
|
125
|
+
def build_url(skip, sort)
|
126
|
+
BASE_URL.tap do |url|
|
127
|
+
skip != 0 && url << "&skip=#{skip * 20}"
|
128
|
+
url << "&sort=#{sort}"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/lib/rrant/store.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'pstore'
|
2
|
+
require 'rrant/error'
|
3
|
+
require 'rrant/helper'
|
4
|
+
|
5
|
+
module Rrant
|
6
|
+
# Public: Handles local storage using PStore and directory structure
|
7
|
+
# initialization. Serializes given rants for proper storage.
|
8
|
+
class Store
|
9
|
+
include Helper
|
10
|
+
|
11
|
+
attr_reader :root, :images, :store
|
12
|
+
|
13
|
+
# Constructor: Initializes directories and store.
|
14
|
+
#
|
15
|
+
# path - Integer, directory where we want to store our rants/images
|
16
|
+
def initialize(path = nil)
|
17
|
+
path = path || Dir.home
|
18
|
+
raise Error::InvalidPath unless Dir.exist?(path)
|
19
|
+
|
20
|
+
@root = "#{path}/.rrant"
|
21
|
+
@images = "#{@root}/images/"
|
22
|
+
|
23
|
+
initialize_directories
|
24
|
+
initialize_store
|
25
|
+
end
|
26
|
+
|
27
|
+
# Public: Adds serialized rants as 'entities' and 'ids' to the store.
|
28
|
+
def add(rants)
|
29
|
+
@store.transaction do
|
30
|
+
@store[:ids] += build_ids(rants)
|
31
|
+
@store[:entities] += build_entities(rants)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def empty?
|
36
|
+
ids.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
# Public: Gets array of 'entities' or 'ids' from the store.
|
40
|
+
%i(ids entities).each do |bucket|
|
41
|
+
define_method(bucket) do
|
42
|
+
@store.transaction { @store[bucket] }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Public: Finds rant with given ID and updates its 'viewed_at' parameter.
|
47
|
+
def touch(rant_id)
|
48
|
+
@store.transaction do
|
49
|
+
@store[:entities] = @store[:entities].map do |rant|
|
50
|
+
rant['viewed_at'] = DateTime.now if rant['id'] == rant_id
|
51
|
+
rant
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
# Private: Creates directory structure if there isn't one.
|
59
|
+
def initialize_directories
|
60
|
+
Dir.mkdir(@root) unless Dir.exist?(@root)
|
61
|
+
Dir.mkdir(@images) unless Dir.exist?(@images)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Private: Initializes PStore to '@store' variable.
|
65
|
+
# If there are no 'ids' or 'entities' in store, it creates them.
|
66
|
+
def initialize_store
|
67
|
+
@store = PStore.new("#{@root}/store.pstore")
|
68
|
+
!ids && initialize_ids
|
69
|
+
!entities && initialize_entities
|
70
|
+
end
|
71
|
+
|
72
|
+
def build_ids(rants)
|
73
|
+
rants.map { |rant| rant['id'] }
|
74
|
+
end
|
75
|
+
|
76
|
+
def build_entities(rants)
|
77
|
+
rants.map { |rant| inject_rant(rant) }
|
78
|
+
end
|
79
|
+
|
80
|
+
# Private: Adds additional parameters to rant.
|
81
|
+
#
|
82
|
+
# rant - Hash.
|
83
|
+
def inject_rant(rant)
|
84
|
+
rant.tap do |injected|
|
85
|
+
injected['created_at'] = DateTime.now
|
86
|
+
injected['viewed_at'] = injected['viewed_at'] || nil
|
87
|
+
injected['image'] = image_for(injected)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Private: Adds 'attached_image' parameter to rant pointing to image
|
92
|
+
# stored in '@images' directory.
|
93
|
+
#
|
94
|
+
# rant - Hash.
|
95
|
+
def image_for(rant)
|
96
|
+
return nil if image_blank?(rant)
|
97
|
+
|
98
|
+
@images + rant['attached_image']['url'].split('/')[-1]
|
99
|
+
end
|
100
|
+
|
101
|
+
# Private: sets 'entities' and 'ids' in store to empty array.
|
102
|
+
%i(ids entities).each do |bucket|
|
103
|
+
define_method("initialize_#{bucket}") do
|
104
|
+
@store.transaction { @store[bucket] = [] }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/rrant.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rrant/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rrant'
|
8
|
+
spec.version = Rrant::VERSION
|
9
|
+
spec.authors = ['Tomas Koutsky']
|
10
|
+
spec.email = ['tomas@stepnivlk.net']
|
11
|
+
|
12
|
+
spec.summary = %q{simple devrant cli}
|
13
|
+
spec.description = %q{Have you ever wanted to read your devRant in console? Now you can!}
|
14
|
+
spec.homepage = 'https://github.com/stepnivlk/rrant'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'httparty', '~> 0.14'
|
25
|
+
spec.add_runtime_dependency 'catpix', '~> 0.2'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
31
|
+
spec.add_development_dependency 'vcr', '~> 3.0'
|
32
|
+
spec.add_development_dependency 'webmock', '~> 2.3'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rrant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomas Koutsky
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: catpix
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.10'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.10'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '2.3'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '2.3'
|
125
|
+
description: Have you ever wanted to read your devRant in console? Now you can!
|
126
|
+
email:
|
127
|
+
- tomas@stepnivlk.net
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".hound.yml"
|
134
|
+
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".travis.yml"
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/rrant
|
143
|
+
- bin/setup
|
144
|
+
- files/bill.jpg
|
145
|
+
- files/devrant.png
|
146
|
+
- lib/rrant.rb
|
147
|
+
- lib/rrant/error.rb
|
148
|
+
- lib/rrant/handler.rb
|
149
|
+
- lib/rrant/helper.rb
|
150
|
+
- lib/rrant/local.rb
|
151
|
+
- lib/rrant/output.rb
|
152
|
+
- lib/rrant/remote.rb
|
153
|
+
- lib/rrant/store.rb
|
154
|
+
- lib/rrant/version.rb
|
155
|
+
- rrant.gemspec
|
156
|
+
homepage: https://github.com/stepnivlk/rrant
|
157
|
+
licenses:
|
158
|
+
- MIT
|
159
|
+
metadata: {}
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubyforge_project:
|
176
|
+
rubygems_version: 2.6.8
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: simple devrant cli
|
180
|
+
test_files: []
|