Sha256: 5d0677b6c721364d66bcb0b55ed6876963ffa3a48683391728af20a1a4980fa4

Contents?: true

Size: 776 Bytes

Versions: 11

Compression:

Stored size: 776 Bytes

Contents

#encoding: UTF-8

module Termit
  class UrlConstructor
    Host = "https://www.bing.com"

    def initialize options
      @options = options
    end

    def url
      @options[:talk] ? construct_sound_url : construct_text_url
    end

    private

    def construct_text_url
      "#{Host}/translator/api/Translate/TranslateArray?from=#{@options.fetch(:source_lang)}&to=#{@options.fetch(:target_lang)}"
    end

    def construct_sound_url
      "#{Host}/translator/api/language/Speak?locale=#{transform_country_code(@options.fetch(:target_lang))}&gender=male&media=audio/mp3&text=#{URI.encode(@options.fetch(:text))}"
    end

    def transform_country_code(code)
      if code == :en
        'en-US'
      else
        "#{code}-#{code.upcase}"
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
termit-3.7.0 lib/termit/url_constructor.rb
termit-3.6.0 lib/termit/url_constructor.rb
termit-3.5.0 lib/termit/url_constructor.rb
termit-3.4.0 lib/termit/url_constructor.rb
termit-3.2.0 lib/termit/url_constructor.rb
termit-3.1.2 lib/termit/url_constructor.rb
termit-3.1.1 lib/termit/url_constructor.rb
termit-3.1.0 lib/termit/url_constructor.rb
termit-3.0.2 lib/termit/url_constructor.rb
termit-3.0.1 lib/termit/url_constructor.rb
termit-3.0.0 lib/termit/url_constructor.rb