Class: YahooKeyphraseApi::KeyPhrase

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_keyphrase_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(appid = YahooKeyphraseApi::Config.app_id) ⇒ KeyPhrase

initializer if creating a new Yahoo Keyphrase API client

Parameters:

  • appid (defaults to: YahooKeyphraseApi::Config.app_id)

    Yahoo APP id

Raises:



28
29
30
31
# File 'lib/yahoo_keyphrase_api.rb', line 28

def initialize(appid = YahooKeyphraseApi::Config.app_id)
  @app_key = appid
  raise YahooKeyPhraseApiError.new('please set app key before use') unless @app_key
end

Instance Method Details

#extract(sentence = '', method = :POST) ⇒ Object

Retrieve a keyphrase

Parameters:

  • sentence (defaults to: '')
  • method (defaults to: :POST)

    :GET or :POST

Returns:

  • hash



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yahoo_keyphrase_api.rb', line 37

def extract(sentence='', method=:POST)
  if method == :GET
    response = HTTParty.get("#{SITE_URL}?appid=#{@app_key}&sentence=#{URI.escape(sentence)}&output=json")
    response.body
  elsif method == :POST
    response = HTTParty.post("#{SITE_URL}", {
      body: {
        appid: @app_key,
        sentence: sentence,
        output: 'json'
      }
    })
    # 413 Request Entity Too Large
    raise YahooKeyPhraseApiError.new(response.message) if response.code == 413
    Hashie::Mash.new MultiJson.decode(response.body)
  else
    # invalid arguments
    raise YahooKeyPhraseApiError.new('invalid request method')
  end
end