lard 0.0.5 → 0.0.6

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/lard +82 -64
  3. metadata +45 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87f51cb7f7af091dc328b99971eec87a2de0220683644d7c4f18581c0b0748b4
4
- data.tar.gz: 21c8a6e0ba001bf3cccdbb6e5630c6b49cd31708e2b59b95d84f358360d663f5
3
+ metadata.gz: 8f99cc882f8a69da2bb153deaf6562413ffaf1ef30d6629891a759923e14cbcb
4
+ data.tar.gz: ddd7735acd5db8b72210fe6cc479dfee474682f46f23eb0b1b4f9e91f2bc6096
5
5
  SHA512:
6
- metadata.gz: bf3a767116cfded14043638e2495ca3d05d18a96c1d6b218328d0b2838ccd4d7835b27b3c513c5cccfacd0db4cacaea59a49123a0d9dac8b0c58369c2ab00e2a
7
- data.tar.gz: 71e4d72b042dd61507be61234397d36ec9b0725b99509811a42e847dfa80f6e48a47f61f14730c90848cb8db241dc3ee6cb15ca96afa0617c89a8e679793793a
6
+ metadata.gz: 374516a4d37c0b9a94a28ae94f7f42bbfb39db3a659018c26898119b6698a046b2b63e52446410973c3ae8c55bdc8addcfd71295dc0dfda53dab69f8dda2a852
7
+ data.tar.gz: 3ba7229ed910834cdf4f7cf39cf5c925c76ef45d300be9010ef3225e88f0416c9ab3e7fc2337a6a2dffb85024bf58678512fc49b1c2b911ab3d3f53e5fa05ab4
data/bin/lard CHANGED
@@ -5,16 +5,14 @@ require 'thor'
5
5
  require 'yaml'
6
6
 
7
7
  # Try to authenticate with ~/.lard.yml
8
- $authenticated = false
9
8
  begin
10
9
  config = YAML.load_file File.expand_path('~/.lard.yml')
11
- rescue
10
+ rescue StandardError
12
11
  config = {}
13
12
  end
14
13
  begin
15
14
  token = config['token']
16
- $authenticated = true if token
17
- rescue
15
+ rescue StandardError
18
16
  token = nil
19
17
  end
20
18
  $options = {
@@ -24,9 +22,16 @@ $options = {
24
22
  }
25
23
  $folders = []
26
24
 
25
+ # A set of utility functions for working with the Larder HTTP API
27
26
  module LardHTTP
27
+ def authorized
28
+ # Assume Auth header longer than "Token " is authorized
29
+ $options[:headers]['Authorization'].length > 6
30
+ end
31
+
28
32
  def get(endpoint, params = nil)
29
- raise "You're not logged in! Run 'lard login' first." unless $authenticated
33
+ raise "You're not logged in! Run 'lard login' first." unless authorized
34
+
30
35
  query = { query: params }
31
36
  opts = $options.merge query
32
37
  res = self.class.get "#{prefix}#{endpoint}", opts
@@ -34,8 +39,14 @@ module LardHTTP
34
39
  end
35
40
 
36
41
  def post(endpoint, args = {})
37
- raise "You're not logged in! Run 'lard login' first." unless $authenticated
38
- opts = { body: args.to_json, headers: {"Content-Type" => "application/json"} }
42
+ raise "You're not logged in! Run 'lard login' first." unless authorized
43
+
44
+ opts = {
45
+ body: args.to_json,
46
+ headers: {
47
+ 'Content-Type' => 'application/json'
48
+ }
49
+ }
39
50
  opts[:headers].merge! $options[:headers]
40
51
  res = self.class.post "#{prefix}#{endpoint}", opts
41
52
  parse_response res
@@ -46,16 +57,17 @@ module LardHTTP
46
57
  end
47
58
 
48
59
  def raw_get(url)
49
- raise "You're not logged in! Run 'lard login' first." unless $authenticated
60
+ raise "You're not logged in! Run 'lard login' first." unless authorized
61
+
50
62
  JSON.parse self.class.get(url, $options).body, symbolize_names: true
51
63
  end
52
64
 
53
65
  def prefix
54
66
  'https://larder.io/api/1/@me/'
55
67
  end
56
-
68
+
57
69
  def get_folder_by_name(name)
58
- fetch_folders unless $folders.length > 0
70
+ fetch_folders if $folders.empty?
59
71
  $folders.find do |folder|
60
72
  folder[:name] == name
61
73
  end
@@ -67,55 +79,58 @@ module LardHTTP
67
79
  end
68
80
 
69
81
  def fetch_tags
70
- res = get 'tags', { limit: 100 }
82
+ res = get 'tags', limit: 200
71
83
  tags = res[:results] || []
72
84
 
73
- while res[:next] != nil
85
+ until res[:next].nil?
74
86
  res = raw_get res[:next]
75
- tags.push *res[:results]
87
+ tags.push(*res[:results])
76
88
  end
77
89
 
78
90
  tags
79
91
  end
80
92
 
81
93
  def fetch_bookmarks(folder_id)
82
- res = get "folders/#{folder_id}", { limit: 100 }
94
+ res = get "folders/#{folder_id}", limit: 200
83
95
  bookmarks = res[:results] || []
84
96
 
85
- while res[:next] != nil
97
+ until res[:next].nil?
86
98
  res = raw_get res[:next]
87
- bookmarks.push *res[:results]
99
+ bookmarks.push(*res[:results])
88
100
  end
89
101
 
90
102
  bookmarks
91
103
  end
92
104
 
93
105
  def fetch_folders
94
- res = get 'folders', { limit: 100 }
106
+ res = get 'folders', limit: 200
95
107
  $folders = res[:results]
96
108
 
97
- while res[:next] != nil
109
+ until res[:next].nil?
98
110
  res = raw_get res[:next]
99
- $folders.push *res[:results]
111
+ $folders.push(*res[:results])
100
112
  end
101
113
 
102
114
  # TODO: Cache these folders
103
115
  end
104
-
116
+
117
+ # TODO: How can we reduce the AbcSize here further?
118
+ # rubocop:disable AbcSize
105
119
  def print_bookmark(bookmark)
106
- puts Paint["#{bookmark[:title]}", :bright]
120
+ puts Paint[(bookmark[:title]).to_s, :bright]
107
121
  puts " #{bookmark[:description]}" if bookmark[:description]
108
122
  puts " #{bookmark[:url]}"
109
-
110
- unless bookmark[:tags] == nil && bookmark[:tags].empty?
111
- print " "
112
- bookmark[:tags].each do |tag|
113
- print Paint["##{tag[:name]}", tag[:color]]
114
- print " "
115
- end
116
- puts ""
123
+
124
+ return if bookmark[:tags].nil? || bookmark[:tags].empty?
125
+
126
+ print ' '
127
+ bookmark[:tags].each do |tag|
128
+ print Paint["##{tag[:name]}", tag[:color]]
129
+ print ' '
117
130
  end
131
+ puts ''
118
132
  end
133
+ # rubocop:enable AbcSize
119
134
 
120
135
  def print_tag(tag)
121
136
  puts Paint["##{tag[:name]}", tag[:color]]
@@ -134,9 +149,7 @@ class Lard < Thor
134
149
 
135
150
  desc 'folders', 'Lists all bookmark folders'
136
151
  def folders
137
- if $folders == []
138
- fetch_folders
139
- end
152
+ fetch_folders if $folders == []
140
153
 
141
154
  $folders.each do |folder|
142
155
  print_folder_name folder
@@ -146,7 +159,7 @@ class Lard < Thor
146
159
  desc 'folder <NAME>', 'Lists all bookmarks in a given folder'
147
160
  def folder(name)
148
161
  folder = get_folder_by_name name
149
- raise "No such folder" unless folder
162
+ raise "Could not find a folder named #{name}!" unless folder
150
163
 
151
164
  print_folder_name folder
152
165
 
@@ -167,53 +180,58 @@ class Lard < Thor
167
180
  desc 'search <QUERY>', 'Search for bookmarks'
168
181
  def search(*args)
169
182
  query = args.join ' '
170
- res = get 'search', { q: query }
171
- raise "No results for query #{query}" unless res[:results].length > 0
183
+ res = get 'search', q: query
184
+ raise "No booknarks found match #{query}!" if res[:results].empty?
185
+
172
186
  bookmarks = res[:results]
173
187
  bookmarks.each do |b|
174
188
  print_bookmark b
175
189
  end
176
190
  end
177
191
 
192
+ # rubocop: disable MethodLength
178
193
  desc 'login [TOKEN]', 'Log in to larder with your API token'
179
194
  def login(token = nil)
180
195
  unless token
181
- puts "Enter your token to save to ~/.lard.yml for lard to authenticate you with."
182
- puts "Note: You can retrive your API token from https://larder.io/apps/clients/"
196
+ puts 'Enter your Larder API token to save to ~/.lard.yml.'
197
+ puts 'Note: You can retrive your API token from https://larder.io/apps/clients/'
183
198
  token = STDIN.readline
184
199
  end
200
+ return unless token
185
201
 
186
- if token
187
- if system "echo \"token: #{token}\" > $HOME/.lard.yml"
188
- puts 'Saved token to ~/.lard.yml'
189
- else
190
- STDERR.puts 'Failed to save token to ~/.lard.yml!'
191
- exit 1
192
- end
202
+ unless system "echo \"token: #{token}\" > $HOME/.lard.yml"
203
+ STDERR.puts 'Failed to save token to ~/.lard.yml!'
204
+ exit 1
193
205
  end
206
+
207
+ puts 'Saved token to ~/.lard.yml'
194
208
  end
195
-
196
- desc 'bookmark <FOLDER> <TITLE> <LINK> [tags...]', 'Creates or edits a bookmark'
197
- option :description, :aliases => :d
209
+ # rubocop: enable MethodLength
210
+
211
+ desc 'bookmark <FOLDER> <TITLE> <LINK> [tags...]',
212
+ 'Creates or edits a bookmark'
213
+ option :description, aliases: :d
198
214
  def bookmark(folder, title, link, *tags)
199
215
  f = get_folder_by_name folder
200
- raise "No such folder" unless f
201
- parent = f[:id]
202
-
203
- res = post 'links/add', {
204
- "title" => title,
205
- "url" => link,
206
- "parent" => parent,
207
- "description" => options[:description],
208
- "tags" => tags
209
- }
210
- unless res[:error]
211
- print_bookmark res
212
- else
213
- # TODO: Test this
214
- raise "Unable to add bookmark"
215
- end
216
+ raise "Could not find a folder named #{folder}!" unless f
217
+
218
+ res = post 'links/add',
219
+ 'title' => title, 'url' => link, 'tags' => tags,
220
+ 'parent' => f[:id],
221
+ 'description' => options[:description]
222
+
223
+ raise 'Unable to add bookmark!' if res[:error]
224
+
225
+ print_bookmark res
216
226
  end
217
227
  end
218
228
 
219
- Lard.start(ARGV)
229
+ begin
230
+ Lard.start(ARGV)
231
+ rescue StandardError => e
232
+ STDERR.puts "Error: #{e.message}"
233
+
234
+ # TODO: Check for developer flag passed to print stacktrace
235
+
236
+ exit 1
237
+ end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - hawkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-25 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2018-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.16.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.16.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: paint
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.20.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.20.0
13
55
  description:
14
56
  email:
15
57
  executables: