Class: MoonropeClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope_client/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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')

Parameters:

  • host (String)

    the hostname to connect to

  • options (Hash) (defaults to: {})

    A hash of options for this connectin



12
13
14
# File 'lib/moonrope_client/connection.rb', line 12

def initialize(host, options = {})
  @host, @options = host, options
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

#hostString (readonly)

Returns the endpoint hostname.

Returns:

  • (String)

    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

#headersHash

Return headers to be set on all requests to the API

Returns:

  • (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_prefixString

Returns the path prefix.

Returns:

  • (String)

    the path prefix



24
25
26
# File 'lib/moonrope_client/connection.rb', line 24

def path_prefix
  @options[:path_prefix] || 'api'
end

#portInteger

Returns the port to conncet to.

Returns:

  • (Integer)

    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.

Parameters:

  • path (String)

    the full path to request

  • params (Hash) (defaults to: {})

    a hash of parameters to send with the request



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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)
  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 then raise Error, "Bad request (400)"
  when 403 then raise Error, "Access denied (403)"
  when 404 then raise Error, "Page not found (404)"
  when 500 then raise Error, "Internal server error (500)"
  else          raise Error, "Unexpected status code #{result.code.to_i}"
  end
end

#request(controller, action, params = {}) ⇒ Object

Make a request and return an appropriate request object.

Parameters:

  • controller (Symbol)

    the controller

  • action (Symbol)

    the action

  • params (Hash) (defaults to: {})

    parameters



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

#sslBoolean

Returns whether or not SSL is enabled for requests or not.

Returns:

  • (Boolean)

    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_agentString

Returns the User-Agent to send with the request.

Returns:

  • (String)

    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

#versionInteger

Returns the version of the API to use.

Returns:

  • (Integer)

    the version of the API to use



52
53
54
# File 'lib/moonrope_client/connection.rb', line 52

def version
  @options[:version] || 1
end