Class: Gcloud::Translate::Connection

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

Overview

calls

Constant Summary collapse

API_VERSION =
"v2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Connection

Creates a new Connection instance.



31
32
33
34
35
36
37
# File 'lib/gcloud/translate/connection.rb', line 31

def initialize key
  @client = Google::APIClient.new application_name:    "gcloud-ruby",
                                  application_version: Gcloud::VERSION,
                                  authorization: nil
  @translate = @client.discovered_api "translate", API_VERSION
  @client.key = key # set key after discovery, helps with tests
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



27
28
29
# File 'lib/gcloud/translate/connection.rb', line 27

def client
  @client
end

Instance Method Details

#detect(text) ⇒ Object



54
55
56
57
58
59
# File 'lib/gcloud/translate/connection.rb', line 54

def detect text
  @client.execute(
    api_method: @translate.detections.list,
    parameters: { q: Array(text), prettyprint: false }
  )
end

#inspectObject



71
72
73
# File 'lib/gcloud/translate/connection.rb', line 71

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

#languages(language = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/gcloud/translate/connection.rb', line 61

def languages language = nil
  params = { target:      language,
             prettyprint: false }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @translate.languages.list,
    parameters: params
  )
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gcloud/translate/connection.rb', line 39

def translate text, to: nil, from: nil, format: nil, cid: nil
  params = { q:           Array(text),
             target:      to,
             source:      from,
             format:      format,
             cid:         cid,
             prettyprint: false
           }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @translate.translations.list,
    parameters: params
  )
end