Module: FanyiAPI::API
Constant Summary collapse
- StrategyNotRegistered =
Class.new StandardError
Instance Method Summary collapse
-
#call(strategy = "youdao", query) ⇒ Object
call strategy with action and arguments.
-
#register(strategy, options = {}) ⇒ Object
register a strategy.
Instance Method Details
#call(strategy = "youdao", query) ⇒ Object
call strategy with action and arguments
20 21 22 23 24 |
# File 'lib/fanyi_api/api.rb', line 20 def call(strategy="youdao", query) stra = instance_variable_get :"@#{strategy}" raise StrategyNotRegistered if stra.nil? stra.(query) end |
#register(strategy, options = {}) ⇒ Object
register a strategy
9 10 11 12 13 14 15 16 17 |
# File 'lib/fanyi_api/api.rb', line 9 def register(strategy, ={}) stra_sym = :"@#{strategy}" stra = instance_variable_get stra_sym if stra.nil? strategy_class = eval("FanyiAPI::Strategies::#{strategy.camel_case}") instance_variable_set stra_sym, strategy_class.new().extend(FanyiAPI::Requester) define_method(strategy) { instance_variable_get stra_sym } end end |