Class: CitySDK::API
- Inherits:
-
Object
- Object
- CitySDK::API
- Defined in:
- lib/citysdk/api.rb
Constant Summary collapse
- @@match_tpl =
{ :match => { :params => {} }, :nodes => [] }
- @@create_tpl =
{ :create => { :params => { :create_type => "create" } }, :nodes => [] }
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
- #authenticate(e, p) ⇒ Object
- #authorized? ⇒ Boolean
- #create_flush ⇒ Object
- #create_node(n) ⇒ Object
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(host, port = 80) ⇒ API
constructor
A new instance of API.
- #match_create_flush ⇒ Object
- #match_create_node(n) ⇒ Object
- #match_flush ⇒ Object
- #match_node(n) ⇒ Object
- #post(path, data) ⇒ Object
- #put(path, data) ⇒ Object
- #release ⇒ Object
- #set_createTemplate(ctpl) ⇒ Object
- #set_host(host, port = nil) ⇒ Object
- #set_layer(l) ⇒ Object
- #set_layer_status(status) ⇒ Object
- #set_matchTemplate(mtpl) ⇒ Object
- #tally(res) ⇒ Object
Constructor Details
#initialize(host, port = 80) ⇒ API
Returns a new instance of API.
28 29 30 31 32 33 34 |
# File 'lib/citysdk/api.rb', line 28 def initialize(host, port=80) @error = ''; @layer = ''; @batch_size = 10; @updated = @created = 0; set_host(host,port) end |
Instance Attribute Details
#batch_size ⇒ Object
Returns the value of attribute batch_size.
11 12 13 |
# File 'lib/citysdk/api.rb', line 11 def batch_size @batch_size end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'lib/citysdk/api.rb', line 10 def error @error end |
Instance Method Details
#authenticate(e, p) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/citysdk/api.rb', line 36 def authenticate(e,p) @email = e; @passw = p; if !( @host == 'api.dev' or @host == 'localhost' or @host == '127.0.0.1' or @host == '0.0.0.0') auth_connection = Faraday.new :url => "https://#{@host}", :ssl => {:verify => false} resp = auth_connection.get '/get_session', { :e => @email, :p => @passw } else resp = @connection.get '/get_session', { :e => @email, :p => @passw } end if resp.status == 200 resp = CitySDK::parseJson(resp.body) if resp[:status] == 'success' @connection.headers['X-Auth'] = resp[:results][0] else raise Exception.new(resp[:message]) end else raise Exception.new(resp.body) end if block_given? yield return self.release end true end |
#authorized? ⇒ Boolean
124 125 126 |
# File 'lib/citysdk/api.rb', line 124 def @connection.headers['X-Auth'] end |
#create_flush ⇒ Object
208 209 210 211 212 213 |
# File 'lib/citysdk/api.rb', line 208 def create_flush if @create[:nodes].length > 0 tally put("/nodes/#{@layer}",@create) @create[:nodes] = [] end end |
#create_node(n) ⇒ Object
119 120 121 122 |
# File 'lib/citysdk/api.rb', line 119 def create_node(n) @create[:nodes] << n create_flush if @create[:nodes].length >= @batch_size end |
#delete(path) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/citysdk/api.rb', line 144 def delete(path) if resp = @connection.delete(path) if resp.status == 200 return CitySDK::parseJson(resp.body) end @error = CitySDK::parseJson(resp.body)[:message] raise HostException.new(@error) end raise CitySDK::Exception.new("DEL needs authorization.") end |
#get(path) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/citysdk/api.rb', line 176 def get(path) resp = @connection.get(path) return CitySDK::parseJson(resp.body) if resp.status == 200 @error = CitySDK::parseJson(resp.body)[:message] raise HostException.new(@error) end |
#match_create_flush ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/citysdk/api.rb', line 183 def match_create_flush if @match[:nodes].length > 0 resp = post('util/match',@match) if resp[:nodes].length > 0 @create[:nodes] = resp[:nodes] res = put("/nodes/#{@layer}",@create) tally(res) @create[:nodes] = [] end @match[:nodes] = [] res end nil end |
#match_create_node(n) ⇒ Object
114 115 116 117 |
# File 'lib/citysdk/api.rb', line 114 def match_create_node(n) @match[:nodes] << n return match_create_flush if @match[:nodes].length >= @batch_size end |
#match_flush ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/citysdk/api.rb', line 199 def match_flush if @match[:nodes].length > 0 resp = post('util/match',@match) @match[:nodes] = [] return resp end end |
#match_node(n) ⇒ Object
108 109 110 111 112 |
# File 'lib/citysdk/api.rb', line 108 def match_node(n) @match[:nodes] << n return match_flush if @match[:nodes].length >= @batch_size return nil end |
#post(path, data) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/citysdk/api.rb', line 156 def post(path,data) if resp = @connection.post(path,data.to_json) return CitySDK::parseJson(resp.body) if resp.status == 200 @error = CitySDK::parseJson(resp.body)[:message] raise HostException.new(@error) end raise CitySDK::Exception.new("POST needs authorization.") end |
#put(path, data) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/citysdk/api.rb', line 166 def put(path,data) if resp = @connection.put(path,data.to_json) return CitySDK::parseJson(resp.body) if resp.status == 200 @error = CitySDK::parseJson(resp.body)[:message] raise HostException.new(@error) end raise CitySDK::Exception.new("PUT needs authorization.") end |
#release ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/citysdk/api.rb', line 128 def release match_flush create_flush # send any remaining entries in the create buffer match_create_flush if resp = @connection.get('/release_session') if resp.status == 200 @connection.headers.delete('X-Auth') else @error = CitySDK::parseJson(resp.body)[:message] raise HostException.new(@error) end end return [@updated, @created] end |
#set_createTemplate(ctpl) ⇒ Object
95 96 97 98 |
# File 'lib/citysdk/api.rb', line 95 def set_createTemplate(ctpl) ctpl[:nodes] = [] @create = @@create_tpl = ctpl end |
#set_host(host, port = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/citysdk/api.rb', line 63 def set_host(host,port=nil) @host = host @port = port if port.nil? if host =~ /^(.*):(\d+)$/ @port = $2 @host = $1 else @port = 80 end end @connection = Faraday.new :url => "http://#{@host}:#{@port}" @connection.headers = { :user_agent => 'CitySDK_API GEM ' + CitySDK::VERSION, :content_type => 'application/json' } begin get('/') rescue Exception => e raise CitySDK::Exception.new("Trouble connecting to api @ #{host}") end @create = @@create_tpl @match = @@match_tpl end |
#set_layer(l) ⇒ Object
100 101 102 |
# File 'lib/citysdk/api.rb', line 100 def set_layer(l) @layer = l end |
#set_layer_status(status) ⇒ Object
104 105 106 |
# File 'lib/citysdk/api.rb', line 104 def set_layer_status(status) put("/layer/#{@layer}/status",{:data => status}) end |
#set_matchTemplate(mtpl) ⇒ Object
90 91 92 93 |
# File 'lib/citysdk/api.rb', line 90 def set_matchTemplate(mtpl) mtpl[:nodes] = [] @match = @@match_tpl = mtpl end |
#tally(res) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/citysdk/api.rb', line 215 def tally(res) if res[:status] == "success" # TODO: also tally debug data! @updated += res[:create][:results][:totals][:updated] @created += res[:create][:results][:totals][:created] end end |