module FanyiAPI::Requester

Constants

InvalidQuery

Public Instance Methods

call(query) click to toggle source
# File lib/fanyi_api/requester.rb, line 10
def call(query)
  raise InvalidQuery, "Invalid query" if query.blank?
  resp = conn.get request_url, { query_key => query } 
  parse resp
end
conn() click to toggle source
# File lib/fanyi_api/requester.rb, line 6
def conn
  @conn ||= Faraday.new
end

Private Instance Methods

parse(resp) click to toggle source

get response body, if the body is nil or we fail to parse the body, return nil

# File lib/fanyi_api/requester.rb, line 20
def parse(resp)
  resp = resp[0] if resp.is_a?(Array)
  resp = resp.body
  begin
    JSON.parse resp
  rescue JSON::ParserError, TypeError
    nil
  end
end