Module: TheBigDB
- Defined in:
- lib/thebigdb/module_attribute_accessors.rb,
lib/thebigdb.rb,
lib/thebigdb/helpers.rb,
lib/thebigdb/request.rb,
lib/thebigdb/version.rb,
lib/thebigdb/resources/user.rb,
lib/thebigdb/resources/statement.rb
Overview
Heavily inspired by “core_ext/module/attribute_accessors” from activesupport (4.0.0.beta)
Defined Under Namespace
Modules: Helpers, VERSION
Classes: Request, StatementRequest
Constant Summary
collapse
- DEFAULT_CONFIGURATION =
{
"api_key" => nil,
"api_host" => "api.thebigdb.com",
"api_port" => 80,
"api_version" => "1",
"use_ssl" => false,
"verify_ssl_certificates" => false,
"before_request_execution" => Proc.new{},
"after_request_execution" => Proc.new{},
"raise_on_api_status_error" => false,
"http_request_executor" => Proc.new {|http, http_request| http.request(http_request) }
}
Class Method Summary
collapse
Class Method Details
.after_request_execution=(object) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/thebigdb.rb', line 62
def self.after_request_execution=(object)
unless object.is_a?(Proc)
raise ArgumentError, "You must pass a proc or lambda"
end
@@after_request_execution = object
end
|
.before_request_execution=(object) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/thebigdb.rb', line 55
def self.before_request_execution=(object)
unless object.is_a?(Proc)
raise ArgumentError, "You must pass a proc or lambda"
end
@@before_request_execution = object
end
|
.create(nodes = {}) ⇒ Object
55
56
57
|
# File 'lib/thebigdb/resources/statement.rb', line 55
def self.create(nodes = {})
StatementRequest.new("create").with("nodes" => nodes)
end
|
.downvote(id = "") ⇒ Object
67
68
69
|
# File 'lib/thebigdb/resources/statement.rb', line 67
def self.downvote(id = "")
StatementRequest.new("downvote").with("id" => id)
end
|
.mattr_accessor(*syms) ⇒ Object
39
40
41
42
|
# File 'lib/thebigdb/module_attribute_accessors.rb', line 39
def self.mattr_accessor(*syms)
mattr_reader(*syms)
mattr_writer(*syms)
end
|
.mattr_reader(*syms) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/thebigdb/module_attribute_accessors.rb', line 3
def self.mattr_reader(*syms)
syms.each do |sym|
raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
class_eval(" @@\#{sym} = nil unless defined? @@\#{sym}\n\n def self.\#{sym}\n @@\#{sym}\n end\n EOS\n\n class_eval(<<-EOS, __FILE__, __LINE__ + 1)\n def \#{sym}\n @@\#{sym}\n end\n EOS\n end\nend\n", __FILE__, __LINE__ + 1)
|
.mattr_writer(*syms) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/thebigdb/module_attribute_accessors.rb', line 22
def self.mattr_writer(*syms)
syms.each do |sym|
raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
class_eval(" def self.\#{sym}=(obj)\n @@\#{sym} = obj\n end\n EOS\n\n class_eval(<<-EOS, __FILE__, __LINE__ + 1)\n def \#{sym}=(obj)\n @@\#{sym} = obj\n end\n EOS\n end\nend\n", __FILE__, __LINE__ + 1)
|
.reset_default_configuration ⇒ Object
36
37
38
39
40
|
# File 'lib/thebigdb.rb', line 36
def self.reset_default_configuration
DEFAULT_CONFIGURATION.each_pair do |key, value|
send(key + "=", value)
end
end
|
.search(nodes = {}) ⇒ Object
51
52
53
|
# File 'lib/thebigdb/resources/statement.rb', line 51
def self.search(nodes = {})
StatementRequest.new("search").with("nodes" => nodes)
end
|
.send_request(*args) ⇒ Object
Shortcut: prepares, executes and returns Hash containing the server’s response
81
82
83
84
85
86
|
# File 'lib/thebigdb.rb', line 81
def self.send_request(*args)
request = Request.new
request.prepare(*args)
request.execute
request.response
end
|
.show(id = "") ⇒ Object
59
60
61
|
# File 'lib/thebigdb/resources/statement.rb', line 59
def self.show(id = "")
StatementRequest.new("show").with("id" => id)
end
|
.Statement(action, params) ⇒ Object
Generic request to statements executor
3
4
5
6
7
8
9
10
|
# File 'lib/thebigdb/resources/statement.rb', line 3
def self.Statement(action, params)
method = ["get", "show", "search"].include?(action.to_s) ? "GET" : "POST"
path = "/statements/#{action}"
request = TheBigDB::Request.new
request.prepare(method, path, params)
request.execute
end
|
.upvote(id = "") ⇒ Object
63
64
65
|
# File 'lib/thebigdb/resources/statement.rb', line 63
def self.upvote(id = "")
StatementRequest.new("upvote").with("id" => id)
end
|
.use_ssl=(bool) ⇒ Object
43
44
45
46
|
# File 'lib/thebigdb.rb', line 43
def self.use_ssl=(bool)
@@api_port = bool ? 443 : 80
@@use_ssl = bool
end
|
.User(action, params) ⇒ Object
2
3
4
5
6
7
8
9
|
# File 'lib/thebigdb/resources/user.rb', line 2
def self.User(action, params)
method = "GET"
path = "/users/#{action}"
request = TheBigDB::Request.new
request.prepare(method, path, params)
request.execute
end
|
.verify_ssl_certificates=(bool) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/thebigdb.rb', line 48
def self.verify_ssl_certificates=(bool)
if bool
raise NotImplementedError, "The certificates are never checked"
end
@@verify_ssl_certificates = bool
end
|
.with_configuration(new_configuration, &block) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/thebigdb.rb', line 69
def self.with_configuration(new_configuration, &block)
current_configuration = Hash[DEFAULT_CONFIGURATION.keys.map{|k| [k, send(k)] }]
new_configuration.each_pair{|k,v| send(k.to_s + "=", v) }
begin
yield
ensure
current_configuration.each_pair{|k,v| send(k.to_s + "=", v) }
end
end
|