class Smaak::Client

Attributes

identifier[R]
route_info[R]

Public Class Methods

new() click to toggle source
Calls superclass method Smaak::Associate::new
# File lib/smaak/client.rb, line 11
def initialize
  super
  set_route_info("")
end

Public Instance Methods

get(identifier, uri, body, ssl = false, ssl_verify = OpenSSL::SSL::VERIFY_PEER) click to toggle source
# File lib/smaak/client.rb, line 39
def get(identifier, uri, body, ssl = false, ssl_verify = OpenSSL::SSL::VERIFY_PEER)
  connect(Net::HTTP::Get, identifier, uri, body, ssl, ssl_verify)
end
post(identifier, uri, body, ssl = false, ssl_verify = OpenSSL::SSL::VERIFY_PEER) click to toggle source
# File lib/smaak/client.rb, line 43
def post(identifier, uri, body, ssl = false, ssl_verify = OpenSSL::SSL::VERIFY_PEER)
  connect(Net::HTTP::Post, identifier, uri, body, ssl, ssl_verify)
end
set_identifier(identifier) click to toggle source
# File lib/smaak/client.rb, line 20
def set_identifier(identifier)
  raise ArgumentError.new("Invalid identifier") unless Smaak::Utils.non_blank_string?(identifier)
  @identifier = identifier
end
set_private_key(key) click to toggle source
# File lib/smaak/client.rb, line 16
def set_private_key(key)
  set_key(key)
end
set_route_info(route_info) click to toggle source
# File lib/smaak/client.rb, line 25
def set_route_info(route_info)
  @route_info = route_info
  @route_info ||= ""
end
sign_request(associate_identifier, adaptor) click to toggle source
# File lib/smaak/client.rb, line 30
def sign_request(associate_identifier, adaptor)
  raise ArgumentError.new("Associate invalid") unless validate_associate(associate_identifier)
  associate = @association_store[associate_identifier]
  raise ArgumentError.new("Invalid adaptor") if adaptor.nil?
  auth_message = Smaak::AuthMessage.create(associate['public_key'].export, associate['psk'], @token_life, @identifier, @route_info, associate['encrypt'])
  adaptor.body = Smaak::Crypto.encrypt(adaptor.body, associate['public_key']) if auth_message.encrypt
  Smaak.sign_authorization_headers(@key, auth_message, adaptor, Smaak::Cavage04::SPECIFICATION)
end

Private Instance Methods

build_http(uri, ssl, ssl_verify) click to toggle source
# File lib/smaak/client.rb, line 65
def build_http(uri, ssl, ssl_verify)
  url = URI.parse(uri)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = ssl
  http.verify_mode = ssl_verify
  return url, http
end
build_request(connector, url, body, identifier) click to toggle source
# File lib/smaak/client.rb, line 73
def build_request(connector, url, body, identifier)
  req = connector.new(url.to_s)
  req.body = body
  adaptor = Smaak.create_adaptor(req)
  (sign_request(identifier, adaptor)).request      
end
connect(connector, identifier, uri, body, ssl, ssl_verify) click to toggle source
# File lib/smaak/client.rb, line 55
def connect(connector, identifier, uri, body, ssl, ssl_verify)
  url, http = build_http(uri, ssl, ssl_verify)
  req = build_request(connector, url, body, identifier)
  request_and_respond(http, req, identifier)

  rescue => ex
    puts "[smaak error] request to associate failed"
    throw ex
end
encrypt_associate?(identifier) click to toggle source
# File lib/smaak/client.rb, line 87
def encrypt_associate?(identifier)
  return false if identifier.nil?
  return false if @association_store[identifier].nil?
  return true if @association_store[identifier]["encrypt"] == true
  return true if @association_store[identifier]["encrypt"].to_s.downcase == "true"
  false
end
request_and_respond(http, req, identifier) click to toggle source
# File lib/smaak/client.rb, line 80
def request_and_respond(http, req, identifier)
  response = http.request(req)
  response.body = Smaak::Crypto.decrypt(response.body, @key) if encrypt_associate?(identifier) and response.code[0] == '2'
  puts "[smaak error]: response from #{identifier} was #{response.code}" unless response.code[0] == '2'
  response
end
validate_associate(associate_identifier) click to toggle source
# File lib/smaak/client.rb, line 49
def validate_associate(associate_identifier)
  return false if associate_identifier.nil?
  return false if @association_store[associate_identifier].nil?
  true
end