Class: Neocities::Client

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

Constant Summary collapse

API_URI =
'https://neocities.org/api/'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/neocities/client.rb', line 9

def initialize(opts={})
  if opts[:api_key]
    @http = HTTP.auth "Bearer #{opts[:api_key]}"
  elsif opts[:sitename] && opts[:password]
    @http = HTTP.basic_auth user: opts[:sitename], pass: opts[:password]
  else
    raise ArgumentError, 'client requires a login (sitename/password) or an api_key'
  end
end

Instance Method Details

#delete(*paths) ⇒ Object



39
40
41
# File 'lib/neocities/client.rb', line 39

def delete(*paths)
  run :post, 'delete', form: {'filenames[]' => paths}
end

#info(sitename) ⇒ Object



43
44
45
# File 'lib/neocities/client.rb', line 43

def info(sitename)
  run :get, 'info', params: {sitename: sitename}
end

#keyObject



23
24
25
# File 'lib/neocities/client.rb', line 23

def key
  run :get, 'key', {}
end

#list(path = nil) ⇒ Object



19
20
21
# File 'lib/neocities/client.rb', line 19

def list(path=nil)
  run :get, 'list', params: {path: path}
end

#upload(path, remote_path = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/neocities/client.rb', line 27

def upload(path, remote_path=nil)
  path = Pathname path

  unless path.exist?
    raise ArgumentError, "#{path.to_s} does not exist."
  end

  run :post, 'upload', form: {
    (remote_path || path.basename) => HTTP::FormData::File.new(path.to_s)
  }
end