Class: HbExporter::Board

Inherits:
Object
  • Object
show all
Includes:
Helper::RecursivelyFetch
Defined in:
lib/hb_exporter/board.rb

Constant Summary collapse

THREAD_COUNT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::RecursivelyFetch

#recursively_fetch

Constructor Details

#initialize(id, opt = {}) ⇒ Board

Returns a new instance of Board.



21
22
23
24
25
26
# File 'lib/hb_exporter/board.rb', line 21

def initialize(id, opt={})
  @id         = id
  @title      = opt[:title]
  @desc       = opt[:desc]
  @pins_count = opt[:pins_count]
end

Instance Attribute Details

#descObject

Returns the value of attribute desc.



13
14
15
# File 'lib/hb_exporter/board.rb', line 13

def desc
  @desc
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/hb_exporter/board.rb', line 13

def id
  @id
end

#pins_countObject

Returns the value of attribute pins_count.



13
14
15
# File 'lib/hb_exporter/board.rb', line 13

def pins_count
  @pins_count
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/hb_exporter/board.rb', line 13

def title
  @title
end

Class Method Details

.load(id) ⇒ Object



16
17
18
# File 'lib/hb_exporter/board.rb', line 16

def self.load(id)
  new(id).tap &:load_data
end

Instance Method Details

#export_pinsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hb_exporter/board.rb', line 67

def export_pins
  board_path = prepare_export_path
  counter    = pins.size

  puts "downloading ".cyan << title
  progress_bar.reset

  pin_grps = pins.group_by.with_index { |_, i| i % THREAD_COUNT }.each_value do |pins|
    Thread.new {
      pins.each do |pin|
        pin.export path: board_path
        progress_bar.increment
        counter -= 1
      end
    }
  end

  while counter > 0; end

  progress_bar.finish
end

#list_pinsObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hb_exporter/board.rb', line 46

def list_pins
  return if pins.empty?

  puts [
    "key".rjust(60),
    "image url"
  ].join(" ")

  pins.each do |pin|
    puts "#{pin.key.to_s.rjust(60).cyan} #{pin.image_url}"
  end
end

#load_dataObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/hb_exporter/board.rb', line 29

def load_data
  opts = {
    headers: { 'X-Requested-With' => 'XMLHttpRequest' }
  }
  HTTParty.get(api_path, opts)['board'].tap do |data|
    @title      = data['title']
    @desc       = data['description']
    @pins_count = data['pin_count']
  end
end

#pinsObject



60
61
62
63
64
# File 'lib/hb_exporter/board.rb', line 60

def pins
  @pins ||= fetch_pins.map do |data|
    Pin.new(data['file']['key'], data: data)
  end
end

#to_sObject



41
42
43
# File 'lib/hb_exporter/board.rb', line 41

def to_s
  "#<board##{id} #{title} - #{desc}>"
end