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.



12
13
14
15
16
17
18
19
20
21
# File 'lib/neocities/client.rb', line 12

def initialize(opts={})
  @uri = URI.parse API_URI
  @http = Net::HTTP.new @uri.host, @uri.port
  @http.use_ssl = true
  @opts = opts

  unless @opts[:api_key] || (@opts[:sitename] && @opts[:password])
    raise ArgumentError, 'client requires a login (sitename/password) or an api_key'
  end
end

Instance Method Details

#delete(*paths) ⇒ Object



41
42
43
# File 'lib/neocities/client.rb', line 41

def delete(*paths)
  post 'delete', 'filenames[]' => paths
end

#info(sitename) ⇒ Object



45
46
47
# File 'lib/neocities/client.rb', line 45

def info(sitename)
  get 'info', sitename: sitename
end

#keyObject



27
28
29
# File 'lib/neocities/client.rb', line 27

def key
  get 'key'
end

#list(path = nil) ⇒ Object



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

def list(path=nil)
  get 'list', :path => path
end

#upload(path, remote_path = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/neocities/client.rb', line 31

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

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

  post 'upload', (remote_path || path.basename) => UploadIO.new(path.to_s, 'application/octet-stream')
end