ebooks 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89faedd51fce5d08320b71bcdc1c66596d03f738
4
- data.tar.gz: 0fae632c95ecdc2e76cfc17f988f4f5074153b69
3
+ metadata.gz: 5b92abdf753365a92607b400fdf303b6e96f5e9a
4
+ data.tar.gz: ee4a3d4cd0c3b6ebceb504120f0eda5feb87bcde
5
5
  SHA512:
6
- metadata.gz: ae09c854c570138b325224c1d55070c5b631e2055d38ddbf72d26b8e879c95f4964c5be1afed090606056e70f9b54ec6e14e40f6994950e6428f3c6cdb320b31
7
- data.tar.gz: 31b8ff107d3dcc4e7b73b15a2db3f39e855b5c450bb1999f29a2da22548aa18b9e2e64a833fdd4afa8c0f66232c17295e39cdeb9f978f3c1d29db08e92a8830e
6
+ metadata.gz: 9934d51b0e7905b64b6c35ec05b4de67a28e20650c117849352b698fde1a33a59e43cf741bed03f060b47d35cc493017ef87e3d0135f7180f272b7494741ec4f
7
+ data.tar.gz: e50bc685459f82a615e68cfeef3a55d7dba77ebdecdef419ecce71fb2133a9aae25ef912b86f15e549ac11f0a239c98f5412117c878e93a392d044f010b81785
data/.gitignore CHANGED
@@ -15,3 +15,7 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ dictionary.mmd
20
+ markov_dict.txt
21
+ tweets.csv
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Ebooks
2
2
 
3
- Generate your own horse_ebooks by @busterbenson and compiled as a gem by @parkr
3
+ Generate your own horse_ebooks by [@busterbenson][] ([original gist][], [blog post][]) and compiled as a gem by [@parkr][]
4
+
5
+ [@busterbenson]: http://wayoftheduck.com/
6
+ [original gist]: https://gist.github.com/busterbenson/6695350
7
+ [blog post]: http://wayoftheduck.com/diy-horse-ebooks
8
+ [@parkr]: https://parkermoo.re
4
9
 
5
10
  ## Installation
6
11
 
@@ -16,9 +21,50 @@ Or install it yourself as:
16
21
 
17
22
  $ gem install ebooks
18
23
 
24
+ ## Configuration
25
+
26
+ The `ebooks` executable and `Ebooks.read_config_file` will load in a yaml file
27
+ (`~/.ebooks` by default) if you want to set your own defaults.
28
+
29
+ Here are the gem's defaults:
30
+
31
+ ```yaml
32
+ :tweets_csv_path: 'tweets.csv'
33
+ :corpus_path: 'markov_dict.txt'
34
+ :dictionary_name: 'dictionary'
35
+ :twitter:
36
+ :consumer_key: ''
37
+ :consumer_secret: ''
38
+ :oauth_token: ''
39
+ :oauth_token_secret: ''
40
+ ```
41
+
19
42
  ## Usage
20
43
 
21
- TODO: Write usage instructions here
44
+ As an API:
45
+
46
+ ```ruby
47
+ config = Ebooks.configuration(my_overrides)
48
+
49
+ # Generate a sentence and return it
50
+ Ebooks.generate(config)
51
+
52
+ # Generate sentence and tweet it
53
+ Ebooks.tweet(config)
54
+
55
+ # Just tweet a sentence
56
+ Ebooks::Twitter.new(config[:twitter]).tweet(my_sentence)
57
+ ```
58
+
59
+ As an executable:
60
+
61
+ ```bash
62
+ # To generate a new horse_ebooks sentence:
63
+ ebooks generate
64
+
65
+ # To generate a new sentence and tweet it:
66
+ ebooks tweet
67
+ ```
22
68
 
23
69
  ## Contributing
24
70
 
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :console do
4
+ sh "irb -r./lib/ebooks.rb"
5
+ end
@@ -0,0 +1,15 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
4
+
5
+ require 'ebooks'
6
+
7
+ config = Ebooks.read_config_file
8
+
9
+ if ARGV[0] == "generate"
10
+ puts Ebooks.generate(config)
11
+ elsif ARGV[0] == "tweet"
12
+ Ebooks.tweet(config)
13
+ else
14
+ abort "Don't understand '#{ARGV[1]}'. Valid commands are: 'generate', 'tweet'."
15
+ end
@@ -1,21 +1,50 @@
1
- require "ebooks/version"
2
-
3
1
  require 'rubygems'
4
2
  require 'thread'
5
3
  require 'csv'
6
4
  require 'twitter'
7
5
  require 'marky_markov'
6
+ require 'yaml'
7
+
8
+ $:.unshift(File.dirname(__FILE__))
9
+
10
+ require 'ebooks/version'
11
+ require 'ebooks/core_ext'
8
12
 
9
13
  module Ebooks
10
14
  autoload :Generator, 'ebooks/generator'
11
15
  autoload :Twitter, 'ebooks/twitter'
12
16
 
17
+ def self.read_config_file(file = '~/.ebooks')
18
+ contents = File.read(file.sub('~', ENV["HOME"]))
19
+ YAML.load(contents)
20
+ end
21
+
13
22
  def self.configuration(overrides = {})
23
+ {
24
+ :tweets_csv_path => 'tweets.csv',
25
+ :corpus_path => 'markov_dict.txt',
26
+ :dictionary_name => 'dictionary', # don't include the .mmd
27
+ :twitter => {
28
+ :consumer_key => '',
29
+ :consumer_secret => '',
30
+ :oauth_token => '',
31
+ :oauth_token_secret => ''
32
+ }
33
+ }.deep_merge(overrides)
34
+ end
14
35
 
36
+ def self.generate(overrides = {})
37
+ config = self.configuration(overrides)
38
+ generator = Ebooks::Generator.new(config)
39
+
40
+ generator.generate_sentence
15
41
  end
16
42
 
17
- def self.generate(config = {})
18
- config = self.configuration(config)
43
+ def self.tweet(overrides = {})
44
+ config = self.configuration(overrides)
45
+
46
+ sentence = self.generate(overrides)
19
47
 
48
+ Ebooks::Twitter.new(config[:twitter]).tweet(sentence)
20
49
  end
21
50
  end
@@ -0,0 +1,15 @@
1
+ class Hash
2
+ def deep_merge(hash)
3
+ target = dup
4
+
5
+ hash.keys.each do |key|
6
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
7
+ target[key] = target[key].deep_merge(hash[key])
8
+ end
9
+
10
+ target[key] = hash[key]
11
+ end
12
+
13
+ target
14
+ end
15
+ end
@@ -1,13 +1,23 @@
1
1
  module Ebooks
2
- module Generator
2
+ class Generator
3
3
 
4
- def self.generate_twitter_corpus(tweets_csv_path = 'tweets.csv', corpus_path = 'markov_dict.txt')
4
+ attr_accessor :dictionary
5
+
6
+ def initialize(config)
7
+ @tweets_csv_path = config[:tweets_csv_path]
8
+ @corpus_path = config[:corpus_path]
9
+ build_corpus
10
+ @dictionary_name = config[:dictionary_name]
11
+ @dictionary = build_dictionary
12
+ end
13
+
14
+ def generate_twitter_corpus
5
15
  # Go to Twitter.com -> Settings -> Download Archive.
6
16
  # This tweets.csv file is in the top directory. Put it in the same directory as this script.
7
- csv_text = CSV.parse(File.read(tweets_csv_path))
17
+ csv_text = CSV.parse(File.read(@tweets_csv_path))
8
18
 
9
19
  # Create a new clean file of text that acts as the seed for your Markov chains
10
- File.open(corpus_path, 'w') do |file|
20
+ File.open(@corpus_path, 'w') do |file|
11
21
  csv_text.reverse.each do |row|
12
22
  # Strip links and new lines
13
23
  tweet_text = row[5].gsub(/(?:f|ht)tps?:\/[^\s]+/, '').gsub(/\n/,' ')
@@ -17,12 +27,32 @@ module Ebooks
17
27
  end
18
28
  end
19
29
 
20
- def self.generate_sentence(corpus_path = 'markov_dict.txt')
30
+ def generate_sentence
21
31
  # Run when you want to generate a new Markov tweet
22
- markov = MarkyMarkov::Dictionary.new('dictionary') # Saves/opens dictionary.mmd
23
- markov.parse_file(corpus_path)
24
- tweet_text = markov.generate_n_sentences(2).split(/\#\</).first.chomp.chop
25
- markov.save_dictionary!
32
+ dictionary.generate_n_sentences(2).split(/\#\</).first.chomp.chop
33
+ end
34
+
35
+ private
36
+
37
+ def build_corpus
38
+ unless File.exists?(@corpus_path)
39
+ generate_twitter_corpus
40
+ end
41
+ end
42
+
43
+ def build_dictionary
44
+ if File.exists?(dictionary_path)
45
+ MarkyMarkov::Dictionary.new(@dictionary_name)
46
+ else
47
+ markov = MarkyMarkov::Dictionary.new(@dictionary_name)
48
+ markov.parse_file(@corpus_path)
49
+ markov.save_dictionary!
50
+ markov
51
+ end
52
+ end
53
+
54
+ def dictionary_path
55
+ "#{@dictionary_name}.mmd"
26
56
  end
27
57
 
28
58
  end
@@ -1,5 +1,5 @@
1
1
  module Ebooks
2
- module Twitter
2
+ class Twitter
3
3
 
4
4
  def initialize(credentials = {})
5
5
  @consumer_key = credentials.fetch(:consumer_key)
@@ -7,15 +7,16 @@ module Ebooks
7
7
  @access_token = credentials.fetch(:oauth_token)
8
8
  @access_token_secret = credentials.fetch(:oauth_token_secret)
9
9
 
10
- Twitter.configure do |config|
10
+ ::Twitter.configure do |config|
11
11
  config.consumer_key = @consumer_key
12
12
  config.consumer_secret = @consumer_secret
13
13
  end
14
14
  end
15
15
 
16
16
  def tweet(tweet_text)
17
- twitter_client = Twitter::Client.new(:oauth_token => @access_token,
17
+ twitter_client = ::Twitter::Client.new(:oauth_token => @access_token,
18
18
  :oauth_token_secret => @access_token_secret)
19
+ tweet_text = tweet_text.gsub('@', '')[0..139]
19
20
  p "#{Time.now}: #{tweet_text}"
20
21
  twitter_client.update(tweet_text)
21
22
  end
@@ -1,3 +1,3 @@
1
1
  module Ebooks
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ebooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Moore
@@ -83,7 +83,8 @@ dependencies:
83
83
  description: Generate your own horse_ebooks for fun and for profit
84
84
  email:
85
85
  - parkrmoore@gmail.com
86
- executables: []
86
+ executables:
87
+ - ebooks
87
88
  extensions: []
88
89
  extra_rdoc_files: []
89
90
  files:
@@ -92,8 +93,10 @@ files:
92
93
  - LICENSE.txt
93
94
  - README.md
94
95
  - Rakefile
96
+ - bin/ebooks
95
97
  - ebooks.gemspec
96
98
  - lib/ebooks.rb
99
+ - lib/ebooks/core_ext.rb
97
100
  - lib/ebooks/generator.rb
98
101
  - lib/ebooks/twitter.rb
99
102
  - lib/ebooks/version.rb