Class: MarvelExplorer::Explorer
- Inherits:
-
Object
- Object
- MarvelExplorer::Explorer
- Defined in:
- lib/marvel_explorer/git.rb,
lib/marvel_explorer/comic.rb,
lib/marvel_explorer/twitter.rb,
lib/marvel_explorer/yamlise.rb,
lib/marvel_explorer/explorer.rb,
lib/marvel_explorer/rankings.rb,
lib/marvel_explorer/characters.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #calculate_rankings(params) ⇒ Object
- #comic ⇒ Object
- #commit ⇒ Object
- #commit_message ⇒ Object
- #end_character ⇒ Object
-
#initialize(config_file = "#{ENV['HOME']}/.marvel_explorer/config.yml") ⇒ Explorer
constructor
A new instance of Explorer.
- #publish ⇒ Object
- #rankings(params = {}) ⇒ Object
- #save ⇒ Object
- #series ⇒ Object
- #start_character ⇒ Object
- #tweet ⇒ Object
- #tweet_message ⇒ Object
- #twitter_client ⇒ Object
- #update ⇒ Object
- #write_yaml(h, path) ⇒ Object
- #yamlise ⇒ Object
- #yamlise_characters ⇒ Object
- #yamlise_comic ⇒ Object
- #yamls ⇒ Object
Constructor Details
#initialize(config_file = "#{ENV['HOME']}/.marvel_explorer/config.yml") ⇒ Explorer
Returns a new instance of Explorer.
5 6 7 8 |
# File 'lib/marvel_explorer/explorer.rb', line 5 def initialize config_file = "#{ENV['HOME']}/.marvel_explorer/config.yml" @config = YAML.load(File.open(File.join(File.dirname(__FILE__), '..', '..', 'config/defaults.yml'))) @config.merge! YAML.load File.open config_file end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/marvel_explorer/explorer.rb', line 3 def config @config end |
Instance Method Details
#calculate_rankings(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/marvel_explorer/rankings.rb', line 3 def calculate_rankings params params = { commits: 35040, repo: @config['jekyll_dir'], limit: 5 }.merge params g = Git.open params[:repo] counts = Hash.new(0) g.log(params[:commits]) .select{ |c| c. =~ /\->/ } .map { |c| /(.*) -> (.*) -> (.*)/.match(c.)[1] } .each { |c| counts[c] += 1 } counts.sort_by { |k, v| v }.reverse .map { |k, v| { 'name' => k, 'score' => v } }[0...params[:limit]] end |
#comic ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/marvel_explorer/comic.rb', line 3 def comic @comic ||= begin comics = Ultron::Comics.by_character_and_vanilla_comics start_character.id @comic = comics.sample until MarvelExplorer.validate_comic @comic @comic = comics.sample end @comic end end |
#commit ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/marvel_explorer/git.rb', line 11 def commit g = Git.open @config['jekyll_dir'] g.add '.' g.commit g.push(g.remote('origin')) end |
#commit_message ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/marvel_explorer/git.rb', line 3 def '%s -> %s -> %s' % [ yamls['start']['name'], yamls['comic']['series']['name'], yamls['end']['name'] ] end |
#end_character ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/marvel_explorer/characters.rb', line 15 def end_character @end_character ||= begin characters = Ultron::Characters.by_comic comic.id end_character = start_character # we want a different character for the next iteration, obvs. until end_character.id != start_character.id && MarvelExplorer.validate_character(end_character) end_character = characters.sample end end_character end end |
#publish ⇒ Object
19 20 21 |
# File 'lib/marvel_explorer/explorer.rb', line 19 def publish commit end |
#rankings(params = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/marvel_explorer/rankings.rb', line 18 def rankings params = {} FileUtils.mkdir_p '%s/_data/' % @config['jekyll_dir'] y = File.open '%s/_data/rankings_%d.yml' % [ @config['jekyll_dir'], params[:commits], ], 'w' y.write calculate_rankings(params).to_yaml y.close end |
#save ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/marvel_explorer/characters.rb', line 28 def save File.open @config['marshal_file'], 'w' do |file| Marshal.dump end_character, file end FileUtils.mkdir_p @config['cache_dir'] File.open '%s/%d' % [ @config['cache_dir'], end_character[:id] ], 'w' do |file| Marshal.dump end_character, file end end |
#series ⇒ Object
14 15 16 17 |
# File 'lib/marvel_explorer/comic.rb', line 14 def series @comic[:title] =~ /(.*) \((.*)\) #(.*)/ { name: $1, period: $2 } end |
#start_character ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/marvel_explorer/characters.rb', line 3 def start_character @start_character ||= begin File.open @config['marshal_file'] do |file| Marshal.load file end rescue Ultron::Characters.find @config['default_id'] ensure true end end |
#tweet ⇒ Object
15 16 17 |
# File 'lib/marvel_explorer/explorer.rb', line 15 def tweet twitter_client.update end |
#tweet_message ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/marvel_explorer/twitter.rb', line 13 def tm = 'In %s, %s appeared in %s #%s with %s' % [ yamls['comic']['year'], yamls['start']['name'], yamls['comic']['series']['name'], yamls['comic']['issue'], yamls['end']['name'] ] if tm.length > @config['tweet_length'].to_i tm = '%s…' % s[0, @config['tweet_length'].to_i - 1] end tm end |
#twitter_client ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/marvel_explorer/twitter.rb', line 3 def twitter_client twitter_config = { consumer_key: @config['twitter']['consumer']['key'], consumer_secret: @config['twitter']['consumer']['secret'], access_token: @config['twitter']['oauth']['token'], access_token_secret: @config['twitter']['oauth']['secret'] } Twitter::REST::Client.new(twitter_config) end |
#update ⇒ Object
10 11 12 13 |
# File 'lib/marvel_explorer/explorer.rb', line 10 def update yamlise save end |
#write_yaml(h, path) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/marvel_explorer/yamlise.rb', line 36 def write_yaml h, path FileUtils.mkdir_p '%s/_data/' % @config['jekyll_dir'] y = File.open '%s/_data/%s.yml' % [ @config['jekyll_dir'], path ], 'w' y.write h.to_yaml y.close end |
#yamlise ⇒ Object
46 47 48 49 50 51 |
# File 'lib/marvel_explorer/yamlise.rb', line 46 def yamlise FileUtils.mkdir_p '%s/_data' % @config['jekyll_dir'] %w{ comic characters }.each do |item| eval "yamlise_#{item}" end end |
#yamlise_characters ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/marvel_explorer/yamlise.rb', line 3 def yamlise_characters [ 'start', 'end' ].each do |c| h = { 'name' => eval("#{c}_character[:name]"), 'description' => eval("#{c}_character[:description]"), 'url' => eval("#{c}_character[:urls][1]['url']"), 'image' => eval("#{c}_character[:thumbnail]") } write_yaml h, c end end |
#yamlise_comic ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/marvel_explorer/yamlise.rb', line 19 def yamlise_comic h = { 'date' => comic[:dates][0]['date'], 'year' => Date.parse(comic[:dates][0]['date']).strftime('%Y'), 'title' => comic[:title], 'issue' => comic[:issueNumber], 'series' => { 'period' => series[:period], 'name' => series[:name] }, 'url' => comic[:urls][0]['url'], 'image' => comic[:thumbnail] } write_yaml h, 'comic' end |
#yamls ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/marvel_explorer/yamlise.rb', line 53 def yamls @yamls ||= begin yamls = {} %w{ start end comic }.each do |thing| begin file = File.open '%s/_data/%s.yml' % [ @config['jekyll_dir'], thing ] rescue Exception update file = File.open '%s/_data/%s.yml' % [ @config['jekyll_dir'], thing ] end y = YAML.load file yamls[thing] = y end yamls end end |