Class: Nerdz

Inherits:
Object
  • Object
show all
Defined in:
lib/nerdz/structs.rb,
lib/nerdz.rb,
lib/nerdz/http.rb,
lib/nerdz/errors.rb,
lib/nerdz/version.rb

Overview

– Copyleft shura. [ [email protected] ]

This file is part of nerdz.

nerdz is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

nerdz is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with nerdz. If not, see <www.gnu.org/licenses/>. ++

Defined Under Namespace

Classes: HTTP, Post, User

Constant Summary collapse

NotLoggedIn =
exception
UserNotFound =
exception
GETQueryNotFound =
exception
POSTQueryNotFound =
exception
UndefinedError =
exception
LoginError =
exception
InsertError =
exception
ShareError =
exception
InvalidUrl =
exception
InvalidAction =
exception
NotOnline =
exception
ERRORS =
Class.new {
  def initialize
    @errors = [nil, NotLoggedIn, UserNotFound, GETQueryNotFound,
      POSTQueryNotFound, UndefinedError, LoginError, InsertError,
      ShareError, InvalidUrl, InvalidAction, NotOnline].freeze
  end

  def [](x)
    x = x.to_s.to_i unless x.is_a?(Integer)
    @errors[x.to_i]
  end
}.new
VERSION =
'0.0.1'

Instance Method Summary collapse

Constructor Details

#initializeNerdz

Returns a new instance of Nerdz.



26
27
28
# File 'lib/nerdz.rb', line 26

def initialize
  @http = HTTP.new
end

Instance Method Details

#login(username, password) ⇒ Object



38
39
40
41
# File 'lib/nerdz.rb', line 38

def (username, password)
  request(:login, {username: username, password: password})
  true
end

#logoutObject



43
44
45
46
# File 'lib/nerdz.rb', line 43

def logout
  request(:logout)
  true
end

#nerdz(message, to = nil) ⇒ Object



57
58
59
60
61
62
# File 'lib/nerdz.rb', line 57

def nerdz(message, to = nil)
  opts = {message: message}
  opts[:to] = self.user(to).id if to
  request(:nerdz_it, opts)
  true
end

#nerdzs(what, args = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nerdz.rb', line 71

def nerdzs(what, args = {})
  opts = {what: what}
  opts[:limit] = args[:from] ? "#{args[:from]},#{args[:max]}" : args[:max].to_s if args[:max]
  opts[:id] = self.user(args[:user]).id if args[:user]
  opts[:pid] = args[:pid] if args[:pid]
  res = request(:nerdzs, opts)
  if res.values.first.is_a?(Hash)
    res.values.map {|nerdz|
      Post.new(nerdz)
    }
  else
    Post.new(res)
  end
end

#request(*args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/nerdz.rb', line 30

def request(*args)
  res = JSON.parse(@http.request(*args).body)
  error = res['error']
  error = ERRORS[error ? error.to_i : error]
  raise error if error
  res
end

#share(url, message = "", to = nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/nerdz.rb', line 64

def share(url, message = "", to = nil)
  opts = {url: url, message: message}
  opts[:to] = self.user(to).id if to
  request(:share_it, opts)
  true
end

#user(id_or_name) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/nerdz.rb', line 48

def user(id_or_name)
  case id_or_name
  when Integer
    User.new(id_or_name, nil)
  when String, Symbol
    User.new(request(:get_id, {username: id_or_name})['id'], id_or_name.to_s)
  end
end