Class: Badger::Badger

Inherits:
Array
  • Object
show all
Defined in:
lib/badger/badger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Badger

Returns a new instance of Badger.



5
6
7
# File 'lib/badger/badger.rb', line 5

def initialize url
  @url = url
end

Instance Attribute Details

#github_slugObject (readonly)

Returns the value of attribute github_slug.



3
4
5
# File 'lib/badger/badger.rb', line 3

def github_slug
  @github_slug
end

#ownerObject (readonly)

Returns the value of attribute owner.



3
4
5
# File 'lib/badger/badger.rb', line 3

def owner
  @owner
end

Class Method Details

.find_license(dir) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/badger/helpers.rb', line 64

def Badger.find_license dir
  license_file = (Dir.entries dir).select { |i| i =~ /LICENSE/ }[0]

  if license_file
    words = File.open(File.join(dir, license_file), 'r').read
    Config.instance.licenses.each_pair do |k, v|
      if words =~ /#{v['regex']}/im
        return k
      end
    end
  end

  nil
end

.gemfiles(dir) ⇒ Object



57
58
59
60
61
62
# File 'lib/badger/helpers.rb', line 57

def Badger.gemfiles dir
  targets = []
  targets += (Dir.entries dir).select { |i| i =~ /Gemfile/ }
  targets += spec_files dir
  targets
end

.git_remote(dir) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/badger/helpers.rb', line 11

def Badger.git_remote dir
  begin
    remote = nil
    is_repo?(dir).remotes.each do |r|
      if r.url.match /github.com/
        remote = r.url
      end
    end

  rescue NoMethodError
    puts 'This repo does not appear to have a github remote'
    exit 2
  end
  if remote.nil?
    puts 'This repo does not appear to have a github remote'
    exit 2
  end

  remote
end

.has_coveralls?(dir) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/badger/helpers.rb', line 44

def Badger.has_coveralls? dir
  lines = []
  gemfiles(dir).each do |gemfile|
    lines += File.readlines(File.join(dir, gemfile))
  end

  lines.grep(/coveralls/).any?
end

.has_gemfile?(dir) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/badger/helpers.rb', line 40

def Badger.has_gemfile? dir
  gemfiles(dir).any?
end

.has_travis?(dir) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/badger/helpers.rb', line 36

def Badger.has_travis? dir
  ((Dir.entries dir).select { |i| '.travis.yml' == i }).any?
end

.is_repo?(dir) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
# File 'lib/badger/helpers.rb', line 2

def Badger.is_repo? dir
  begin
    Git.open(dir)
  rescue ArgumentError
    puts 'Run this from inside a git repo'
    exit 1
  end
end

.search_gemspec(dir) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/badger/helpers.rb', line 79

def Badger.search_gemspec dir
  spec_file = spec_files(dir)[0]

  if spec_file
    params            = {}
    gs                = File.readlines(File.join(dir, spec_file))
    params[:rubygem]  = eval (gs.grep /\.name /)[0].sub(/^\s.*\./, '')
    lines = (gs.grep /licenses?/)
    if lines.any?
      l                 = eval (lines[0]).sub(/^\s.*\./, '')
      params[:licenses] = l.class.name == 'Array' ? l : [l]
    end
  end

  params || nil
end

.slug_extract(remote_url) ⇒ Object



32
33
34
# File 'lib/badger/helpers.rb', line 32

def Badger.slug_extract remote_url
  remote_url.match(/.*github.com[\/:](.*)/)[1].gsub(/.git$/, '')
end

.spec_files(dir) ⇒ Object



53
54
55
# File 'lib/badger/helpers.rb', line 53

def Badger.spec_files dir
  (Dir.entries dir).select { |i| i =~ /gemspec/ }
end

Instance Method Details

#add(service) ⇒ Object



17
18
19
20
# File 'lib/badger/badger.rb', line 17

def add service
  self << Service.badge(service, github_slug)
  self.delete nil
end

#badge_type(type) ⇒ Object



36
37
38
# File 'lib/badger/badger.rb', line 36

def badge_type type
  Config.instance.config['badge_type'] = type
end

#bonusObject



31
32
33
34
# File 'lib/badger/badger.rb', line 31

def bonus
  self.uniq!
  self << Bonus.badge(self)
end

#license(type) ⇒ Object



22
23
24
25
# File 'lib/badger/badger.rb', line 22

def license type
  self << License.badge(type, owner)
  self.delete nil
end

#rubygem(name) ⇒ Object



27
28
29
# File 'lib/badger/badger.rb', line 27

def rubygem name
  self << Rubygem.badge(name)
end

#style(style) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/badger/badger.rb', line 40

def style style
  unless Config.instance.config['valid_styles'].include? style
    puts "Invalid style choice '#{style}'"
    exit 3
  end

  Config.instance.config['badge_style'] = style
end

#to_sObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/badger/badger.rb', line 49

def to_s
  self.uniq!

  s = ''
  self.each do |badge|
    s << badge
    s << "\n"
  end

  s
end