2015-09-07 28 views
-2

我正在尝试拨打电话,但不断收到错误消息。这是我的代码:如何在Ruby中调用函数?

require 'rubygems' 
require 'net/http' 
require 'uri' 
require 'json' 

class AlchemyAPI 

    #Setup the endpoints 
    @@ENDPOINTS = {} 
    @@ENDPOINTS['taxonomy'] = {} 
    @@ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy' 
    @@ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy' 
    @@ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy' 

    @@BASE_URL = 'http://access.alchemyapi.com/calls' 

    def initialize() 

    begin 
     key = File.read('C:\Users\KVadher\Desktop\api_key.txt') 
     key.strip! 

     if key.empty? 
      #The key file should't be blank 
      puts 'The api_key.txt file appears to be blank, please copy/paste your API key in the file: api_key.txt' 
      puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html'    
      Process.exit(1) 
     end 

     if key.length != 40 
      #Keys should be exactly 40 characters long 
      puts 'It appears that the key in api_key.txt is invalid. Please make sure the file only includes the API key, and it is the correct one.' 
      Process.exit(1) 
     end 

     @apiKey = key 
    rescue => err 
     #The file doesn't exist, so show the message and create the file. 
     puts 'API Key not found! Please copy/paste your API key into the file: api_key.txt' 
     puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html' 

     #create a blank file to hold the key 
     File.open("api_key.txt", "w") {} 
     Process.exit(1) 
    end 
    end 

    # Categorizes the text for a URL, text or HTML. 
    # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/ 
    # For the docs, please refer to: http://www.alchemyapi.com/api/taxonomy/ 
    # 
    # INPUT: 
    # flavor -> which version of the call, i.e. url, text or html. 
    # data -> the data to analyze, either the the url, text or html code. 
    # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 
    # 
    # Available Options: 
    # showSourceText -> 0: disabled (default), 1: enabled. 
    # 
    # OUTPUT: 
    # The response, already converted from JSON to a Ruby object. 
    # 
    def taxonomy(flavor, data, options = {}) 


    unless @@ENDPOINTS['taxonomy'].key?(flavor) 
     return { 'status'=>'ERROR', 'statusInfo'=>'Taxonomy info for ' + flavor + ' not available' } 

    end 

    #Add the URL encoded data to the options and analyze 
    options[flavor] = data 
    return analyze(@@ENDPOINTS['taxonomy'][flavor], options) 
    print 

    end 

    **taxonomy(text,"trees",1)** 

end 

在** **我已经进入了我的电话。我在做什么不正确。我收到的错误是:

C:/Users/KVadher/Desktop/testrub:139:in `<class:AlchemyAPI>': undefined local variable or method `text' for AlchemyAPI:Class (NameError) 
    from C:/Users/KVadher/Desktop/testrub:6:in `<main>' 

我感觉好像我正在调用,并且api代码本身有问题吗?虽然我可能是错的。

+1

什么是'分类标准文本'(text,“trees”,1)'。那是因为'text'没有被定义。 –

+0

但是,当我定义文本时,出现以下错误:C:/ Users/KVadher/Desktop/testrub:140:in <':未定义方法'taxonomy'为AlchemyAPI:Class(NoMethodError) \t from C:/ Users/KVadher/Desktop/testrub:6:'

' – semiflex

+1

我认为'taxonomy(text,“trees”,1)'line应该在类之外。 'al = AlchemyAPI.new al.taxonomy(text,“trees”,1)' 使用您定义的'text'。 –

回答

1

是的,正如jon snow所说,函数(方法)调用必须在类之外。方法与类一起定义。

此外,Options应该是Hash,而不是一个数字,因为你打电话给options[flavor] = data,这会导致你另一个问题。

我相信也许你打算把text放在引号中,因为这是你的口味之一。

而且,因为你声明的类,这就是所谓的一个实例方法,你必须使类的实例来使用:

my_instance = AlchemyAPI.new 
my_taxonomy = my_instance.taxonomy("text", "trees") 

这足以让它开始工作,它似乎像你有办法让这一切都工作,虽然。祝你好运!