class RubyTCC::REST::Client
Constants
- ENDPOINT
- URL_PREFIX
- URL_SUFFIX
Attributes
session_id[RW]
Public Instance Methods
complete_post(method, result, options)
click to toggle source
Perform a standardized post with module information
# File lib/rubytcc/rest/client.rb, line 63 def complete_post(method, result, options) response = post(URL_SUFFIX + method, options) result = Object.const_get("RubyTCC::" + result + "Result") xml = REXML::Document.new(response.body).root status = xml.elements["Result"].text if status != "Success" then raise RubyTCC::Error::ResultError.new(status) else result.load_from_xml(REXML::Document.new(response.body).root) end end
connection_options()
click to toggle source
@return [Hash]
# File lib/rubytcc/rest/client.rb, line 21 def connection_options @connection_options ||= { :builder => middleware, :headers => { # :accept => 'application/json', :user_agent => user_agent, }, :request => { :open_timeout => 10, :timeout => 30, }, :proxy => proxy, :ssl => { :verify => false} } end
credentials?()
click to toggle source
@return [Boolean]
Calls superclass method
RubyTCC::Client#credentials?
# File lib/rubytcc/rest/client.rb, line 81 def credentials? super || session_id? end
get(path, params = {})
click to toggle source
Perform an HTTP GET request
# File lib/rubytcc/rest/client.rb, line 51 def get(path, params = {}) headers = request_headers(:get, URL_PREFIX + path, params) request(:get, path, params, headers) end
middleware()
click to toggle source
@note Faraday’s middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one. @see github.com/technoweenie/faraday#advanced-middleware-usage @see mislav.uniqpath.com/2011/07/faraday-advanced-http/ @return [Faraday::RackBuilder]
# File lib/rubytcc/rest/client.rb, line 41 def middleware @middleware ||= Faraday::RackBuilder.new do |faraday| # Encodes as "application/x-www-form-urlencoded" if not already encoded faraday.request :url_encoded # Set default HTTP adapter faraday.adapter :net_http end end
post(path, params = {})
click to toggle source
Perform an HTTP POST request
# File lib/rubytcc/rest/client.rb, line 57 def post(path, params = {}) headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, ENDPOINT + path, params, {}) : request_headers(:post, ENDPOINT + path, params) request(:post, path, params, headers) end
session_id?()
click to toggle source
@return [Boolean]
# File lib/rubytcc/rest/client.rb, line 76 def session_id? !!session_id end
Private Instance Methods
connection()
click to toggle source
# File lib/rubytcc/rest/client.rb, line 87 def connection @connection ||= Faraday.new(URL_PREFIX, connection_options) end
request(method, path, params = {}, headers = {})
click to toggle source
# File lib/rubytcc/rest/client.rb, line 91 def request(method, path, params = {}, headers = {}) connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env rescue Faraday::Error::TimeoutError, Timeout::Error => error raise(RubyTCC::Error::RequestTimeout.new(error)) #rescue Faraday::Error::ClientError, JSON::ParserError => error # raise(RubyTCC::Error.new(error)) end
request_headers(method, url, params = {}, signature_params = params)
click to toggle source
# File lib/rubytcc/rest/client.rb, line 99 def request_headers(method, url, params = {}, signature_params = params) headers = {} headers[:accept] = '*/*' headers[:content_type] = 'application/x-www-form-urlencoded; charset=UTF-8' headers end