Class: MoonropeClient::Connection
- Inherits:
-
Object
- Object
- MoonropeClient::Connection
- Defined in:
- lib/moonrope_client/connection.rb
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
The endpoint hostname.
Instance Method Summary collapse
- #controller(name) ⇒ Object
-
#headers ⇒ Hash
Return headers to be set on all requests to the API.
-
#initialize(host, options = {}) ⇒ Connection
constructor
Initialize a new connection object to an API.
- #method_missing(name, value = nil) ⇒ Object
-
#path_prefix ⇒ String
The path prefix.
-
#port ⇒ Integer
The port to conncet to.
-
#raw_request(path, params = {}) ⇒ Object
Make a request to the remote API server and return the raw output from the request.
-
#request(controller, action, params = {}) ⇒ Object
Make a request and return an appropriate request object.
- #request!(controller, action, params = {}) ⇒ Object
-
#ssl ⇒ Boolean
Whether or not SSL is enabled for requests or not.
-
#user_agent ⇒ String
The User-Agent to send with the request.
-
#version ⇒ Integer
The version of the API to use.
Constructor Details
#initialize(host, options = {}) ⇒ Connection
Initialize a new connection object to an API
moonrope = MoonropeClient::Connection.new('myapp.com', :ssl => true, :path_prefix => 'api')
12 13 14 |
# File 'lib/moonrope_client/connection.rb', line 12 def initialize(host, = {}) @host, @options = host, end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, value = nil) ⇒ Object
82 83 84 |
# File 'lib/moonrope_client/connection.rb', line 82 def method_missing(name, value = nil) value.nil? ? self.controller(name) : super end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns the endpoint hostname.
19 20 21 |
# File 'lib/moonrope_client/connection.rb', line 19 def host @host end |
Instance Method Details
#controller(name) ⇒ Object
78 79 80 |
# File 'lib/moonrope_client/connection.rb', line 78 def controller(name) MoonropeClient::Controller.new(self, name) end |
#headers ⇒ Hash
Return headers to be set on all requests to the API
45 46 47 |
# File 'lib/moonrope_client/connection.rb', line 45 def headers @options[:headers] || {} end |
#path_prefix ⇒ String
Returns the path prefix.
24 25 26 |
# File 'lib/moonrope_client/connection.rb', line 24 def path_prefix @options[:path_prefix] || 'api' end |
#port ⇒ Integer
Returns the port to conncet to.
38 39 40 |
# File 'lib/moonrope_client/connection.rb', line 38 def port @options[:port] || (ssl ? 443 : 80) end |
#raw_request(path, params = {}) ⇒ Object
Make a request to the remote API server and return the raw output from the request.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 |
#request(controller, action, params = {}) ⇒ Object
Make a request and return an appropriate request object.
70 71 72 |
# File 'lib/moonrope_client/connection.rb', line 70 def request(controller, action, params = {}) MoonropeClient::Request.new(self, controller, action, params).make end |
#request!(controller, action, params = {}) ⇒ Object
74 75 76 |
# File 'lib/moonrope_client/connection.rb', line 74 def request!(controller, action, params = {}) MoonropeClient::Request.new(self, controller, action, params).make! end |
#ssl ⇒ Boolean
Returns whether or not SSL is enabled for requests or not.
31 32 33 |
# File 'lib/moonrope_client/connection.rb', line 31 def ssl @options[:ssl] || false end |
#user_agent ⇒ String
Returns the User-Agent to send with the request.
59 60 61 |
# File 'lib/moonrope_client/connection.rb', line 59 def user_agent @options[:user_agent] || "Moonrope Ruby Library/#{MoonropeClient::VERSION}" end |
#version ⇒ Integer
Returns the version of the API to use.
52 53 54 |
# File 'lib/moonrope_client/connection.rb', line 52 def version @options[:version] || 1 end |