Class: Bulkippt::Loader
- Inherits:
-
Object
- Object
- Bulkippt::Loader
- Defined in:
- lib/bulkippt.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #credentials_valid? ⇒ Boolean
- #extract_bookmarks(file_path) ⇒ Object
-
#initialize(service, logger = Logger.new(STDOUT)) ⇒ Loader
constructor
A new instance of Loader.
- #submit_bookmarks(bookmarks) ⇒ Object
Constructor Details
#initialize(service, logger = Logger.new(STDOUT)) ⇒ Loader
Returns a new instance of Loader.
13 14 15 16 |
# File 'lib/bulkippt.rb', line 13 def initialize(service, logger = Logger.new(STDOUT)) @service = service @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/bulkippt.rb', line 11 def logger @logger end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
11 12 13 |
# File 'lib/bulkippt.rb', line 11 def service @service end |
Instance Method Details
#credentials_valid? ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bulkippt.rb', line 18 def credentials_valid? begin @account = @service.account true rescue Kippt::APIError => e false rescue => e @logger.error e. false end end |
#extract_bookmarks(file_path) ⇒ Object
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 58 |
# File 'lib/bulkippt.rb', line 30 def extract_bookmarks(file_path) begin validate_file_path file_path rescue => e @logger.error e. return [] end begin @csv = CSV.read(file_path, {headers: true}) rescue ArgumentError => e @logger.warn e. + " | Forcing UTF-8..." begin data = IO.read(file_path).force_encoding("ISO-8859-1").encode("utf-8", replace: nil) @csv = CSV.parse(data, {headers: true}) rescue => e @logger.error "Failed to force UTF-8 | " + e. return [] end end begin validate_headers(normalize_headers(@csv.headers)) parse_csv(@csv) rescue => e @logger.error e. [] end end |
#submit_bookmarks(bookmarks) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/bulkippt.rb', line 60 def submit_bookmarks(bookmarks) begin submitted = [] bookmarks.each do |bookmark| clip = clip_from_bookmark bookmark if clip.save submitted << clip @logger.info "SUCCESS: #{clip.title} #{clip.url}" else @logger.error "FAILURE: #{clip.title} #{clip.url} | #{clip.errors[]}" end end submitted rescue => e @logger.error e. [] end end |