Class: NimbleNodes::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/nimble_nodes/server.rb

Class Method Summary collapse

Class Method Details

.domainObject

set ENV variable in development to override production domain



39
40
41
# File 'lib/nimble_nodes/server.rb', line 39

def self.domain
  ENV['NIMBLE_NODES_DOMAIN'].nil? ? 'nimblenodes.com' : ENV['NIMBLE_NODES_DOMAIN']
end

.get(path) ⇒ Object

HTTP Requests

GETs and POSTs both add App.token to request automatically



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nimble_nodes/server.rb', line 10

def self.get(path)   
  begin
    path = NimbleNodes::App.path(path)
    uri = NimbleNodes::Server.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

.post(path, params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nimble_nodes/server.rb', line 23

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

.uri(path) ⇒ Object

returns parsed URI for given path at server



49
50
51
# File 'lib/nimble_nodes/server.rb', line 49

def self.uri(path)
  URI.parse(url(path))
end

.uri_with_token(path) ⇒ Object

returns parsed URI with App.token in query



54
55
56
57
58
59
60
# File 'lib/nimble_nodes/server.rb', line 54

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

.url(path = '/') ⇒ Object

returns string for url to given path at server



44
45
46
# File 'lib/nimble_nodes/server.rb', line 44

def self.url(path='/')
  'http://' + domain + path
end