Skip to content
Browse files

get and post now handle adding app prefex to given path. Also catch a…

…ny errors during request and return nil
  • Loading branch information...
1 parent 0854678 commit 7f474e1946d469032bc3fba323095781f5caa83b Jordan Glasner committed Apr 4, 2010
Showing with 53 additions and 8 deletions.
  1. +53 −8 lib/nimble_nodes/server.rb
View
61 lib/nimble_nodes/server.rb
@@ -4,18 +4,63 @@ module NimbleNodes
class Server
- def self.post(path,args)
- url = URI.parse(url_to(path))
- request = Net::HTTP::Post.new(url.path)
- request.set_form_data({:token => ENV['NN_APP_TOKEN'], :json => args.to_json, :created_at => Time.now })
- response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
+ #= HTTP Requests
+ # GETs and POSTs both add App.token to request automatically
+
+ def self.get(path)
+ begin
+ path = NimbleNodes::App.path(path)
+ uri = uri_with_token(path)
+ path = uri.route_from(NimbleNodes::Server.url[0..-2]).to_s
+ request = Net::HTTP::Get.new(path)
+ return Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}.body
+ rescue
+ return nil
+ end
+ end
+
+
+ def self.post(path,params)
+ begin
+ path = NimbleNodes::App.path(path)
+ uri = uri(path)
+ request = Net::HTTP::Post.new(uri.path)
+ request.set_form_data({:token => NimbleNodes::App.token, :json => params.to_json, :created_at => Time.now })
+ return Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
+ rescue
+ return nil
+ end
+ end
+
+
+ #= Domain & URLs
+
+ # set ENV variable in development to override production domain
+ def self.domain
+ ENV['NIMBLE_NODES_DOMAIN'].nil? ? 'nimblenodes.com' : ENV['NIMBLE_NODES_DOMAIN']
end
- def self.url_to(path)
- 'http://' + ENV['NN_SERVER_DOMAIN'] + path
- # 'http://nimblenodes.com' + path
+ # returns string for url to given path at server
+ def self.url(path='/')
+ 'http://' + domain + path
end
+ # returns parsed URI for given path at server
+ def self.uri(path)
+ URI.parse(url(path))
+ end
+
+ # returns parsed URI with App.token in query
+ def self.uri_with_token(path)
+ uri = uri(path)
+ query = (uri.query || '').split('&')
+ query.push("token=#{NimbleNodes::App.token}")
+ uri.query = query.join('&')
+ uri
+ end
+
+
+
end
end

0 comments on commit 7f474e1

Please sign in to comment.
Something went wrong with that request. Please try again.