Class: CitySDK::API
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#format ⇒ Object
Returns the value of attribute format.
-
#last_result ⇒ Object
readonly
Returns the value of attribute last_result.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
Instance Method Summary collapse
- #add_format(path) ⇒ Object
- #authenticate(name, password) ⇒ Object
- #authorized? ⇒ Boolean
- #create_flush ⇒ Object
- #create_object(n) ⇒ Object
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(host) ⇒ API
constructor
A new instance of API.
- #layers ⇒ Object
- #next ⇒ Object
- #objects(layer = nil) ⇒ Object
- #owners ⇒ Object
- #patch(path, data) ⇒ Object
- #post(path, data) ⇒ Object
- #put(path, data) ⇒ Object
- #release ⇒ Object
- #set_host(host) ⇒ Object
- #set_layer(l) ⇒ Object
- #tally(res) ⇒ Object
- #write(method, path, data = nil) ⇒ Object
Constructor Details
#initialize(host) ⇒ API
Returns a new instance of API.
16 17 18 19 20 21 22 23 24 |
# File 'lib/citysdk/api.rb', line 16 def initialize(host) @error = ''; @layer = ''; @batch_size = 1000; @per_page = 25; @format = 'jsonld' @updated = @created = 0; set_host(host) end |
Instance Attribute Details
#batch_size ⇒ Object
Returns the value of attribute batch_size.
12 13 14 |
# File 'lib/citysdk/api.rb', line 12 def batch_size @batch_size end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
11 12 13 |
# File 'lib/citysdk/api.rb', line 11 def error @error end |
#format ⇒ Object
Returns the value of attribute format.
14 15 16 |
# File 'lib/citysdk/api.rb', line 14 def format @format end |
#last_result ⇒ Object (readonly)
Returns the value of attribute last_result.
10 11 12 |
# File 'lib/citysdk/api.rb', line 10 def last_result @last_result end |
#per_page ⇒ Object
Returns the value of attribute per_page.
13 14 15 |
# File 'lib/citysdk/api.rb', line 13 def per_page @per_page end |
Instance Method Details
#add_format(path) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/citysdk/api.rb', line 76 def add_format(path) if path !~ /format/ path = path + ((path =~ /\?/) ? "&" : "?") + "format=#{@format}" end return path if path =~ /per_page/ path + "&per_page=#{@per_page}" end |
#authenticate(name, password) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/citysdk/api.rb', line 26 def authenticate(name, password) @name = name; @password = password; resp = @connection.get '/session', { name: @name, password: @password } if resp.status.between?(200, 299) resp = CitySDK::parse_json(resp.body) if (resp.class == Hash) and resp[:session_key] @connection.headers['X-Auth'] = resp[:session_key] else raise Exception.new('Invalid credentials') end else raise Exception.new(resp.body) end if block_given? begin yield ensure self.release end end true end |
#authorized? ⇒ Boolean
110 111 112 |
# File 'lib/citysdk/api.rb', line 110 def !! @connection.headers['X-Auth'] end |
#create_flush ⇒ Object
175 176 177 178 179 180 |
# File 'lib/citysdk/api.rb', line 175 def create_flush if @create[:features].length > 0 tally post("/layers/#{@layer}/objects",@create) @create[:features] = [] end end |
#create_object(n) ⇒ Object
105 106 107 108 |
# File 'lib/citysdk/api.rb', line 105 def create_object(n) @create[:features] << n create_flush if @create[:features].length >= @batch_size end |
#delete(path) ⇒ Object
129 130 131 |
# File 'lib/citysdk/api.rb', line 129 def delete(path) write :delete, path end |
#get(path) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/citysdk/api.rb', line 166 def get(path) resp = @connection.get(add_format(path)) @next = (resp.headers['Link'] =~ /^<(.+)>;\s*rel="next"/) ? $1 : nil @last_result = { status: resp.status, headers: resp.headers } return CitySDK::parse_json(resp.body) if resp.status.between?(200, 299) @error = CitySDK::parse_json(resp.body)[:error] raise HostException.new(@error) end |
#layers ⇒ Object
93 94 95 |
# File 'lib/citysdk/api.rb', line 93 def layers get "/layers" end |
#next ⇒ Object
89 90 91 |
# File 'lib/citysdk/api.rb', line 89 def next @next ? get(@next) : "{}" end |
#objects(layer = nil) ⇒ Object
101 102 103 |
# File 'lib/citysdk/api.rb', line 101 def objects(layer=nil) !!layer ? get("/layers/#{layer}/objects") : get("/objects") end |
#owners ⇒ Object
97 98 99 |
# File 'lib/citysdk/api.rb', line 97 def owners get "/owners" end |
#patch(path, data) ⇒ Object
137 138 139 |
# File 'lib/citysdk/api.rb', line 137 def patch(path, data) write :patch, path, data end |
#post(path, data) ⇒ Object
133 134 135 |
# File 'lib/citysdk/api.rb', line 133 def post(path, data) write :post, path, data end |
#put(path, data) ⇒ Object
141 142 143 |
# File 'lib/citysdk/api.rb', line 141 def put(path, data) write :put, path, data end |
#release ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/citysdk/api.rb', line 114 def release # send any remaining entries in the create buffer create_flush if resp = @connection.delete('/session') if resp.status.between?(200, 299) @connection.headers.delete('X-Auth') else @error = CitySDK::parse_json(resp.body)[:error] raise HostException.new(@error) end end return {created: @created} end |
#set_host(host) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/citysdk/api.rb', line 51 def set_host(host) host = "https://" + host if host.index('http').nil? if host.index('https') == 0 @connection = Faraday.new url: host, ssl: { verify: false } else @connection = Faraday.new url: host end @connection.headers = { :user_agent => 'CitySDK LD API GEM ' + CitySDK::VERSION, :content_type => 'application/json' } begin get('/') rescue Exception => e raise CitySDK::Exception.new("Trouble connecting to API @ #{host}") end @create = { type: "FeatureCollection", features: [] } end |
#set_layer(l) ⇒ Object
84 85 86 87 |
# File 'lib/citysdk/api.rb', line 84 def set_layer(l) create_flush @layer = l end |
#tally(res) ⇒ Object
182 183 184 |
# File 'lib/citysdk/api.rb', line 182 def tally(res) @created += res.length end |
#write(method, path, data = nil) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/citysdk/api.rb', line 145 def write(method, path, data = nil) if payload = data ? data.to_json : nil resp = @connection.send(method, path, payload) @last_result = { status: resp.status, headers: resp.headers } if resp.status == 401 and @name and @password # API was authenticated before, so probably timed out. Try again! if authenticate(@name, @password) resp = @connection.send(method, path, payload) @last_result = { status: resp.status, headers: resp.headers } end end return CitySDK::parse_json(resp.body) if resp.status.between?(200, 299) @error = CitySDK::parse_json(resp.body)[:error] || {status: resp.status} raise HostException.new(@error) end raise CitySDK::Exception.new("#{method.upcase} needs authorization") end |