Class: STSPlatform::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/stsplatform/requesthandler.rb

Overview

Main handler of the library. Defines successors in the chain of responsibility and in our case handles the REST requests to the STS Platform.

Direct Known Subclasses

Client, Data, Fields, Orgs, Sensors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(successor = nil, id = "") ⇒ RequestHandler

Returns a new instance of RequestHandler.



12
13
14
15
16
17
18
# File 'lib/stsplatform/requesthandler.rb', line 12

def initialize(successor=nil,id="")
  @url = "http://wotkit.sensetecnic.com/api"
  @_successor = successor
  resource =  self.class.name.downcase.split('::').last || ''
  @resource = "/#{resource}/#{id.to_s}"
  @auth = nil
end

Instance Attribute Details

#_successorObject

Returns the value of attribute _successor.



10
11
12
# File 'lib/stsplatform/requesthandler.rb', line 10

def _successor
  @_successor
end

#authObject

Returns the value of attribute auth.



10
11
12
# File 'lib/stsplatform/requesthandler.rb', line 10

def auth
  @auth
end

#resourceObject

Returns the value of attribute resource.



10
11
12
# File 'lib/stsplatform/requesthandler.rb', line 10

def resource
  @resource
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/stsplatform/requesthandler.rb', line 10

def url
  @url
end

Instance Method Details

#delete(payload = nil, resource = "") ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/stsplatform/requesthandler.rb', line 96

def delete(payload=nil,resource="")
  unless @_successor.nil?
    return @_successor.delete(payload,@resource + resource)
  else
    uri = URI.parse(@url+resource)
    req = Net::HTTP::Delete.new(uri, initheader={'Content-Type'=>'application/json'})
    req.body = handle_payload(payload)
    unless handle_authentication().nil?
      req.basic_auth auth[:key_id].to_s, auth[:key_password].to_s
    end
    res = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }
    return handle_response(res)
  end
end

#get(params = nil, resource = "") ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stsplatform/requesthandler.rb', line 45

def get(params=nil,resource="")
  unless @_successor.nil?
    return @_successor.get(params,@resource + resource)
  else
    uri = URI.parse(@url+resource)
    uri.query = URI.encode_www_form(handle_params(params))
    req = Net::HTTP::Get.new(uri)
    unless handle_authentication().nil?
      req.basic_auth auth[:key_id].to_s, auth[:key_password].to_s
    end
    res = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }
    return handle_response(res)
  end
end

#post(payload = nil, resource = "") ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stsplatform/requesthandler.rb', line 62

def post(payload=nil,resource="")
  unless @_successor.nil?
    return @_successor.post(payload,@resource + resource)
  else
    uri = URI.parse(@url+resource)
    req = Net::HTTP::Post.new(uri, initheader={'Content-Type'=>'application/json'})
    req.body = handle_payload(payload)
    unless handle_authentication().nil?
      req.basic_auth auth[:key_id].to_s, auth[:key_password].to_s
    end
    res = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }
    return handle_response(res)
  end
end

#put(payload = nil, resource = "") ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stsplatform/requesthandler.rb', line 79

def put(payload=nil,resource="")
  unless @_successor.nil?
    return @_successor.put(payload,@resource + resource)
  else
    uri = URI.parse(@url+resource)
    req = Net::HTTP::Put.new(uri, initheader={'Content-Type'=>'application/json'})
    req.body = handle_payload(payload)
    unless handle_authentication().nil?
      req.basic_auth auth[:key_id].to_s, auth[:key_password].to_s
    end
    res = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }
    return handle_response(res)
  end
end

#set_config(config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stsplatform/requesthandler.rb', line 20

def set_config(config)
  if config.has_key?(:url)
    unless /http:\/\//.match(config[:url]).nil?
      @url = config[:url]
    else
      raise STSPlatform::STSPlatformError.new("Malformed URL string")
    end
  end
  if config.has_key?(:auth)
    unless config[:auth].has_key?(:username) || config[:auth].has_key?(:key_id)
        raise STSPlatform::STSPlatformError.new("Malformed Auth Hash. Must contain username or key_id")
    end
    if config[:auth].has_key?(:username)
      @auth = {key_id:config[:auth][:username],key_password:config[:auth][:password]}
    elsif config[:auth].has_key?(:key_id)
      @auth = config[:auth]
    end
  end

end

#set_handler(handler) ⇒ Object



41
42
43
# File 'lib/stsplatform/requesthandler.rb', line 41

def set_handler(handler)
  @_successor = handler
end