Class: Bulkippt::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/bulkippt.rb', line 11

def logger
  @logger
end

#serviceObject (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

Returns:

  • (Boolean)


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

def credentials_valid?
  begin
    @account = @service.
    true
  rescue Kippt::APIError => e
    false
  rescue => e
    @logger.error e.message
    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.message
    return []
  end

  begin
    @csv = CSV.read(file_path, {headers: true})
  rescue ArgumentError => e
    @logger.warn e.message + " | 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.message
      return []
    end
  end

  begin
    validate_headers(normalize_headers(@csv.headers))
    parse_csv(@csv)
  rescue => e
    @logger.error e.message
    []
  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
     = []
    bookmarks.each do |bookmark|
      clip = clip_from_bookmark bookmark
      if clip.save
         << clip
        @logger.info "SUCCESS: #{clip.title} #{clip.url}"
      else
        @logger.error "FAILURE: #{clip.title} #{clip.url} | #{clip.errors[]}"
      end
    end
    
  rescue => e
    @logger.error e.message
    []
  end
end