Class: YoudaoFanyi::Connector
- Inherits:
-
Object
- Object
- YoudaoFanyi::Connector
- Includes:
- HTTParty
- Defined in:
- lib/youdao_fanyi/connector.rb
Instance Attribute Summary collapse
-
#error_code ⇒ Object
readonly
Returns the value of attribute error_code.
-
#key ⇒ Object
Returns the value of attribute key.
-
#keyfrom ⇒ Object
Returns the value of attribute keyfrom.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#translation ⇒ Object
readonly
Returns the value of attribute translation.
Instance Method Summary collapse
-
#initialize ⇒ Connector
constructor
A new instance of Connector.
- #request(word, *options) ⇒ Object
Constructor Details
#initialize ⇒ Connector
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_code ⇒ Object (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 |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/youdao_fanyi/connector.rb', line 6 def key @key end |
#keyfrom ⇒ Object
Returns the value of attribute keyfrom.
6 7 8 |
# File 'lib/youdao_fanyi/connector.rb', line 6 def keyfrom @keyfrom end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
7 8 9 |
# File 'lib/youdao_fanyi/connector.rb', line 7 def q @q end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/youdao_fanyi/connector.rb', line 7 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/youdao_fanyi/connector.rb', line 7 def response @response end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
7 8 9 |
# File 'lib/youdao_fanyi/connector.rb', line 7 def results @results end |
#translation ⇒ Object (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 |
# File 'lib/youdao_fanyi/connector.rb', line 28 def request(word, *) = . @q = word.to_s.gsub("_"," ") @response = self.class.get('', :query => {:q => @q}.merge(:key => @key, :keyfrom => @keyfrom)) @results = JSON.load(@response.body) @error_code, @query = @results['errorCode'], @results['query'] @translation = @error_code == 0 ? @results['translation'].first : YoudaoFanyi::Errors.(@error_code) self end |