Class: CitySDK::API

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_sizeObject

Returns the value of attribute batch_size.



11
12
13
# File 'lib/citysdk/api.rb', line 11

def batch_size
  @batch_size
end

#errorObject (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

Returns:

  • (Boolean)


124
125
126
# File 'lib/citysdk/api.rb', line 124

def authorized?
  @connection.headers['X-Auth']
end

#create_flushObject



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

Raises:



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/citysdk/api.rb', line 144

def delete(path)
  if authorized? 
    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

Raises:



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_flushObject



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_flushObject



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

Raises:



156
157
158
159
160
161
162
163
164
# File 'lib/citysdk/api.rb', line 156

def post(path,data)
  if authorized? 
    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

Raises:



166
167
168
169
170
171
172
173
174
# File 'lib/citysdk/api.rb', line 166

def put(path,data)
  if authorized? 
    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

#releaseObject



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 authorized?
    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