Class: MarvelExplorer::Explorer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configObject

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.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

#comicObject



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

#commitObject



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 commit_message
  g.push(g.remote('origin'))
end

#commit_messageObject



3
4
5
6
7
8
9
# 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_characterObject



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

#publishObject



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

#saveObject



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

#seriesObject



14
15
16
17
# File 'lib/marvel_explorer/comic.rb', line 14

def series
  @comic[:title] =~ /(.*) \((.*)\) #(.*)/
  { name: $1, period: $2 }
end

#start_characterObject



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

#tweetObject



15
16
17
# File 'lib/marvel_explorer/explorer.rb', line 15

def tweet
  twitter_client.update tweet_message
end

#tweet_messageObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/marvel_explorer/twitter.rb', line 13

def tweet_message
  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_clientObject



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

#updateObject



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

#yamliseObject



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_charactersObject



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_comicObject



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

#yamlsObject



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