neocities 0.0.5 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc8567acab05a229147c442cf46dcb8ac0c411ac
4
- data.tar.gz: e64269881d5f0cf8832fcfba5d0ae0d1b18a2bba
3
+ metadata.gz: a7fc3c5a5006cc43a1d376c76c016af351a0b577
4
+ data.tar.gz: 34b8fc7ddd3bac03b081855fc589a3e09d9e49ce
5
5
  SHA512:
6
- metadata.gz: b1b550fb524099c129db0e6c76aeb24b4a5232a2839248b8a89cf7306b2e8780a8c2dcdc96cdabec529f120dad89e00c553c9eab3282a7c2cdf779ee3bb1106c
7
- data.tar.gz: bba3cb4c3b246360b33463b68295e49a83f6be9d0e4a6b1d43058cb6dac030e4c65fbe020989bb69b72f1452af6f3f3577bc6c257828de2c6d79af17d1e88892
6
+ metadata.gz: f83a532c0b208f5f73d77fa8727a439df85baac46ff9a0dcb746d38ca78497af68c1a6d924204b394a025ef419149dcdb2595dbb48192bf7484bad611fd77d76
7
+ data.tar.gz: 294cf5bd9ef2413b8196fd7ad156e142b6d680f51166fff2f0ae1c277cf983cbb93aff446bf0e27f51ea5daee0afe5200fc298e2b57467d8e26b9bb64a24e5fa
data/lib/neocities/cli.rb CHANGED
@@ -19,12 +19,16 @@ module Neocities
19
19
  @subargs = @argv[1..@argv.length]
20
20
  @prompt = TTY::Prompt.new
21
21
  @api_key = ENV['NEOCITIES_API_KEY'] || nil
22
- @app_config_path = File.join self.class.app_config_path, 'config'
22
+ @app_config_path = File.join self.class.app_config_path('neocities'), 'config'
23
23
  end
24
24
 
25
25
  def display_response(resp)
26
26
  if resp[:result] == 'success'
27
27
  puts "#{@pastel.green.bold 'SUCCESS:'} #{resp[:message]}"
28
+ elsif resp[:result] == 'error' && resp[:error_type] == 'file_exists'
29
+ out = "#{@pastel.yellow.bold 'EXISTS:'} #{resp[:message]}"
30
+ out += " (#{resp[:error_type]})" if resp[:error_type]
31
+ puts out
28
32
  else
29
33
  out = "#{@pastel.red.bold 'ERROR:'} #{resp[:message]}"
30
34
  out += " (#{resp[:error_type]})" if resp[:error_type]
@@ -207,7 +211,9 @@ module Neocities
207
211
  print @pastel.bold("Uploading #{path} ... ")
208
212
  resp = @client.upload path, path
209
213
 
210
- if resp[:result] == 'success'
214
+ if resp[:result] == 'error' && resp[:error_type] == 'file_exists'
215
+ print @pastel.yellow.bold("EXISTS") + "\n"
216
+ elsif resp[:result] == 'success'
211
217
  print @pastel.green.bold("SUCCESS") + "\n"
212
218
  else
213
219
  print "\n"
@@ -380,11 +386,7 @@ HERE
380
386
  exit
381
387
  end
382
388
 
383
- def self.app_config_path
384
- File.join root_config_path, 'neocities'
385
- end
386
-
387
- def self.root_config_path
389
+ def self.app_config_path(name)
388
390
  platform = if RUBY_PLATFORM =~ /win32/
389
391
  :win32
390
392
  elsif RUBY_PLATFORM =~ /darwin/
@@ -396,14 +398,30 @@ HERE
396
398
  end
397
399
 
398
400
  case platform
399
- when :win32
400
- return File.join(ENV['LOCALAPPDATA']) if ENV['LOCALAPPDATA']
401
- File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data'
401
+ when :linux
402
+ if ENV['XDG_CONFIG_HOME']
403
+ return File.join(ENV['XDG_CONFIG_HOME'], name)
404
+ end
405
+
406
+ if ENV['HOME']
407
+ return File.join(ENV['HOME'], '.config', name)
408
+ end
402
409
  when :darwin
403
- File.join ENV['HOME'], 'Library', 'Application Support'
410
+ return File.join(ENV['HOME'], 'Library', 'Application Support', name)
404
411
  else
405
- return File.join(ENV['XDG_CONFIG_HOME']) if ENV['XDG_CONFIG_HOME']
406
- File.join ENV['HOME'], '.config'
412
+ # Windows platform detection is weird, just look for the env variables
413
+ if ENV['LOCALAPPDATA']
414
+ return File.join(ENV['LOCALAPPDATA'], name)
415
+ end
416
+
417
+ if ENV['USERPROFILE']
418
+ return File.join(ENV['USERPROFILE'], 'Local Settings', 'Application Data', name)
419
+ end
420
+
421
+ # Should work for the BSDs
422
+ if ENV['HOME']
423
+ return File.join(ENV['HOME'], '.'+name)
424
+ end
407
425
  end
408
426
  end
409
427
  end
@@ -3,12 +3,11 @@ begin
3
3
  rescue
4
4
  end
5
5
 
6
- require 'net/http'
7
- require 'net/https'
8
6
  require 'json'
9
7
  require 'pathname'
10
8
  require 'uri'
11
- require 'net/http/post/multipart'
9
+ require 'digest'
10
+ require 'httpclient'
12
11
 
13
12
  module Neocities
14
13
  class Client
@@ -16,13 +15,19 @@ module Neocities
16
15
 
17
16
  def initialize(opts={})
18
17
  @uri = URI.parse API_URI
19
- @http = Net::HTTP.new @uri.host, @uri.port
20
- @http.use_ssl = true
18
+ @http = HTTPClient.new force_basic_auth: true
21
19
  @opts = opts
22
20
 
23
- unless @opts[:api_key] || (@opts[:sitename] && @opts[:password])
21
+ unless opts[:api_key] || (opts[:sitename] && opts[:password])
24
22
  raise ArgumentError, 'client requires a login (sitename/password) or an api_key'
25
23
  end
24
+
25
+ if opts[:api_key]
26
+ @http.default_header = {'Authorization' => "Bearer #{opts[:api_key]}"}
27
+ else
28
+ @http.set_auth API_URI, opts[:sitename], opts[:password]
29
+ end
30
+
26
31
  end
27
32
 
28
33
  def list(path=nil)
@@ -33,6 +38,10 @@ module Neocities
33
38
  get 'key'
34
39
  end
35
40
 
41
+ def upload_hash(remote_path, sha1_hash)
42
+ post 'upload_hash', remote_path => sha1_hash
43
+ end
44
+
36
45
  def upload(path, remote_path=nil)
37
46
  path = Pathname path
38
47
 
@@ -40,7 +49,17 @@ module Neocities
40
49
  raise ArgumentError, "#{path.to_s} does not exist."
41
50
  end
42
51
 
43
- post 'upload', (remote_path || path.basename) => UploadIO.new(path.to_s, 'application/octet-stream')
52
+ rpath = (remote_path || path.basename)
53
+
54
+ res = upload_hash rpath, Digest::SHA1.file(path.to_s).hexdigest
55
+
56
+ if res[:files] && res[:files][remote_path.to_s.to_sym] == true
57
+ return {result: 'error', error_type: 'file_exists', message: 'file already exists and matches local file, not uploading'}
58
+ else
59
+ File.open(path.to_s) do |file|
60
+ post 'upload', rpath => file
61
+ end
62
+ end
44
63
  end
45
64
 
46
65
  def delete(*paths)
@@ -51,25 +70,16 @@ module Neocities
51
70
  get 'info', sitename: sitename
52
71
  end
53
72
 
54
- private
55
-
56
73
  def get(path, params={})
57
- req = Net::HTTP::Get.new "#{@uri.path}#{path}?#{URI.encode_www_form(params)}"
58
- request req
74
+ uri = @uri+path
75
+ uri.query = URI.encode_www_form params
76
+ resp = @http.get uri
77
+ JSON.parse resp.body, symbolize_names: true
59
78
  end
60
79
 
61
80
  def post(path, args={})
62
- req = Net::HTTP::Post::Multipart.new("#{@uri.path}#{path}", args)
63
- res = request req
64
- end
65
-
66
- def request(req)
67
- if @opts[:api_key]
68
- req['Authorization'] = "Bearer #{@opts[:api_key]}"
69
- else
70
- req.basic_auth @opts[:sitename], @opts[:password]
71
- end
72
- resp = @http.request req
81
+ uri = @uri+path
82
+ resp = @http.post uri, args
73
83
  JSON.parse resp.body, symbolize_names: true
74
84
  end
75
85
  end
@@ -1,3 +1,3 @@
1
1
  module Neocities
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.7'
3
3
  end
data/neocities.gemspec CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
  spec.extensions = ['ext/mkrf_conf.rb']
19
19
 
20
- spec.add_dependency 'tty-table', '~> 0.8', '>= 0.8.0'
21
- spec.add_dependency 'tty-prompt', '~> 0.12', '>= 0.12.0'
22
- spec.add_dependency 'pastel', '~> 0.7', '>= 0.7.1'
23
- spec.add_dependency 'multipart-post', '~> 2.0', '>= 2.0.0'
20
+ spec.add_dependency 'tty-table', '~> 0.8', '>= 0.8.0'
21
+ spec.add_dependency 'tty-prompt', '~> 0.12', '>= 0.12.0'
22
+ spec.add_dependency 'pastel', '~> 0.7', '>= 0.7.1'
23
+ spec.add_dependency 'httpclient', '~> 2.8', '>= 2.8.3'
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neocities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Drake
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-20 00:00:00.000000000 Z
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-table
@@ -71,25 +71,25 @@ dependencies:
71
71
  - !ruby/object:Gem::Version
72
72
  version: 0.7.1
73
73
  - !ruby/object:Gem::Dependency
74
- name: multipart-post
74
+ name: httpclient
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - "~>"
78
78
  - !ruby/object:Gem::Version
79
- version: '2.0'
79
+ version: '2.8'
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 2.0.0
82
+ version: 2.8.3
83
83
  type: :runtime
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '2.0'
89
+ version: '2.8'
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
- version: 2.0.0
92
+ version: 2.8.3
93
93
  description:
94
94
  email:
95
95
  - contact@neocities.org