Sha256: f2bae2a768e734f8295ecf8e8a7ab1e18d703647626e02b6e2a3ae2ca32e9b9b

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

#encoding: utf-8

module YoudaoFanyi
  class Connector

    attr_accessor :key, :keyfrom
    attr_reader :q, :results, :response, :error_code, :translation, :query

    include HTTParty

    base_uri 'http://fanyi.youdao.com/fanyiapi.do'
    default_params :type => :data, :doctype => :json, :version => 1.1

    # YoudaoFanyi::Configuration.http_proxy = 'http://127.0.0.1:8118'
    #or
    # > export http_proxy=http://localhost:8118
    if proxy = YoudaoFanyi::Configuration.http_proxy || ENV['http_proxy']
      proxy = URI.parse(proxy)
      http_proxy proxy.host, proxy.port
    end

    def initialize
      api_key = YoudaoFanyi.api_key()
      @key = api_key["key"] || api_key[:key]
      @keyfrom = api_key["keyfrom"] || api_key[:keyfrom]
    end

    def request(word, *options)
      options = options.extract_options!
      @q = word.to_s
      @response = self.class.get('', :query => {:q => word}.merge(:key => @key, :keyfrom => @keyfrom))
#      require "pry"
#      binding.pry
      @results = JSON.load(@response.body)
      @error_code, @query = @results['errorCode'], @results['query']
      @translation = @error_code == 0 ? @results['translation'].first : YoudaoFanyi::Errors.error_message(@error_code)
      self
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
youdao_fanyi-0.1.2.1 lib/youdao_fanyi/connector.rb
youdao_fanyi-0.1.2 lib/youdao_fanyi/connector.rb
youdao_fanyi-0.1.1 lib/youdao_fanyi/connector.rb
youdao_fanyi-0.1.0 lib/youdao_fanyi/connector.rb