class MoonropeClient::Connection
Attributes
@return [String] the endpoint hostname
Public Class Methods
Initialize a new connection object to an API
moonrope = MoonropeClient::Connection.new('myapp.com', :ssl => true, :path_prefix => 'api')
@param host [String] the hostname to connect to @param options [Hash] A hash of options for this connectin
# File lib/moonrope_client/connection.rb, line 12 def initialize(host, options = {}) @host, @options = host, options end
Public Instance Methods
# File lib/moonrope_client/connection.rb, line 78 def controller(name) MoonropeClient::Controller.new(self, name) end
@return [Hash] return headers to be set on all requests to the API
# File lib/moonrope_client/connection.rb, line 45 def headers @options[:headers] || {} end
# File lib/moonrope_client/connection.rb, line 82 def method_missing(name, value = nil) value.nil? ? self.controller(name) : super end
@return [String] the path prefix
# File lib/moonrope_client/connection.rb, line 24 def path_prefix @options[:path_prefix] || 'api' end
@return [Integer] the port to conncet to
# File lib/moonrope_client/connection.rb, line 38 def port @options[:port] || (ssl ? 443 : 80) end
Make a request to the remote API server and return the raw output from the request.
@param path [String] the full path to request @param params [Hash] a hash of parameters to send with the request
# File lib/moonrope_client/connection.rb, line 93 def raw_request(path, params = {}) request = Net::HTTP::Post.new(path) request.set_form_data(params) request.add_field 'User-Agent', self.user_agent headers.each { |k,v| request.add_field k, v } connection = Net::HTTP.new(self.host, self.port) connection.open_timeout = @options[:connect_timeout] || 10 if ssl connection.use_ssl = true connection.verify_mode = OpenSSL::SSL::VERIFY_PEER end result = connection.request(request) case result.code.to_i when 200 then result.body when 400 raise Error.new("Bad request (400)", body: result.body) when 403 raise Error.new("Access denied (403)", body: result.body) when 404 raise Error.new("Page not found (404)", body: result.body) when 301..309 raise Error.new("Redirect (#{result.code})", body: result.body, location: result['Location']) when 500 raise Error.new("Internal server error (500)", body: result.body) else raise Error.new("Error (#{result.code.to_i})", body: result.body) end rescue Net::OpenTimeout raise Error.new("Connection timeout to #{self.host}:#{self.port}") end
Make a request and return an appropriate request object.
@param controller [Symbol] the controller @param action [Symbol] the action @param params [Hash] parameters
# File lib/moonrope_client/connection.rb, line 70 def request(controller, action, params = {}) MoonropeClient::Request.new(self, controller, action, params).make end
# File lib/moonrope_client/connection.rb, line 74 def request!(controller, action, params = {}) MoonropeClient::Request.new(self, controller, action, params).make! end
@return [Boolean] whether or not SSL is enabled for requests or not
# File lib/moonrope_client/connection.rb, line 31 def ssl @options[:ssl] || false end
@return [String] the User-Agent to send with the request
# File lib/moonrope_client/connection.rb, line 59 def user_agent @options[:user_agent] || "Moonrope Ruby Library/#{MoonropeClient::VERSION}" end
@return [Integer] the version of the API to use
# File lib/moonrope_client/connection.rb, line 52 def version @options[:version] || 1 end