Class: Spacialdb::Client
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_layer(name, database, table_name = nil, srid = 4326) ⇒ Object
-
#create ⇒ Object
-
#default_resource_options_for_uri(uri) ⇒ Object
-
#delete(uri, extra_headers = {}) ⇒ Object
-
#destroy(name) ⇒ Object
Destroy the database permanently.
-
#extract_warning(response) ⇒ Object
-
#get(uri, extra_headers = {}) ⇒ Object
-
#initialize(login, password, host = Spacialdb::Auth.default_host) ⇒ Client
constructor
A new instance of Client.
-
#list ⇒ Object
-
#list_layers ⇒ Object
Get a list of layers on the database.
-
#local_ca_file ⇒ Object
-
#post(uri, payload = "", extra_headers = {}) ⇒ Object
-
#process(method, uri, extra_headers = {}, payload = nil) ⇒ Object
-
#realize_full_uri(given) ⇒ Object
-
#remove_layer(name) ⇒ Object
-
#resource(uri, options = {}) ⇒ Object
-
#show_layer(name) ⇒ Object
-
#spacialdb_headers ⇒ Object
Methods included from Helpers
ask, confirm_command, display, echo_off, echo_on, error, home_directory, json_decode, json_encode, longest, redisplay
Constructor Details
#initialize(login, password, host = Spacialdb::Auth.default_host) ⇒ Client
Returns a new instance of Client.
21
22
23
24
25
|
# File 'lib/spacialdb/client.rb', line 21
def initialize(login, password, host=Spacialdb::Auth.default_host)
@login = login
@password = password
@host = host
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
19
20
21
|
# File 'lib/spacialdb/client.rb', line 19
def host
@host
end
|
#login ⇒ Object
Returns the value of attribute login.
19
20
21
|
# File 'lib/spacialdb/client.rb', line 19
def login
@login
end
|
#password ⇒ Object
Returns the value of attribute password.
19
20
21
|
# File 'lib/spacialdb/client.rb', line 19
def password
@password
end
|
Class Method Details
.auth(login, password, host = Spacialdb::Auth.default_host) ⇒ Object
37
38
39
40
|
# File 'lib/spacialdb/client.rb', line 37
def self.auth(login, password, host=Spacialdb::Auth.default_host)
client = new(login, password, host)
json_decode client.get('/api/users/credentials', :accept => 'json').to_s
end
|
.gem_version_string ⇒ Object
15
16
17
|
# File 'lib/spacialdb/client.rb', line 15
def self.gem_version_string
"spacialdb-gem/#{version}"
end
|
.signup(username, email, password, password_confirmation, host = Spacialdb::Auth.default_host) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/spacialdb/client.rb', line 27
def self.signup(username, email, password, password_confirmation, host=Spacialdb::Auth.default_host)
client = new(email, password, host)
json_decode client.post('/api/users',
{ :email => email,
:username => username,
:password => password,
:password_confirmation => password_confirmation },
:accept => 'json').to_s
end
|
.version ⇒ Object
11
12
13
|
# File 'lib/spacialdb/client.rb', line 11
def self.version
Spacialdb::VERSION
end
|
Instance Method Details
#add_layer(name, database, table_name = nil, srid = 4326) ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/spacialdb/client.rb', line 64
def add_layer(name, database, table_name=nil, srid=4326)
payload = {:name => name, :database => database}
payload.merge!(:table_name => table_name) unless table_name == nil
payload.merge!(:srid => srid) unless srid == 4326
post('/api/layers', payload, :accept => 'json')
end
|
#create ⇒ Object
46
47
48
|
# File 'lib/spacialdb/client.rb', line 46
def create
post('/api/databases', '', :accept => 'json')
end
|
#default_resource_options_for_uri(uri) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/spacialdb/client.rb', line 145
def default_resource_options_for_uri(uri)
if ENV["SPACIALDB_SSL_VERIFY"] == "disable"
{}
elsif realize_full_uri(uri) =~ %r|^https://beta.spacialdb.com|
{ :verify_ssl => OpenSSL::SSL::VERIFY_PEER, :ssl_ca_file => local_ca_file }
else
{}
end
end
|
#delete(uri, extra_headers = {}) ⇒ Object
84
85
86
|
# File 'lib/spacialdb/client.rb', line 84
def delete(uri, ={}) process(:delete, uri, )
end
|
#destroy(name) ⇒ Object
Destroy the database permanently.
51
52
53
|
# File 'lib/spacialdb/client.rb', line 51
def destroy(name)
delete("/api/databases/#{name}", :accept => 'json').to_s
end
|
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/spacialdb/client.rb', line 105
def (response)
return unless response
if response.[:x_spacialdb_warning] && @warning_callback
warning = response.[:x_spacialdb_warning]
@displayed_warnings ||= {}
unless @displayed_warnings[warning]
@warning_callback.call(warning)
@displayed_warnings[warning] = true
end
end
end
|
#get(uri, extra_headers = {}) ⇒ Object
76
77
78
|
# File 'lib/spacialdb/client.rb', line 76
def get(uri, ={}) process(:get, uri, )
end
|
#list ⇒ Object
42
43
44
|
# File 'lib/spacialdb/client.rb', line 42
def list
json_decode get('/api/databases', :accept => 'json').to_s
end
|
#list_layers ⇒ Object
Get a list of layers on the database
56
57
58
|
# File 'lib/spacialdb/client.rb', line 56
def list_layers()
json_decode get("/api/layers", :accept => 'json').to_s
end
|
#local_ca_file ⇒ Object
155
156
157
|
# File 'lib/spacialdb/client.rb', line 155
def local_ca_file
File.expand_path("../../../data/cacert.pem", __FILE__)
end
|
#post(uri, payload = "", extra_headers = {}) ⇒ Object
80
81
82
|
# File 'lib/spacialdb/client.rb', line 80
def post(uri, payload="", ={}) process(:post, uri, , payload)
end
|
#process(method, uri, extra_headers = {}, payload = nil) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/spacialdb/client.rb', line 88
def process(method, uri, ={}, payload=nil)
= .merge()
args = [method, payload, ].compact
resource_options = default_resource_options_for_uri(uri)
begin
response = resource(uri, resource_options).send(*args)
rescue RestClient::SSLCertificateNotVerified => ex
host = URI.parse(realize_full_uri(uri)).host
error "WARNING: Unable to verify SSL certificate for #{host}\nTo disable SSL verification, run with SPACIALDB_SSL_VERIFY=disable"
end
(response)
response
end
|
#realize_full_uri(given) ⇒ Object
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/spacialdb/client.rb', line 134
def realize_full_uri(given)
full_host = (host =~ /^http/) ? host : "https://#{host}"
host = URI.parse(full_host)
uri = URI.parse(given)
uri.host ||= host.host
uri.scheme ||= host.scheme || "https"
uri.path = (uri.path[0..0] == "/") ? uri.path : "/#{uri.path}"
uri.port = host.port if full_host =~ /\:\d+/
uri.to_s
end
|
#remove_layer(name) ⇒ Object
72
73
74
|
# File 'lib/spacialdb/client.rb', line 72
def remove_layer(name)
delete("/api/layers/#{name}", :accept => 'json').to_s
end
|
#resource(uri, options = {}) ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/spacialdb/client.rb', line 126
def resource(uri, options={})
RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
resource = RestClient::Resource.new(realize_full_uri(uri),
options.merge(:user => login, :password => password)
)
resource
end
|
#show_layer(name) ⇒ Object
60
61
62
|
# File 'lib/spacialdb/client.rb', line 60
def show_layer(name)
json_decode get("/api/layers/#{name}", :accept => 'json').to_s
end
|
117
118
119
120
121
122
123
124
|
# File 'lib/spacialdb/client.rb', line 117
def {
'X-Spacialdb-API-Version' => '1',
'User-Agent' => self.class.gem_version_string,
'X-Ruby-Version' => RUBY_VERSION,
'X-Ruby-Platform' => RUBY_PLATFORM
}
end
|