Sha256: 0e8e9bceafa38da418a5b5dbcc84aa3d53df9418919f370c9211864122a14f33

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

module Moonshado
  class Configuration

    OPTIONS = [:api_key, :development_environments, :keywords,
               :sms_uri, :keyword_uri, :auto_register_keyword,
               :environment_name, :host, :http_open_timeout,
               :http_read_timeout, :port, :protocol, :secure].freeze

    attr_accessor :api_key
    attr_accessor :keywords
    attr_accessor :sms_uri
    attr_accessor :keywords_uri
    attr_accessor :secure
    attr_accessor :http_open_timeout
    attr_accessor :http_read_timeout
    attr_accessor :host
    attr_accessor :auto_register_keywords
    attr_accessor :production_environment
    attr_accessor :port

    alias_method :secure?, :secure

    def initialize
      @secure                   = false
      @host                     = 'heroku.moonshado.com'
      @http_open_timeout        = 10
      @http_read_timeout        = 10
      @production_environment   = true
      @sms_uri                  = '/sms'
      @keywords_uri             = '/keywords'
      @auto_register_keywords   = false
      @port                     = default_port
    end

    def api_key
      formatted_api_key if @production_environment
    end

    def formatted_api_key
      url_obj = URI.parse(@api_key)

      if (url_obj.class == URI::Generic)
        @api_key
      else
        url_obj.user
      end
    end

    def [](option)
      send(option)
    end

    def to_hash
      OPTIONS.inject({}) do |hash, option|
        hash.merge(option.to_sym => send(option))
      end
    end

    def merge(hash)
      to_hash.merge(hash)
    end

    def port
      @port || default_port
    end

    def protocol
      if secure?
        'https'
      else
        'http'
      end
    end

    private
      def default_port
        if secure?
          443
        else
          80
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
moonshado-sms-1.0.1 lib/moonshado/configuration.rb
moonshado-sms-1.0.0 lib/moonshado/configuration.rb