Class: SmsNotify::Api::Command
- Inherits:
-
Object
- Object
- SmsNotify::Api::Command
- Defined in:
- lib/sms_notify/api/command.rb
Overview
Takes care of the trenchwork in executing an API operation through the API in the form of an HTTP GET request.
Instance Method Summary collapse
-
#build_url(params_hash) ⇒ Object
Builds an escaped query string from a hash and returns a full URL complete with host, path, license_key and command provided at initialization.
-
#execute(parameters) ⇒ Object
Builds a URL fit for sending to CDYNE SmsNotify! API and returns the body of the HTTP response.
-
#initialize(cmd_name, license_key, secure = true, opts = {}) ⇒ Command
constructor
Create a new instance that can be used to interact with the API.
Constructor Details
#initialize(cmd_name, license_key, secure = true, opts = {}) ⇒ Command
Create a new instance that can be used to interact with the API.
Required Attributes
-
cmd_name
-
license_key
Optional Attributes
-
secure (defaults to true)
-
opts - hash of extra options (not currently used)
Example
command = Command.new('SendSMSBasic', 'secret_key', false)
24 25 26 27 28 29 |
# File 'lib/sms_notify/api/command.rb', line 24 def initialize(cmd_name, license_key, secure=true, opts={}) @name = cmd_name @license_key = license_key @secure = secure = opts end |
Instance Method Details
#build_url(params_hash) ⇒ Object
Builds an escaped query string from a hash and returns a full URL complete with host, path, license_key and command provided at initialization.
42 43 44 45 |
# File 'lib/sms_notify/api/command.rb', line 42 def build_url(params_hash) param_string = '?' + params_hash.merge(:LicenseKey => @license_key).map { |k, v| "#{::CGI.escape(k.to_s)}=#{::CGI.escape(v.to_s)}" }.sort.join('&') URI.parse(File.join(endpoint_url, @name + param_string)) end |
#execute(parameters) ⇒ Object
Builds a URL fit for sending to CDYNE SmsNotify! API and returns the body of the HTTP response.
33 34 35 36 37 |
# File 'lib/sms_notify/api/command.rb', line 33 def execute(parameters) url = build_url(parameters) response = get_response(url) handle_errors(response[0]).body end |