Class: GrammarServerSession

Inherits:
Object
  • Object
show all
Defined in:
lib/nugramserver-ruby/nugramserver-ruby.rb

Overview

This class represents a session with NuGram Hosted Server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, username, password) ⇒ GrammarServerSession

Returns a new instance of GrammarServerSession.



49
50
51
52
53
54
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 49

def initialize(server, username, password)
  @server = server
  @username = username
  @password = password
  @sessionid = self.connect
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



47
48
49
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 47

def password
  @password
end

#serverObject (readonly)

Returns the value of attribute server.



47
48
49
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 47

def server
  @server
end

#usernameObject (readonly)

Returns the value of attribute username.



47
48
49
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 47

def username
  @username
end

Instance Method Details

#connectObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 61

def connect
  Net::HTTP.start(@server.host, @server.port) {|http|
    req = Net::HTTP::Post.new('/session')
    req.body= "responseFormat=json"
    req.basic_auth @username, @password
    response = http.request(req)
    case response
      when Net::HTTPSuccess then
         result = JSON.parse(response.body)
         result['session']['id']
      else
        response.error!
      end
  }
end

#disconnectObject

Terminates the session with NuGram Hosted Server



78
79
80
81
82
83
84
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 78

def disconnect
  Net::HTTP.start(@server.host, @server.port) {|http|
    req = Net::HTTP::Delete.new("/session/#{@sessionid}")
    req.basic_auth @username, @password
    response = http.request(req)
  }
end

#get_idObject

Returns the session ID



57
58
59
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 57

def get_id
  @sessionid
end

#instantiate(grammarPath, context) ⇒ Object

This method instantiates a dynamic grammar and loads it. The ‘context’ argument is expected to be a hash (dictionary) that maps strings to values. Each value must be convertible to standard JSON. Each key in the context must correspond to the name of a variable in the ABNF template.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 112

def instantiate(grammarPath, context)
  Net::HTTP.start(@server.host, @server.port) {|http|
    req = Net::HTTP::Post.new("/grammar/#{@sessionid}/#{grammarPath}")
    body = URI.escape(context.to_json)
    req.body= "responseFormat=json&context=#{context.to_json}"
    req.basic_auth @username, @password
    response = http.request(req)
    case response
      when Net::HTTPSuccess then
         result = JSON.parse(response.body)
         InstantiatedGrammar.new(self, result['grammar'])
      else
        response.error!
      end
  }    
end

#load(grammarPath) ⇒ Object

This method requests NuGram Hosted Server to load a static grammar.



103
104
105
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 103

def load(grammarPath)
  instantiate(grammarPath, {})
end

#upload(grammarPath, content) ⇒ Object

This method uploads a source grammar to NuGram Hosted Server.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nugramserver-ruby/nugramserver-ruby.rb', line 87

def upload(grammarPath, content)
  Net::HTTP.start(@server.host, @server.port) {|http|
    req = Net::HTTP::Put.new("/grammar/#{grammarPath}")
    req.body= content
    req.basic_auth @username, @password
    response = http.request(req)
    case response
      when Net::HTTPSuccess then
        true
      else
        response.error!
      end
  }    
end