Sha256: d4308081718fe2b4a56df0819d24fb3f7617b7fe37df4c87cc2ae77e7a6bf604

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'erb'

require 'social_url/errors'
require 'social_url/version'

require 'social_url/message'
require 'social_url/facebook'
require 'social_url/google'
require 'social_url/pinterest'
require 'social_url/twitter'

module SocialUrl
  include ERB::Util

  class << self
    NETWORKS = [:facebook, :google, :pinterest, :twitter]
    KEYS = {
      u: :url,
      url: :u,
      description: :text,
      text: :description
    }

    def networks
      NETWORKS
    end

    def normalize(options)
      opts = {}

      options.each do |key, value|
        next unless value

        if key == :hashtags
          opts[key] = normalize_hashtags(value)
        elsif value.is_a?(String)
          opts[key] = normalize_string(value)
        elsif value.is_a?(Array)
          opts[key] = normalize_array(value)
        end
      end

      normalize_keys(opts)
    end

    def normalize_keys(options)
      opts = options.dup

      options.each do |key, value|
        opts[KEYS[key]] = opts[key] if KEYS[key]
      end

      opts
    end

    def normalize_hashtags(array)
      array.collect do |value|
        value.delete(' ')
      end.join(',')
    end

    def normalize_string(string)
      ERB::Util.url_encode(string)
    end

    def normalize_array(array)
      array.collect do |value|
        ERB::Util.url_encode(value)
      end.join(',')
    end

    def filtered_params(options, params)
      params.collect do |param|
        next unless options[param]
        "#{param}=#{options[param]}"
      end.compact.join('&')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
social-url-1.1.0 lib/social_url.rb