bulkippt 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Bulkippt
2
2
 
3
- TODO: Write a gem description
3
+ Allows you to upload your bookmarks to kippt.com in bulk. You'll need a kippt.com account (of course) from which you'll obtain your API token along with your username. From there, all you need is a CSV file with url, title and description headers and the loader will push those links to kippt.com on your behalf.
4
4
 
5
5
  ## Installation
6
6
 
@@ -17,8 +17,18 @@ Or install it yourself as:
17
17
  $ gem install bulkippt
18
18
 
19
19
  ## Usage
20
-
21
- TODO: Write usage instructions here
20
+ ```ruby
21
+ require 'kippt'
22
+ require 'bulkippt'
23
+ require 'yaml' # if you store your creds in a yaml file
24
+
25
+ creds = YAML.load(File.open(File.expand_path('./config/my_kippt_creds.yml')))
26
+ service = Kippt::Client.new(username: creds['username'], token: creds['token'])
27
+ loader = Bulkippt::Loader.new(client, Logger.new(STDOUT))
28
+ csv_path = File.expand_path('./my_bookmarks.csv')
29
+ bookmarks = loader.extract_bookmarks csv_path
30
+ submitted = loader.submit_bookmarks bookmarks
31
+ ```
22
32
 
23
33
  ## Contributing
24
34
 
@@ -0,0 +1,42 @@
1
+ require 'ostruct'
2
+
3
+ module Bulkippt
4
+
5
+ class FakeService
6
+
7
+ attr_reader :username, :token, :clips
8
+
9
+ def initialize(username, token)
10
+ @username = username
11
+ @token = token
12
+ @clips = Clips.new
13
+ end
14
+
15
+ def account
16
+ if credentials_valid?(@username, @token)
17
+ {username: @username, token: @token}.to_json
18
+ else
19
+ raise Kippt::APIError, "Can't find an user with this username and api_key"
20
+ end
21
+ end
22
+
23
+ def credentials_valid?(username, token)
24
+ return false if username != 'valid' && @token != 'valid'
25
+ true
26
+ end
27
+
28
+ end
29
+
30
+ class Clips
31
+ def build
32
+ Clip.new
33
+ end
34
+ end
35
+
36
+ class Clip < OpenStruct
37
+ def save
38
+ self
39
+ end
40
+ end
41
+
42
+ end
@@ -1,3 +1,3 @@
1
1
  module Bulkippt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/bulkippt.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "bulkippt/version"
2
+ require "bulkippt/fake_service"
2
3
 
3
4
  require 'csv'
4
5
  require 'logger'
@@ -28,40 +29,20 @@ module Bulkippt
28
29
 
29
30
  def extract_bookmarks(file_path)
30
31
  begin
31
- raise Exception, "CSV not found: #{file_path}" if !File.exists?(file_path)
32
-
32
+ validate_file_path file_path
33
33
  @csv = CSV.read(file_path, {headers: true})
34
- headers = @csv.headers.map(&:downcase) #normalize colum titles
35
-
36
- #url and title are required, description and folder are not
37
- raise Exception, "Missing 'url' column" unless headers.include? 'url'
38
- raise Exception, "Missing 'title' column" unless headers.include? 'title'
39
-
40
- bookmarks = []
41
- @csv.each do |b|
42
- row = {}
43
- b.to_hash.each_pair do |k,v|
44
- row.merge!({k.downcase => v})
45
- end
46
- bookmark = OpenStruct.new(url: row['url'], title: row['title'])
47
- bookmark.description = row['description'] if headers.include? 'description'
48
- bookmark.folder = row['folder'] if headers.include? 'folder'
49
- bookmarks << bookmark
50
- end
51
- bookmarks
34
+ validate_headers(normalize_headers(@csv.headers))
35
+ parse_csv(@csv)
52
36
  rescue => e
53
37
  @logger.error e.message
54
38
  end
55
39
  end
56
40
 
57
41
  def submit_bookmarks(bookmarks)
58
- submitted = []
59
42
  begin
43
+ submitted = []
60
44
  bookmarks.each do |bookmark|
61
- clip = @service.clips.build
62
- clip.url = bookmark.url
63
- clip.title = bookmark.title
64
- clip.notes = bookmark.description
45
+ clip = clip_from_bookmark bookmark
65
46
  if clip.save
66
47
  submitted << clip
67
48
  @logger.info "SUCCESS: #{clip.title} #{clip.url}"
@@ -75,5 +56,43 @@ module Bulkippt
75
56
  end
76
57
  end
77
58
 
59
+ private
60
+
61
+ def parse_csv(csv)
62
+ result = []
63
+ csv.each do |b|
64
+ row = {}
65
+ b.to_hash.each_pair {|k,v| row.merge!({k.downcase => v})}
66
+ result << bookmark_instance(row['url'], row['title'], row.fetch('description',''), row.fetch('folder',''))
67
+ end
68
+ result
69
+ end
70
+
71
+ def normalize_headers(headers)
72
+ headers.map(&:downcase)
73
+ end
74
+
75
+ def validate_file_path(file_path)
76
+ raise Exception, "CSV not found: #{file_path}" if !File.exists?(file_path)
77
+ end
78
+
79
+ def validate_headers(headers)
80
+ #url and title are required, description and folder are not
81
+ raise Exception, "Missing 'url' column" unless headers.include? 'url'
82
+ raise Exception, "Missing 'title' column" unless headers.include? 'title'
83
+ end
84
+
85
+ def bookmark_instance(url, title, desc, folder)
86
+ OpenStruct.new(url: url, title: title, description: desc, folder: folder)
87
+ end
88
+
89
+ def clip_from_bookmark(bookmark)
90
+ clip = @service.clips.build
91
+ clip.url = bookmark.url
92
+ clip.title = bookmark.title
93
+ clip.notes = bookmark.description
94
+ clip
95
+ end
96
+
78
97
  end
79
98
  end
data/spec/spec_helper.rb CHANGED
@@ -1,43 +1,2 @@
1
1
  %w{kippt json}.each{ |lib| require lib}
2
2
  %w{bulkippt}.each { |file| require File.join(File.dirname(__FILE__),"../lib", file)}
3
-
4
- module Bulkippt
5
-
6
- class FakeService
7
-
8
- attr_reader :username, :token, :clips
9
-
10
- def initialize(username, token)
11
- @username = username
12
- @token = token
13
- @clips = Clips.new
14
- end
15
-
16
- def account
17
- if credentials_valid?(@username, @token)
18
- {username: @username, token: @token}.to_json
19
- else
20
- raise Kippt::APIError, "Can't find an user with this username and api_key"
21
- end
22
- end
23
-
24
- def credentials_valid?(username, token)
25
- return false if username != 'valid' && @token != 'valid'
26
- true
27
- end
28
-
29
- end
30
-
31
- class Clips
32
- def build
33
- Clip.new
34
- end
35
- end
36
-
37
- class Clip < OpenStruct
38
- def save
39
- self
40
- end
41
- end
42
-
43
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulkippt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-11 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: kippt
@@ -90,6 +90,7 @@ files:
90
90
  - Rakefile
91
91
  - bulkippt.gemspec
92
92
  - lib/bulkippt.rb
93
+ - lib/bulkippt/fake_service.rb
93
94
  - lib/bulkippt/version.rb
94
95
  - spec/data/empty.csv
95
96
  - spec/data/good.csv