pocketbeuter 0.0.1

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.
@@ -0,0 +1,72 @@
1
+ require 'pocketbeuter/conf'
2
+ require "net/http"
3
+ require "json"
4
+ require "colorize"
5
+
6
+ module Pocketbeuter
7
+ class Pocket
8
+ def initialize
9
+ end
10
+ end
11
+ end
12
+
13
+ #module Pocket
14
+ # def all
15
+ # uri = URI.parse(Conf::GET_URL)
16
+ # http = Net::HTTP.new(uri.host, uri.port)
17
+ # request = Net::HTTP::Post.new(uri.request_uri)
18
+ # request.set_form_data("consumer_key" => Conf.config[:consumer_key],
19
+ # "state" => "all",
20
+ # "access_token" => Conf.config[:access_token])
21
+ # http.use_ssl = true
22
+ # res = http.request(request)
23
+ # json = JSON.parse(res.body)
24
+ # puts json['status']
25
+ # puts json['complete']
26
+ # json['list'].each do |a|
27
+ # Hash[*a].each do |k,v|
28
+ # # {"item_id"=>"4158604",
29
+ # # "resolved_id"=>"4158604",
30
+ # # "given_url"=>"http://ds9a.nl/amazing-dna/",
31
+ # # "given_title"=>"DNA seen through the eyes of a coder",
32
+ # # "favorite"=>"0",
33
+ # # "status"=>"0",
34
+ # # "time_added"=>"1340118098",
35
+ # # "time_updated"=>"1340118101",
36
+ # # "time_read"=>"0",
37
+ # # "time_favorited"=>"0",
38
+ # # "sort_id"=>498,
39
+ # # "resolved_title"=>"DNA seen through the eyes of a coder",
40
+ # # "resolved_url"=>"http://ds9a.nl/amazing-dna/",
41
+ # # "excerpt"=>"This is just some rambling by a computer programmer about DNA. I'm not a molecular geneticist. If you spot the inevitable mistakes, please mail me (bert hubert) at ahu@ds9a.nl. I'm not trying to force my view unto the DNA - each observation here is quite 'uncramped'.",
42
+ # # "is_article"=>"0",
43
+ # # "is_index"=>"0",
44
+ # # "has_video"=>"0",
45
+ # # "has_image"=>"1",
46
+ # # "word_count"=>"4486"}
47
+ # puts '['.blue + "%9s".white % v['item_id'] + ']'.blue + ' : ' + '"'.red + v['given_title'].cyan + '"'.red + " - " + v['given_url'].yellow
48
+ # end
49
+ # end
50
+ #
51
+ # File.open(Conf.config[:cache], 'w') do |f|
52
+ # f.write(JSON.dump(json))
53
+ # f.close
54
+ # end
55
+ # end
56
+ # def open(id)
57
+ # json = []
58
+ # if File.exists?(Conf.config[:cache])
59
+ # File.open(Conf.config[:cache], 'r') do |f|
60
+ # json = JSON.load(f.read)
61
+ # end
62
+ # end
63
+ # json['list'].each do |a|
64
+ # Hash[*a].each do |k,v|
65
+ # if v['item_id'] == id
66
+ # Launchy.open(v['given_url'])
67
+ # end
68
+ # end
69
+ # end
70
+ # end
71
+ # extend Pocket
72
+ #end
@@ -0,0 +1,3 @@
1
+ module Pocketbeuter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pocketbeuter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pocketbeuter"
8
+ spec.version = Pocketbeuter::VERSION
9
+ spec.authors = ["Piotr Król"]
10
+ spec.email = ["pietrushnic@gmail.com"]
11
+ spec.description = %q{command line interface to Pocket}
12
+ spec.summary = %q{command line interface to Pocket}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "launchy"
24
+ spec.add_development_dependency "thor"
25
+ spec.add_development_dependency "colorize"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "webmock"
28
+ spec.add_development_dependency "vcr"
29
+ end
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'pocketbeuter'
2
+ require 'helpers'
3
+ require 'spec_helper'
4
+ include WebMock::API
5
+
6
+ describe Pocketbeuter::CLI do
7
+ before :each do
8
+ @cli = Pocketbeuter::CLI.new
9
+ $old_stderr = $stderr
10
+ $old_stdout = $stdout
11
+ $stderr = StringIO.new
12
+ $stdout = StringIO.new
13
+ end
14
+
15
+ after :each do
16
+ $stderr = $old_stderr
17
+ $stdout = $old_stdout
18
+ end
19
+
20
+ describe '#createapp' do
21
+ before do
22
+ File.open(fixtures_path + "/createapp", File::RDWR | File::TRUNC | File::CREAT, 0600) do |f|
23
+ f.write ""
24
+ end
25
+ @cli.config.path = fixtures_path + "/createapp"
26
+ end
27
+ it 'print instructions and set account_name, consumer_key and redirect_uri' do
28
+ expect($stdout).to receive(:print)
29
+ expect($stdin).to receive(:gets).and_return('\n')
30
+ expect($stdout).to receive(:print).with("Enter account name [#{ENV['USER']}]: ")
31
+ expect($stdin).to receive(:gets).and_return('foo')
32
+ expect($stdout).to receive(:print).with('Enter consumer key: ')
33
+ expect($stdin).to receive(:gets).and_return('foo1337')
34
+ expect($stdout).to receive(:print).with('Enter redirect uri: ')
35
+ expect($stdin).to receive(:gets).and_return('http://foo.bar')
36
+ @cli.createapp
37
+ expect(@cli.config.account_name).to match(/foo/)
38
+ expect(@cli.config.consumer_key).to match(/foo1337/)
39
+ expect(@cli.config.redirect_uri).to match(/http:\/\/foo.bar/)
40
+ config = {"options"=>{}, "account"=>{"foo"=>{"consumer_key"=>"foo1337", "redirect_uri"=>"http://foo.bar"}}}
41
+ expect(@cli.config.load_config).to eq(config)
42
+ end
43
+ end
44
+ vcr_options = { :cassette_name => "authorize", :record => :new_episodes }
45
+ describe '#authorize', :vcr => vcr_options do
46
+ before do
47
+ File.open(fixtures_path + "/createapp", File::RDWR | File::TRUNC | File::CREAT, 0600) do |f|
48
+ f.write ""
49
+ end
50
+ @cli.config.path = fixtures_path + "/createapp"
51
+ @cli.config.account_name = 'foo'
52
+ @cli.config.consumer_key = 'foo1337'
53
+ @cli.config.redirect_uri = 'foo1337'
54
+ end
55
+ it 'request code, access_token and username' do
56
+ expect($stdout).to receive(:print)
57
+ expect($stdin).to receive(:gets).and_return('\n')
58
+ @cli.authorize
59
+ expect(@cli.config.code).to match(/foobar42/)
60
+ expect(@cli.config.access_token).to match(/barfoo42/)
61
+ expect(@cli.config.username).to match(/joedoe/)
62
+ config = {"options"=>{}, "account"=>{"foo"=>{"consumer_key"=>"foo1337", "redirect_uri"=>"foo1337", "code"=>"foobar42", "access_token"=>"barfoo42", "username"=>"joedoe"}}}
63
+ expect(@cli.config.load_config).to eq(config)
64
+ end
65
+ end
66
+ end
67
+
@@ -0,0 +1,384 @@
1
+ require 'pocketbeuter'
2
+ require 'helpers'
3
+
4
+ describe Pocketbeuter::ConfigFile do
5
+ after :each do
6
+ Pocketbeuter::ConfigFile.instance.reset
7
+ end
8
+
9
+ it 'is a singleton' do
10
+ expect(Pocketbeuter::ConfigFile).to be_a Class
11
+ expect do
12
+ Pocketbeuter::ConfigFile.new
13
+ end.to raise_error(NoMethodError, /private method `new' called/)
14
+ end
15
+
16
+ describe '#[]' do
17
+ it 'return account' do
18
+ cfg = Pocketbeuter::ConfigFile.instance
19
+ cfg.path = fixtures_path + "/multiple_accounts"
20
+ expect(cfg['account'].keys).to match_array(['foo'])
21
+ end
22
+ it 'return options' do
23
+ cfg = Pocketbeuter::ConfigFile.instance
24
+ cfg.path = fixtures_path + "/options"
25
+ expect(cfg['options'].keys).to match_array(['abc', 'cde'])
26
+ end
27
+ end
28
+
29
+ describe '#account' do
30
+ it 'return account' do
31
+ cfg = Pocketbeuter::ConfigFile.instance
32
+ cfg.path = fixtures_path + "/multiple_accounts"
33
+ expect(cfg.account.keys).to match_array(['foo'])
34
+ end
35
+ end
36
+
37
+ describe '#account=' do
38
+ before do
39
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
40
+ f.write ''
41
+ end
42
+ end
43
+ it 'set foo account' do
44
+ cfg = Pocketbeuter::ConfigFile.instance
45
+ cfg.path = fixtures_path + "/emptyrc"
46
+ cfg.account = {
47
+ 'foo' => {}
48
+ }
49
+ expect(cfg.account.keys).to match_array(['foo'])
50
+ end
51
+ it 'set empty foo account' do
52
+ cfg = Pocketbeuter::ConfigFile.instance
53
+ cfg.path = fixtures_path + "/emptyrc"
54
+ cfg.account = {
55
+ 'foo' => {}
56
+ }
57
+ expect(cfg.account.values[0]).to be_empty
58
+ end
59
+ end
60
+
61
+ describe '#account_name' do
62
+ it 'return account' do
63
+ cfg = Pocketbeuter::ConfigFile.instance
64
+ cfg.path = fixtures_path + "/multiple_accounts"
65
+ expect(cfg.account_name).to match(/foo/)
66
+ end
67
+ end
68
+
69
+ describe '#account_name=' do
70
+ before do
71
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
72
+ f.write ''
73
+ end
74
+ end
75
+ it 'return account' do
76
+ cfg = Pocketbeuter::ConfigFile.instance
77
+ cfg.path = fixtures_path + "/emptyrc"
78
+ cfg.account_name = 'foo'
79
+ expect(cfg.account_name).to match(/foo/)
80
+ end
81
+ end
82
+
83
+ describe '#[]=' do
84
+ before do
85
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
86
+ f.write ''
87
+ end
88
+ end
89
+ it 'adds user account' do
90
+ cfg = Pocketbeuter::ConfigFile.instance
91
+ cfg.path = fixtures_path + '/emptyrc'
92
+ cfg['account'] = {
93
+ 'foo' => {
94
+ 'consumer_key' => '1234-12345567889',
95
+ 'redirect_uri' => 'http://google.com'
96
+ }
97
+ }
98
+ expect(cfg['account'].keys).to match_array(['foo'])
99
+ expect(cfg['account']['foo'].keys).to match_array(['consumer_key', 'redirect_uri'])
100
+ end
101
+ it 'adds options' do
102
+ cfg = Pocketbeuter::ConfigFile.instance
103
+ cfg.path = fixtures_path + '/emptyrc'
104
+ cfg['options'] = {
105
+ 'foo' => {
106
+ 'consumer_key' => '1234-12345567889',
107
+ 'redirect_uri' => 'http://google.com'
108
+ }
109
+ }
110
+ expect(cfg['options'].keys).to match_array(['foo'])
111
+ expect(cfg['options']['foo'].keys).to match_array(['consumer_key', 'redirect_uri'])
112
+ end
113
+ end
114
+
115
+ describe '#redirect_uri' do
116
+ before do
117
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
118
+ f.write ''
119
+ end
120
+ @cfg = Pocketbeuter::ConfigFile.instance
121
+ @cfg.path = fixtures_path + '/emptyrc'
122
+ @cfg['account'] = {
123
+ 'foo' => {
124
+ 'consumer_key' => '1234-12345567889',
125
+ 'redirect_uri' => 'http://google.com'
126
+ }
127
+ }
128
+ end
129
+
130
+ it 'returns redirect_uri for user foo' do
131
+ expect(@cfg.redirect_uri).to match(/http:\/\/google.com/)
132
+ end
133
+ end
134
+
135
+ describe '#redirect_uri=' do
136
+ before do
137
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
138
+ f.write ''
139
+ end
140
+ @cfg = Pocketbeuter::ConfigFile.instance
141
+ @cfg.path = fixtures_path + '/emptyrc'
142
+ @cfg['account'] = {
143
+ 'foo' => {
144
+ }
145
+ }
146
+ end
147
+
148
+ it 'returns redirect_uri for user foo' do
149
+ @cfg.redirect_uri = 'http://google.com'
150
+ expect(@cfg.redirect_uri).to match(/http:\/\/google.com/)
151
+ end
152
+ it 'change redirect_uri for user foo' do
153
+ @cfg.redirect_uri = 'http://google.com'
154
+ @cfg.redirect_uri = 'http://gaagle.com'
155
+ expect(@cfg.redirect_uri).to match(/http:\/\/gaagle.com/)
156
+ end
157
+ end
158
+
159
+ describe '#consumer_key' do
160
+ before do
161
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
162
+ f.write ''
163
+ end
164
+ @cfg = Pocketbeuter::ConfigFile.instance
165
+ @cfg.path = fixtures_path + '/emptyrc'
166
+ @cfg['account'] = {
167
+ 'foo' => {
168
+ 'consumer_key' => '1234-12345567889',
169
+ 'redirect_uri' => 'http://google.com'
170
+ }
171
+ }
172
+ end
173
+
174
+ it 'returns consumer_key for user foo' do
175
+ expect(@cfg.consumer_key).to match(/1234-12345567889/)
176
+ end
177
+ end
178
+
179
+ describe '#consumer_key=' do
180
+ before do
181
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
182
+ f.write ''
183
+ end
184
+ @cfg = Pocketbeuter::ConfigFile.instance
185
+ @cfg.path = fixtures_path + '/emptyrc'
186
+ @cfg['account'] = {
187
+ 'foo' => {
188
+ }
189
+ }
190
+ end
191
+
192
+ it 'returns consumer_key for user foo' do
193
+ @cfg.consumer_key = '1234-12345567889'
194
+ expect(@cfg.consumer_key).to match(/1234-12345567889/)
195
+ end
196
+ it 'change consumer_key for user foo' do
197
+ @cfg.consumer_key = '1234-12345567889'
198
+ @cfg.consumer_key = '1234-12345567888'
199
+ expect(@cfg.consumer_key).to match(/1234-12345567888/)
200
+ end
201
+ end
202
+
203
+ describe '#access_token' do
204
+ before do
205
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
206
+ f.write ''
207
+ end
208
+ @cfg = Pocketbeuter::ConfigFile.instance
209
+ @cfg.path = fixtures_path + '/emptyrc'
210
+ @cfg['account'] = {
211
+ 'foo' => {
212
+ 'access_token' => '1234-12345567889',
213
+ 'redirect_uri' => 'http://google.com'
214
+ }
215
+ }
216
+ end
217
+
218
+ it 'returns access_token for user foo' do
219
+ expect(@cfg.access_token).to match(/1234-12345567889/)
220
+ end
221
+ end
222
+
223
+ describe '#access_token=' do
224
+ before do
225
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
226
+ f.write ''
227
+ end
228
+ @cfg = Pocketbeuter::ConfigFile.instance
229
+ @cfg.path = fixtures_path + '/emptyrc'
230
+ @cfg['account'] = {
231
+ 'foo' => {
232
+ }
233
+ }
234
+ end
235
+
236
+ it 'returns access_token for user foo' do
237
+ @cfg.access_token = '1234-12345567889'
238
+ expect(@cfg.access_token).to match(/1234-12345567889/)
239
+ end
240
+ it 'change access_token for user foo' do
241
+ @cfg.access_token = '1234-12345567889'
242
+ @cfg.access_token = '1234-12345567888'
243
+ expect(@cfg.access_token).to match(/1234-12345567888/)
244
+ end
245
+ end
246
+
247
+ describe '#code' do
248
+ before do
249
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
250
+ f.write ''
251
+ end
252
+ @cfg = Pocketbeuter::ConfigFile.instance
253
+ @cfg.path = fixtures_path + '/emptyrc'
254
+ @cfg['account'] = {
255
+ 'foo' => {
256
+ 'code' => '1234-12345567889',
257
+ 'redirect_uri' => 'http://google.com'
258
+ }
259
+ }
260
+ end
261
+
262
+ it 'returns code for user foo' do
263
+ expect(@cfg.code).to match(/1234-12345567889/)
264
+ end
265
+ end
266
+
267
+ describe '#code=' do
268
+ before do
269
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
270
+ f.write ''
271
+ end
272
+ @cfg = Pocketbeuter::ConfigFile.instance
273
+ @cfg.path = fixtures_path + '/emptyrc'
274
+ @cfg['account'] = {
275
+ 'foo' => {
276
+ }
277
+ }
278
+ end
279
+
280
+ it 'returns code for user foo' do
281
+ @cfg.code = '1234-12345567889'
282
+ expect(@cfg.code).to match(/1234-12345567889/)
283
+ end
284
+ it 'change code for user foo' do
285
+ @cfg.code = '1234-12345567889'
286
+ @cfg.code = '1234-12345567888'
287
+ expect(@cfg.code).to match(/1234-12345567888/)
288
+ end
289
+ end
290
+
291
+ describe '#username' do
292
+ before do
293
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
294
+ f.write ''
295
+ end
296
+ @cfg = Pocketbeuter::ConfigFile.instance
297
+ @cfg.path = fixtures_path + '/emptyrc'
298
+ @cfg['account'] = {
299
+ 'foo' => {
300
+ 'username' => '1234-12345567889',
301
+ 'redirect_uri' => 'http://google.com'
302
+ }
303
+ }
304
+ end
305
+
306
+ it 'returns username for user foo' do
307
+ expect(@cfg.username).to match(/1234-12345567889/)
308
+ end
309
+ end
310
+
311
+ describe '#username=' do
312
+ before do
313
+ File.open(fixtures_path + '/emptyrc', File::RDWR | File::CREAT | File::TRUNC, 600) do |f|
314
+ f.write ''
315
+ end
316
+ @cfg = Pocketbeuter::ConfigFile.instance
317
+ @cfg.path = fixtures_path + '/emptyrc'
318
+ @cfg['account'] = {
319
+ 'foo' => {
320
+ }
321
+ }
322
+ end
323
+
324
+ it 'returns username for user foo' do
325
+ @cfg.username = '1234-12345567889'
326
+ expect(@cfg.username).to match(/1234-12345567889/)
327
+ end
328
+ it 'change username for user foo' do
329
+ @cfg.username = '1234-12345567889'
330
+ @cfg.username = '1234-12345567888'
331
+ expect(@cfg.username).to match(/1234-12345567888/)
332
+ end
333
+ end
334
+
335
+ describe '#path' do
336
+ it 'get default path' do
337
+ expect(Pocketbeuter::ConfigFile.instance.path).to match(File.join(File.expand_path('~'), Pocketbeuter::ConfigFile::CONFIG_NAME))
338
+ end
339
+ end
340
+
341
+ describe '#path=' do
342
+ it 'set path' do
343
+ cfg = Pocketbeuter::ConfigFile.instance
344
+ cfg.path = fixtures_path + "/#{Pocketbeuter::ConfigFile::CONFIG_NAME}"
345
+ expect(cfg.path).to match(fixtures_path + "/#{Pocketbeuter::ConfigFile::CONFIG_NAME}")
346
+ end
347
+ end
348
+
349
+ describe '#load_config' do
350
+ before do
351
+ File.open(fixtures_path + "/emptyrc", File::RDWR | File::TRUNC | File::CREAT, 0600) do |f|
352
+ f.write ""
353
+ end
354
+ end
355
+ it 'when file exist' do
356
+ cfg = Pocketbeuter::ConfigFile.instance
357
+ cfg.path = fixtures_path + "/multiple_accounts"
358
+ expect(cfg.load_config['account']['foo'].keys).to include('consumer_key','redirect_uri')
359
+ end
360
+ it 'when file exist but is empty' do
361
+ cfg = Pocketbeuter::ConfigFile.instance
362
+ cfg.path = fixtures_path + "/emptyrc"
363
+ expect(cfg.load_config['account'].keys).to be_empty
364
+ end
365
+ it 'when file doesn not exist' do
366
+ cfg = Pocketbeuter::ConfigFile.instance
367
+ cfg.path = fixtures_path + "/foo"
368
+ expect(cfg.load_config['account'].keys).to be_empty
369
+ end
370
+ it 'when file path is empty' do
371
+ cfg = Pocketbeuter::ConfigFile.instance
372
+ cfg.path = ''
373
+ expect(cfg.load_config['account'].keys).to be_empty
374
+ end
375
+ end
376
+
377
+ describe '#empty?' do
378
+ it 'return default_config' do
379
+ cfg = Pocketbeuter::ConfigFile.instance
380
+ cfg.path = ''
381
+ expect(cfg.empty?).to be_true
382
+ end
383
+ end
384
+ end