Sha256: 4254f063d67773fc862b3811b04ff2d4106e0a619b4cb3d8791b76857a91fc9c

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Sms50X

  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 balance
      response = Faraday.get("#{host}/balance/#{api_key}")
      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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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