Sha256: 1d1b81b51bf7fdf2883808c7fd3b1a85de02ebbb548aece075e0d0f62c50fd81

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Sms50X
  extend Configuration

  class Client

    attr_accessor :api_key, :country_code, :host

    def initialize(*args)
      options = args.last.is_a?(Hash) ? args.pop : {}

      @api_key = get_api_key(args[0])
      @country_code = get_country_code(args[1])

      if [@api_key, @country_code].any? { |k| k.nil? }
        raise ArgumentError, 'API key and country code are required'
      end

      @host = get_host

    end

    def send_message(phone, message)
      response = Faraday.get("#{host}/sms/#{api_key}/t=#{phone}&m=#{escape(message)}")
      response.body.to_i
    end

    def balance
      response = Faraday.get("#{host}/balance/#{api_key}")
      response.body.to_i
    end

    def get_stats(month = Date.today.month)
      response = Faraday.get("#{host}/stat/#{api_key}/#{month}")
      response.body.to_i
    end

    private

      def get_api_key(arg)
        [arg, Sms50X.api_key, ENV['SMS50X_API_KEY']].find{ |x| x }
      end

      def get_country_code(arg)
        [arg, Sms50X.country_code, ENV['SMS50X_COUNTRY_CODE']].find{ |x| x }
      end

      def get_host
        # ISO 3166-1 alpha-3 codes
        countries = {
          'GTM' => '502', # Guatemala
          'SLV' => '503', # El Salvador
          'HND' => '504', # Honduras
          'CRI' => '506', # Costa Rica
          'PAN' => '507'  # Panama
        }

        code = countries.fetch(country_code) { countries['CRI'] }

        "http://api.sms#{code}.com"
      end

      def escape(component)
        CGI.escape(component.to_s)
      end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sms50X-ruby-0.2.0 lib/sms50X/client.rb