Class: Usman::SmsService
- Inherits:
-
Object
- Object
- Usman::SmsService
- Defined in:
- app/services/usman/sms_service.rb
Instance Attribute Summary collapse
-
#aws_credentials ⇒ Object
readonly
Returns the value of attribute aws_credentials.
-
#dialing_prefix ⇒ Object
readonly
Returns the value of attribute dialing_prefix.
-
#error_details ⇒ Object
readonly
Returns the value of attribute error_details.
-
#error_heading ⇒ Object
readonly
Returns the value of attribute error_heading.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#mobile_number ⇒ Object
readonly
Returns the value of attribute mobile_number.
-
#sns_client ⇒ Object
readonly
Returns the value of attribute sns_client.
Instance Method Summary collapse
- #clear_errors ⇒ Object
- #create_sns_client ⇒ Object
- #errors ⇒ Object
- #get_aws_credentials ⇒ Object
- #get_config_file ⇒ Object
-
#initialize(params) ⇒ SmsService
constructor
A new instance of SmsService.
- #send_sms ⇒ Object
- #set_error(key, hsh = {}) ⇒ Object
Constructor Details
#initialize(params) ⇒ SmsService
Returns a new instance of SmsService.
12 13 14 15 16 17 18 19 20 |
# File 'app/services/usman/sms_service.rb', line 12 def initialize(params) @dialing_prefix = params[:dialing_prefix] @mobile_number = params[:mobile_number] @message = params[:message] @aws_credentials = {} # Initialize error variables clear_errors end |
Instance Attribute Details
#aws_credentials ⇒ Object (readonly)
Returns the value of attribute aws_credentials.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def aws_credentials @aws_credentials end |
#dialing_prefix ⇒ Object (readonly)
Returns the value of attribute dialing_prefix.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def dialing_prefix @dialing_prefix end |
#error_details ⇒ Object (readonly)
Returns the value of attribute error_details.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def error_details @error_details end |
#error_heading ⇒ Object (readonly)
Returns the value of attribute error_heading.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def error_heading @error_heading end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def @error_message end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def @message end |
#mobile_number ⇒ Object (readonly)
Returns the value of attribute mobile_number.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def mobile_number @mobile_number end |
#sns_client ⇒ Object (readonly)
Returns the value of attribute sns_client.
7 8 9 |
# File 'app/services/usman/sms_service.rb', line 7 def sns_client @sns_client end |
Instance Method Details
#clear_errors ⇒ Object
76 77 78 79 80 |
# File 'app/services/usman/sms_service.rb', line 76 def clear_errors @error_heading = nil @error_message = nil @error_details = {} end |
#create_sns_client ⇒ Object
52 53 54 55 56 57 58 |
# File 'app/services/usman/sms_service.rb', line 52 def create_sns_client @sns_client = Aws::SNS::Client.new( region: @aws_credentials["region"], access_key_id: @aws_credentials["access_key_id"], secret_access_key: @aws_credentials["secret_access_key"] ) end |
#errors ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/services/usman/sms_service.rb', line 82 def errors { heading: @error_heading, message: @error_message, details: @error_details } end |
#get_aws_credentials ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/usman/sms_service.rb', line 26 def get_aws_credentials if File.exist?(get_config_file) @aws_credentials = YAML.load_file(get_config_file) unless @aws_credentials.is_a?(Hash) && @aws_credentials.has_key?("region") && @aws_credentials.has_key?("access_key_id") && @aws_credentials.has_key?("secret_access_key") set_error("sms.credentials.invalid_format", errors) return end begin create_sns_client rescue Aws::SNS::Errors::ServiceError set_error("sms.credentials.invalid_credentials", errors) return rescue set_error("sms.credentials.unexpected_error", errors) return end else set_error("sms.credentials.secret_file_not_found", errors) return end @aws_credentials end |
#get_config_file ⇒ Object
22 23 24 |
# File 'app/services/usman/sms_service.rb', line 22 def get_config_file 'config/aws-secret.yml' end |
#send_sms ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'app/services/usman/sms_service.rb', line 60 def send_sms # Get AWS Credentials to create an SNS Client get_aws_credentials if @error_heading.blank? phone_number = "#{@dialing_prefix}#{@mobile_number}" #@sns_client.publish(phone_number: phone_number, message: @message) end end |
#set_error(key, hsh = {}) ⇒ Object
70 71 72 73 74 |
# File 'app/services/usman/sms_service.rb', line 70 def set_error(key, hsh={}) @error_heading = I18n.t("#{key}.heading") @error_message = I18n.t("#{key}.message") @error_details = hsh if hsh.is_a?(Hash) end |