Class: Coderbits::Glue

Inherits:
Object
  • Object
show all
Defined in:
lib/coderbits/glue.rb

Constant Summary collapse

OPTIONS =

valid options

[
  :badge_size, :save_to, :layout, :style, :library, :selector,
  :padding, :margin, :nocomments, :output_image, :output_style,
  :width, :height
]

Instance Method Summary collapse

Constructor Details

#initialize(username, options = {}) ⇒ Glue

Returns a new instance of Glue.



13
14
15
16
# File 'lib/coderbits/glue.rb', line 13

def initialize username, options = {}
  safe options
  @username = username
end

Instance Method Details

#download_imagesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/coderbits/glue.rb', line 30

def download_images
  # get user data
  data = Coderbits::Client.new.get @username

  # collect images
  images = data[:badges].select{ |b| b[:earned] }.map { |b| b[:image_link].match(/(.*)\/(.*)/)[2] }.uniq

  # get connection
  connection = Coderbits::Client.new.connection

  # create directory
  Dir.mkdir @options[:save_to] unless Dir.exists? @options[:save_to]
  Dir.mkdir "#{@options[:save_to]}/badges" unless Dir.exists? "#{@options[:save_to]}/badges"

  # download images
  images.each do |img|
    # specify badge size
    img = change_size img unless @options[:badge_size] == 64

    # download image
    response = connection.get "https://coderbits.com/images/badges/#{img}"
    
    # save to disk
    if response.headers["content-type"] == "image/png"
      File.open("#{@options[:save_to]}/badges/#{img.downcase}", 'wb') { |f| f.write response.body }
    end
  end
end

#generate_htmlObject



63
64
65
# File 'lib/coderbits/glue.rb', line 63

def generate_html
  nil # coming soon
end

#merge_imagesObject



59
60
61
# File 'lib/coderbits/glue.rb', line 59

def merge_images
  SpriteFactory.run! "#{@options[:save_to]}/badges", @options
end

#run!Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/coderbits/glue.rb', line 18

def run!
  imgs = download_images
  css  = merge_images
  html = generate_html

  {
    images: imgs,
    css: css,
    html: html
  }
end

#safe(options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/coderbits/glue.rb', line 67

def safe options
  # select only allowed options
  options = options.select { |k, v| OPTIONS.include? k }

  dflt = {
    badge_size: 64,       # badges size for download
    save_to: './',        # path for files

    layout: 'horizontal', # horizontal, vertical or packed
    style: 'css',         # css, scss or sass
    library: 'chunkypng', # rmagick or chunkypng
    selector: '.badge-',  # selector
    padding: 0,           # add padding to each sprite
    margin: 0,            # add margin to each sprite
    nocomments: true      # suppress generation of comments in output stylesheet
  }

  if options.has_key? :save_to
    save_to = options[:save_to]
  else
    save_to = dflt[:save_to]
  end
  
  dflt[:output_image] = "#{save_to}/badges.png" # output location for generated image
  dflt[:output_style] = "#{save_to}/badges.css" # output location for generated stylesheet

  @options = dflt.merge options
end