class Bluehouselab::Sms
Constants
- HEADERS
- HTTP_ERRORS
- ROUTES
- SEND_RESULT_ERRORS
Public Class Methods
new(options)
click to toggle source
# File lib/bluehouselab/sms.rb, line 40 def initialize(options) raise ArgumentError.new "api_key cannot be nil" unless options[:api_key] raise ArgumentError.new "app_id cannot be nil" unless options[:app_id] raise ArgumentError.new "sender cannot be nil" unless options[:sender] @api_key = options[:api_key] @app_id = options[:app_id] @sender = options[:sender] @base_url = "https://#{@app_id}:#{@api_key}@api.bluehouselab.com/smscenter/v1.0/" end
Public Instance Methods
result(request_id)
click to toggle source
# File lib/bluehouselab/sms.rb, line 64 def result(request_id) res = HTTParty.get "#{@base_url}#{ROUTES[:result]}/#{request_id}", { :headers => HEADERS } parse_result_response res.code, res.parsed_response end
send_message(options)
click to toggle source
# File lib/bluehouselab/sms.rb, line 50 def send_message(options) raise ArgumentError.new "send_message options requires a type either :sms or :lms" unless options[:type] raise ArgumentError.new "send_message options requires an array of receiver phone numbers" unless options[:receivers] && options[:receivers].is_a?(Array) raise ArgumentError.new "send_message options requires content" unless options[:content] type = options[:type].nil? ? :sms : options[:type] raise ArgumentError.new "send_message options[:type] must be one of :sms or :lms" unless type == :sms || type == :lms raise "content too long for type :sms" unless (options[:content].bytesize <= 80 && type == :sms) || ( type == :lms) res = send type, options[:receivers], options[:content], options[:subject] parse_send_response res.code, res.parsed_response end
Private Instance Methods
parse_error_and_code(code)
click to toggle source
# File lib/bluehouselab/sms.rb, line 71 def parse_error_and_code(code) if code >= 400 error = HTTP_ERRORS[code] success = false else error = nil success = true end { :error => error, :success => success, :status_code => code } end
parse_result_response(code,parsed_body)
click to toggle source
# File lib/bluehouselab/sms.rb, line 82 def parse_result_response(code,parsed_body) error_and_code = parse_error_and_code(code) if error_and_code[:success] if parsed_body['status'] == 0 error_and_code.merge! :sent_time => Time.parse(parsed_body['sent_time']) else error = SEND_RESULT_ERRORS[parsed_body['status']] error_and_code.merge!({ :error => error }) end else error_and_code end end
parse_send_response(code,parsed_body)
click to toggle source
# File lib/bluehouselab/sms.rb, line 96 def parse_send_response(code,parsed_body) error_and_code = parse_error_and_code(code) { :message_ids => code >= 400 ? [] : parsed_body["sent"] }.merge! error_and_code end
send(type,receivers,content,subject)
click to toggle source
# File lib/bluehouselab/sms.rb, line 101 def send(type,receivers,content,subject) body = { :sender => @sender, :receivers => receivers, :content => content } if subject && type == :lms body.merge! :subject => subject end HTTParty.post "#{@base_url}#{ROUTES[type]}", :body => body.to_json, :headers => HEADERS end