Class: Wit::REST::Client
- Inherits:
-
Object
- Object
- Wit::REST::Client
- Defined in:
- lib/wit_ruby/rest/client.rb
Overview
Wit::Session::Client class holds the authentication parameters and handles making the HTTP requests to the Wit API. These methods should be internally called and never called directly from the user. An example call to instantiate client is done like this with defaults:
> @client = Wit::Session:Client.new
Constant Summary collapse
- DEFAULTS =
Default settings for the client connection to the Wit api.
{ :token => ENV["WIT_AI_TOKEN"], :addr => 'api.wit.ai', :port => 443, :use_ssl => true, :ssl_verify_peer => true, :ssl_ca_file => File.dirname(__FILE__) + '/../../../conf/cacert.pem', :timeout => 30, :proxy_addr => nil, :proxy_port => nil, :proxy_user => nil, :proxy_pass => nil, :retry_limit => 1, }
Instance Attribute Summary collapse
-
#last_request ⇒ Object
readonly
Allows for the reading of the last request, last response, and the current session.
-
#last_response ⇒ Object
readonly
Allows for the reading of the last request, last response, and the current session.
-
#last_result ⇒ Object
readonly
Allows for the reading of the last request, last response, and the current session.
-
#session ⇒ Object
readonly
Allows for the reading of the last request, last response, and the current session.
Instance Method Summary collapse
-
#change_auth(new_auth) ⇒ Object
Change the given auth token.
-
#initialize(options = {}) ⇒ Wit::REST::Client
constructor
Initialize the new instance with either the default parameters or given parameters.
-
#request_from_result(rest, path, body) ⇒ Object
Takes in a body and path and creates a net/http class and uses it to call a request to API.
-
#rest_method ⇒ Wit::REST::Result
Defines each REST method for the given client.
Constructor Details
#initialize(options = {}) ⇒ Wit::REST::Client
Initialize the new instance with either the default parameters or given parameters. Token can either be given in options or defaults to ENV
42 43 44 45 46 47 48 |
# File 'lib/wit_ruby/rest/client.rb', line 42 def initialize( = {}) ## Token is overidden if given in set params. @params = DEFAULTS.merge @auth_token = @params[:token].strip setup_conn setup_session end |
Instance Attribute Details
#last_request ⇒ Object (readonly)
Allows for the reading of the last request, last response, and the current session.
35 36 37 |
# File 'lib/wit_ruby/rest/client.rb', line 35 def last_request @last_request end |
#last_response ⇒ Object (readonly)
Allows for the reading of the last request, last response, and the current session.
35 36 37 |
# File 'lib/wit_ruby/rest/client.rb', line 35 def last_response @last_response end |
#last_result ⇒ Object (readonly)
Allows for the reading of the last request, last response, and the current session.
35 36 37 |
# File 'lib/wit_ruby/rest/client.rb', line 35 def last_result @last_result end |
#session ⇒ Object (readonly)
Allows for the reading of the last request, last response, and the current session.
35 36 37 |
# File 'lib/wit_ruby/rest/client.rb', line 35 def session @session end |
Instance Method Details
#change_auth(new_auth) ⇒ Object
Change the given auth token.
54 55 56 |
# File 'lib/wit_ruby/rest/client.rb', line 54 def change_auth(new_auth) @auth_token = new_auth.strip end |
#request_from_result(rest, path, body) ⇒ Object
Takes in a body and path and creates a net/http class and uses it to call a request to API.
86 87 88 89 90 91 92 |
# File 'lib/wit_ruby/rest/client.rb', line 86 def request_from_result(rest, path, body) method_rest_class = Net::HTTP.const_get rest.capitalize refresh_request = method_rest_class.new path, {"Authorization" => "Bearer #{@auth_token}"} refresh_request.set_form_data(body) unless body == nil ## Connect and send it to the API server. return connect_send(refresh_request) end |
#rest_method ⇒ Wit::REST::Result
Defines each REST method for the given client. GET, PUT, POST and DELETE
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wit_ruby/rest/client.rb', line 62 [:get, :put, :post, :delete].each do |rest_method| ## Get the given class for Net::HTTP depending on the current method. method_rest_class = Net::HTTP.const_get rest_method.to_s.capitalize ## Define the actual method for Wit::Session:Client define_method rest_method do |path, params=nil, content_overide=nil| request = method_rest_class.new path, {"Authorization" => "Bearer #{@auth_token}"} ## If post or put, set content-type to be JSON if [:post, :put].include?(rest_method) request.body = params ## Will check if there is a Content-Type Header overide, and if not, default to ## JSON specific header. request["Content-Type"] = content_overide || "application/json" request["Accept"] = "application/vnd.wit.20160202+json" end return connect_send(request) end end |