Class: Gcloud::Translate::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/translate/service.rb

Overview

Represents the service to Translate, exposing the API calls.

Constant Summary collapse

API =

Alias to the Google Client API module

Google::Apis::TranslateV2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, retries: nil, timeout: nil) ⇒ Service

Creates a new Service instance.



34
35
36
37
38
39
40
41
42
# File 'lib/gcloud/translate/service.rb', line 34

def initialize key, retries: nil, timeout: nil
  @service = API::TranslateService.new
  @service.client_options.application_name    = "gcloud-ruby"
  @service.client_options.application_version = Gcloud::VERSION
  @service.request_options.retries = retries || 3
  @service.request_options.timeout_sec = timeout if timeout
  @service.authorization = nil
  @service.key = key
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



30
31
32
# File 'lib/gcloud/translate/service.rb', line 30

def credentials
  @credentials
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



48
49
50
# File 'lib/gcloud/translate/service.rb', line 48

def mocked_service
  @mocked_service
end

Instance Method Details

#detect(text) ⇒ Object

Returns API::ListDetectionsResponse



61
62
63
64
65
# File 'lib/gcloud/translate/service.rb', line 61

def detect text
  service.list_detections Array(text)
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#inspectObject



75
76
77
# File 'lib/gcloud/translate/service.rb', line 75

def inspect
  "#{self.class}"
end

#languages(language = nil) ⇒ Object

Returns API::ListLanguagesResponse



69
70
71
72
73
# File 'lib/gcloud/translate/service.rb', line 69

def languages language = nil
  service.list_languages target: language
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#serviceObject



44
45
46
47
# File 'lib/gcloud/translate/service.rb', line 44

def service
  return mocked_service if mocked_service
  @service
end

#translate(text, to: nil, from: nil, format: nil, cid: nil) ⇒ Object

Returns API::ListTranslationsResponse



52
53
54
55
56
57
# File 'lib/gcloud/translate/service.rb', line 52

def translate text, to: nil, from: nil, format: nil, cid: nil
  service.list_translations Array(text), to, cid: cid, format: format,
                                             source: from
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end