Class: YoudaoFanyi::Connector

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/youdao_fanyi/connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnector

Returns a new instance of Connector.



22
23
24
25
26
# File 'lib/youdao_fanyi/connector.rb', line 22

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

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def error_code
  @error_code
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/youdao_fanyi/connector.rb', line 6

def key
  @key
end

#keyfromObject

Returns the value of attribute keyfrom.



6
7
8
# File 'lib/youdao_fanyi/connector.rb', line 6

def keyfrom
  @keyfrom
end

#qObject (readonly)

Returns the value of attribute q.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def q
  @q
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def response
  @response
end

#resultsObject (readonly)

Returns the value of attribute results.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def results
  @results
end

#translationObject (readonly)

Returns the value of attribute translation.



7
8
9
# File 'lib/youdao_fanyi/connector.rb', line 7

def translation
  @translation
end

Instance Method Details

#request(word, *options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/youdao_fanyi/connector.rb', line 28

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