Class: CloudNaturalLanguage::API

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

Constant Summary collapse

HOST =
'language.googleapis.com'
PORT =
443
API_BASE_PATH =
'/v1beta1/documents:'
ANALYZE_ENTITIES_PATH =
API_BASE_PATH + 'analyzeEntities'
ANALYZE_SENTIMENT_PATH =
API_BASE_PATH + 'analyzeSentiment'
ANNOTATE_TEXT_PATH =
API_BASE_PATH + 'annotateText'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ API

Returns a new instance of API.



16
17
18
# File 'lib/cloud_natural_language/api.rb', line 16

def initialize(api_key)
  self.api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/cloud_natural_language/api.rb', line 15

def api_key
  @api_key
end

Instance Method Details

#analyze_entities(content, lang = 'EN') ⇒ Object



30
31
32
33
34
# File 'lib/cloud_natural_language/api.rb', line 30

def analyze_entities(content, lang = 'EN')
  uri = build_uri(ANALYZE_ENTITIES_PATH)
  body = document(content, lang).merge(encodingType: 'UTF8').to_json
  post(uri, body).body
end

#analyze_sentiment(content, lang = 'EN') ⇒ Object



36
37
38
39
# File 'lib/cloud_natural_language/api.rb', line 36

def analyze_sentiment(content, lang = 'EN')
  uri = build_uri(ANALYZE_SENTIMENT_PATH)
  post(uri, document(content, lang).to_json).body
end

#annotate_text(content, lang = 'EN', opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/cloud_natural_language/api.rb', line 41

def annotate_text(content, lang = 'EN', opts = {})
  uri = build_uri(ANNOTATE_TEXT_PATH)
  body = document(content, lang)
    .merge(features(opts))
    .merge(encodingType: 'UTF8')
    .to_json
  post(uri, body).body
end

#post(uri, body) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/cloud_natural_language/api.rb', line 20

def post(uri, body)
  req = Net::HTTP::Post.new(uri.request_uri)
  req['Content-Type'] = 'application/json'
  req.body = body

  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.request(req)
end