Class: YahooContentAnalysis::Client

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/yahoo_content_analysis/client.rb

Constant Summary

Constants included from Connection

YahooContentAnalysis::Connection::ALLOWED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#add_default_options, #connection, #oauth_opts

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
# File 'lib/yahoo_content_analysis/client.rb', line 21

def initialize(options={}, &block)
  setup(options)
  yield(self) if block_given?
end

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



10
11
12
# File 'lib/yahoo_content_analysis/client.rb', line 10

def current_options
  @current_options
end

Instance Method Details

#analyze(content, opts = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/yahoo_content_analysis/client.rb', line 35

def analyze(content, opts={})
  content = content.to_s.squish
  response = connection.post do |request|
    request.body = options(content)
  end
  YahooContentAnalysis::Response.new(response)
end

#condition(content) ⇒ Object



47
48
49
# File 'lib/yahoo_content_analysis/client.rb', line 47

def condition(content)
  uri?(content) ? url(content) : text(content)
end

#options(content) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/yahoo_content_analysis/client.rb', line 59

def options(content)
  {
    'q'                => query(content),
    'format'           => 'json',
    'max'              => '50',
    'related_entities' => 'true',
    'show_metadata'    => 'true'
  }
end

#query(content) ⇒ Object



43
44
45
# File 'lib/yahoo_content_analysis/client.rb', line 43

def query(content)
  "SELECT * FROM contentanalysis.analyze WHERE related_entities = 'true' and #{condition(content)}"
end

#setup(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/yahoo_content_analysis/client.rb', line 26

def setup(options={})
  opts = Hash[options.map{ |k, v| [k.to_sym, v] }]
  opts = YahooContentAnalysis.options.merge(opts)
  self.current_options = opts
  Configuration.keys.each do |key|
    send("#{key}=", opts[key])
  end
end

#text(content) ⇒ Object



55
56
57
# File 'lib/yahoo_content_analysis/client.rb', line 55

def text(content)
  %{ text = '#{content.gsub("'", %q(\\\'))}' }
end

#uri?(string) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/yahoo_content_analysis/client.rb', line 69

def uri?(string)
  uri = URI.parse(string)
  %w( http https ).include?(uri.scheme)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end

#url(content) ⇒ Object



51
52
53
# File 'lib/yahoo_content_analysis/client.rb', line 51

def url(content)
  %{ url = '#{content}' }
end