class MarvelExplorer::Explorer

Attributes

config[RW]

Public Class Methods

new(config_file = " click to toggle source
# 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

Public Instance Methods

calculate_rankings(params) click to toggle source
# 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.message =~ /\->/ }
  .map { |c| /(.*) -> (.*) -> (.*)/.match(c.message)[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() click to toggle source
# 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() click to toggle source
# File lib/marvel_explorer/git.rb, line 11
def commit
  g = Git.open @config['jekyll_dir']

  g.add '.'
  g.commit commit_message
  g.push(g.remote('origin'))
end
commit_message() click to toggle source
# File lib/marvel_explorer/git.rb, line 3
def commit_message
  '%s -> %s -> %s' % [
    yamls['start']['name'],
    yamls['comic']['series']['name'],
    yamls['end']['name']
  ]
end
end_character() click to toggle source
# 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() click to toggle source
# File lib/marvel_explorer/explorer.rb, line 19
def publish
  commit
end
rankings(params = {}) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/marvel_explorer/comic.rb, line 14
def series
  @comic[:title] =~ /(.*) \((.*)\) #(.*)/
  { name: $1, period: $2 }
end
start_character() click to toggle source
# 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() click to toggle source
# File lib/marvel_explorer/explorer.rb, line 15
def tweet
  twitter_client.update tweet_message
end
tweet_message() click to toggle source
# File lib/marvel_explorer/twitter.rb, line 13
def tweet_message
  tm = 'In %s, %s appeared in %s #%s with %s %s' % [
    yamls['comic']['year'],
    yamls['start']['name'],
    yamls['comic']['series']['name'],
    yamls['comic']['issue'],
    yamls['end']['name'],
    @config['marvelexplorer_url']
  ]

  if tm.length > @config['tweet_length'].to_i
    tm = '%s…' % s[0, @config['tweet_length'].to_i - 1]
  end

  tm
end
twitter_client() click to toggle source
# 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() click to toggle source
# File lib/marvel_explorer/explorer.rb, line 10
def update
  yamlise
  save
end
write_yaml(h, path) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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