Class: Spacialdb::Client

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Helpers
Defined in:
lib/spacialdb/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(, password, host=Spacialdb::Auth.default_host)
  @login = 
  @password = password
  @host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/spacialdb/client.rb', line 19

def host
  @host
end

#loginObject

Returns the value of attribute login.



19
20
21
# File 'lib/spacialdb/client.rb', line 19

def 
  @login
end

#passwordObject

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(, password, host=Spacialdb::Auth.default_host)
  client = new(, password, host)
  json_decode client.get('/api/users/credentials', :accept => 'json').to_s
end

.gem_version_stringObject



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.(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

.versionObject



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

#createObject



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

:nodoc:



84
85
86
# File 'lib/spacialdb/client.rb', line 84

def delete(uri, extra_headers={})    # :nodoc:
  process(:delete, uri, extra_headers)
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

#extract_warning(response) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/spacialdb/client.rb', line 105

def extract_warning(response)
  return unless response
  if response.headers[:x_spacialdb_warning] && @warning_callback
    warning = response.headers[: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

:nodoc:



76
77
78
# File 'lib/spacialdb/client.rb', line 76

def get(uri, extra_headers={})    # :nodoc:
  process(:get, uri, extra_headers)
end

#listObject



42
43
44
# File 'lib/spacialdb/client.rb', line 42

def list
  json_decode get('/api/databases', :accept => 'json').to_s
end

#list_layersObject

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_fileObject



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

:nodoc:



80
81
82
# File 'lib/spacialdb/client.rb', line 80

def post(uri, payload="", extra_headers={})    # :nodoc:
  process(:post, uri, extra_headers, 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, extra_headers={}, payload=nil)
  headers  = spacialdb_headers.merge(extra_headers)
  args     = [method, payload, headers].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

  extract_warning(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 => , :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

#spacialdb_headersObject

:nodoc:



117
118
119
120
121
122
123
124
# File 'lib/spacialdb/client.rb', line 117

def spacialdb_headers   # :nodoc:
  {
    'X-Spacialdb-API-Version' => '1',
    'User-Agent'           => self.class.gem_version_string,
    'X-Ruby-Version'       => RUBY_VERSION,
    'X-Ruby-Platform'      => RUBY_PLATFORM
  }
end