Class: Google::Cloud::Language::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/language/service.rb

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials, host: nil, client_config: nil, timeout: nil) ⇒ Service

Creates a new Service instance.



33
34
35
36
37
38
39
40
# File 'lib/google/cloud/language/service.rb', line 33

def initialize project, credentials, host: nil, client_config: nil,
               timeout: nil
  @project = project
  @credentials = credentials
  @host = host || V1beta1::LanguageServiceApi::SERVICE_ADDRESS
  @client_config = client_config || {}
  @timeout = timeout
end

Instance Attribute Details

#client_configObject

Returns the value of attribute client_config.



29
30
31
# File 'lib/google/cloud/language/service.rb', line 29

def client_config
  @client_config
end

#credentialsObject

Returns the value of attribute credentials.



29
30
31
# File 'lib/google/cloud/language/service.rb', line 29

def credentials
  @credentials
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/google/cloud/language/service.rb', line 29

def host
  @host
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



64
65
66
# File 'lib/google/cloud/language/service.rb', line 64

def mocked_service
  @mocked_service
end

#projectObject

Returns the value of attribute project.



29
30
31
# File 'lib/google/cloud/language/service.rb', line 29

def project
  @project
end

#timeoutObject

Returns the value of attribute timeout.



29
30
31
# File 'lib/google/cloud/language/service.rb', line 29

def timeout
  @timeout
end

Instance Method Details

#annotate(doc_grpc, syntax: false, entities: false, sentiment: false, encoding: nil) ⇒ Object

Returns API::BatchAnnotateImagesResponse



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/google/cloud/language/service.rb', line 72

def annotate doc_grpc, syntax: false, entities: false, sentiment: false,
             encoding: nil
  if syntax == false && entities == false && sentiment == false
    syntax = true
    entities = true
    sentiment = true
  end
  features = V1beta1::AnnotateTextRequest::Features.new(
    extract_syntax: syntax, extract_entities: entities,
    extract_document_sentiment: sentiment)
  encoding = verify_encoding! encoding
  execute do
    service.annotate_text doc_grpc, features, encoding,
                          options: default_options
  end
end

#chan_credsObject



47
48
49
50
51
52
# File 'lib/google/cloud/language/service.rb', line 47

def chan_creds
  return credentials if insecure?
  require "grpc"
  GRPC::Core::ChannelCredentials.new.compose \
    GRPC::Core::CallCredentials.new credentials.client.updater_proc
end

#channelObject



42
43
44
45
# File 'lib/google/cloud/language/service.rb', line 42

def channel
  require "grpc"
  GRPC::Core::Channel.new host, nil, chan_creds
end

#entities(doc_grpc, encoding: nil) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/google/cloud/language/service.rb', line 89

def entities doc_grpc, encoding: nil
  encoding = verify_encoding! encoding
  execute do
    service.analyze_entities doc_grpc, encoding,
                             options: default_options
  end
end

#insecure?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/google/cloud/language/service.rb', line 66

def insecure?
  credentials == :this_channel_is_insecure
end

#inspectObject



103
104
105
# File 'lib/google/cloud/language/service.rb', line 103

def inspect
  "#{self.class}(#{@project})"
end

#sentiment(doc_grpc) ⇒ Object



97
98
99
100
101
# File 'lib/google/cloud/language/service.rb', line 97

def sentiment doc_grpc
  execute do
    service.analyze_sentiment doc_grpc, options: default_options
  end
end

#serviceObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/google/cloud/language/service.rb', line 54

def service
  return mocked_service if mocked_service
  @service ||= V1beta1::LanguageServiceApi.new(
    service_path: host,
    channel: channel,
    timeout: timeout,
    client_config: client_config,
    app_name: "gcloud-ruby",
    app_version: Google::Cloud::Language::VERSION)
end