Class: Neocitizen::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hash

Build a new Neocitizen client.

Parameters:

  • options (Hash) (defaults to: {})

    initialization options

Options Hash (options):

  • :username (String)

    Neocities username

  • :password (String)

    Neocities password

  • :api_host (String) — default: https://neocities.org

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/neocitizen/client.rb', line 17

def initialize(options = {})
  options = {
    username: ENV["NEOCITIZEN_USERNAME"],
    password: ENV["NEOCITIZEN_PASSWORD"]
  }.merge(options)

  raise ArgumentError, "Username and password are required" unless options[:username] && options[:password]

  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/neocitizen/client.rb', line 7

def options
  @options
end

Instance Method Details

#info(sitename = nil) ⇒ Hash

Get info about a Neocities site

Examples:

Get info on a site

client.info("cool-site")

Parameters:

  • sitename (String) (defaults to: nil)

    Neocities site to get info about (defaults to the authenticated user)

Returns:

  • (Hash)

    JSON from the response



36
37
38
39
40
# File 'lib/neocitizen/client.rb', line 36

def info(sitename = nil)
  sitename ||= options[:username]
  response = connection.get "/api/info", { sitename: sitename }
  MultiJson.load(response.body)
end

#upload(*files) ⇒ Object

Upload one or more files to Neocities

Examples:

Upload files

client.upload("index.html", "cool-graphic.gif")

Parameters:

  • files (Array)

    file names to upload



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/neocitizen/client.rb', line 50

def upload(*files)
  payload = files.inject({}) do |hash, filename|
    type = MimeMagic.by_path(filename)
    basename = File.basename(filename)
    hash[basename.to_sym] = Faraday::UploadIO.new(filename, type)
    hash
  end

  response = connection.post "/api/upload", payload
  MultiJson.load(response.body)
end