Class: HbExporter::Board
- Inherits:
-
Object
- Object
- HbExporter::Board
- Includes:
- Helper::RecursivelyFetch
- Defined in:
- lib/hb_exporter/board.rb
Constant Summary collapse
- THREAD_COUNT =
10
Instance Attribute Summary collapse
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#id ⇒ Object
Returns the value of attribute id.
-
#pins_count ⇒ Object
Returns the value of attribute pins_count.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #export_pins ⇒ Object
-
#initialize(id, opt = {}) ⇒ Board
constructor
A new instance of Board.
- #list_pins ⇒ Object
- #load_data ⇒ Object
- #pins ⇒ Object
- #to_s ⇒ Object
Methods included from Helper::RecursivelyFetch
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
#desc ⇒ Object
Returns the value of attribute desc.
13 14 15 |
# File 'lib/hb_exporter/board.rb', line 13 def desc @desc end |
#id ⇒ Object
Returns the value of attribute id.
13 14 15 |
# File 'lib/hb_exporter/board.rb', line 13 def id @id end |
#pins_count ⇒ Object
Returns the value of attribute pins_count.
13 14 15 |
# File 'lib/hb_exporter/board.rb', line 13 def pins_count @pins_count end |
#title ⇒ Object
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_pins ⇒ Object
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 .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 .increment counter -= 1 end } end while counter > 0; end .finish end |
#list_pins ⇒ Object
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_data ⇒ Object
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 |
#pins ⇒ Object
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_s ⇒ Object
41 42 43 |
# File 'lib/hb_exporter/board.rb', line 41 def to_s "#<board##{id} #{title} - #{desc}>" end |