class MooMoo::BaseCommand
Defines the basic command structure.
For OpenSRS api methods, create them like this, using the proper api action name and object:
register_service :action_one, :object_one
If you need customized responses, create a custom method:
def custom_action(parameter) api_action_one(... custom parameter ...) ... custom response processing ... end
Attributes
host[R]
key[R]
port[R]
response[R]
username[R]
Public Class Methods
new(params = {})
click to toggle source
Constructor
Required¶ ↑
* <tt>:host</tt> - host of the OpenSRS server * <tt>:key</tt> - private key * <tt>:username</tt> - username of the reseller
Optional¶ ↑
* <tt>:port</tt> - port to connect on
# File lib/moo_moo/base_command.rb, line 43 def initialize(params = {}) @host = params[:host] || MooMoo.config.host || raise(OpenSRSException, "Host is required") @key = params[:key] || MooMoo.config.key || raise(OpenSRSException, "Key is required") @username = params[:username] || MooMoo.config.username || raise(OpenSRSException, "Username is required") @port = params[:port] || MooMoo.config.port || 55443 end
register_service(action_name, object)
click to toggle source
Register an api service for the current class.
register_service :action_one, :object_one
A method called “api_action_one” will then be created.
Parameters¶ ↑
-
action_name
- the api action to be called -
object
- the object
# File lib/moo_moo/base_command.rb, line 28 def self.register_service(action_name, object) define_method("api_#{action_name}") do |*args| perform(action_name, object, args.first || {}) end end
Public Instance Methods
attributes()
click to toggle source
Returns the response attributes.
# File lib/moo_moo/base_command.rb, line 61 def attributes response.body['attributes'] end
message()
click to toggle source
Returns the response message if one is present
# File lib/moo_moo/base_command.rb, line 56 def message response.body['response_text'] end
successful?()
click to toggle source
Returns whether or not the command executed was successful
# File lib/moo_moo/base_command.rb, line 51 def successful? response.body['is_success'].to_i == 1 end
Private Instance Methods
faraday_request(action, object, params)
click to toggle source
Performs the Faraday
request.
# File lib/moo_moo/base_command.rb, line 80 def faraday_request(action, object, params) Faraday.new(:url => "https://#{host}:#{port}", :ssl => {:verify => true}) do |c| c.request :open_srs_xml_builder, action, object, params, key, username c.response :parse_open_srs c.response :open_srs_errors c.response :moo_moo_logger c.adapter :net_http end.post end
index_array(arr)
click to toggle source
perform(action, object, params = {})
click to toggle source
Runs a command
Required¶ ↑
* <tt>:command</tt> - command to run * <tt>:command</tt> - command to run
Optional¶ ↑
* <tt>:params</tt> - parameters for the command
# File lib/moo_moo/base_command.rb, line 75 def perform(action, object, params = {}) (@response = faraday_request(action, object, params)) && attributes end