class PryTranslator::Provider::BaseProvider

Public Class Methods

new(href, api_param_key) click to toggle source
# File lib/pry-translator/provider.rb, line 10
def initialize(href, api_param_key)
  @api_href = href
  @api_param_hash = read_config(api_param_key)

  values = @api_param_hash.values
  if values.include? nil or values.include? ''
    puts required_key_tips(api_param_key)
    raise ProviderError.new('init provider error.')
  end
end

Public Instance Methods

get(api_url, args={}) click to toggle source
# File lib/pry-translator/provider.rb, line 43
def get(api_url, args={})
  conn = Faraday.new(@api_href)

  resp = conn.get api_url, (@api_param_hash.clone.merge! args)

  resp.body
end
read_config(keys) click to toggle source
# File lib/pry-translator/provider.rb, line 21
def read_config(keys)
  config_prefix = 'Pry.config.translator'
  
  keys.inject({}) do |hash, key|
    hash[key] = eval("#{config_prefix}_#{key.to_s}")

    hash
  end
end
required_key_tips(keys, href='') click to toggle source
# File lib/pry-translator/provider.rb, line 31
def required_key_tips(keys, href='')
  config_prefix = 'Pry.config.translator'

  info = "you are using #{ Pry.config.translator ||PryTranslator::DEFAULT_PROVIDER } service, you need configure\n"
  info << keys.inject('') do |str, key| 
    str += "    #{config_prefix}_#{key.to_s} = #{key.to_s.upcase}\n".colorize(:red)
  end

  info << "in ~/.pryrc file"
  info
end