Class: LanguageLayer::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/language_detection.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_key) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
# File 'lib/language_detection.rb', line 27

def initialize(access_key)

  if access_key.nil?
    raise LanguageLayer::MissingArgumentException.new 'access_key'
  end

  @access_key = access_key

end

Instance Method Details

#batch(query, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/language_detection.rb', line 81

def batch(query, options = {})

  if query.nil?
    raise LanguageLayer::MissingArgumentException.new 'query'
    return
  end

  # Create a shallow copy so we don't manipulate the original reference
  q = options.dup

  # Populate the Query
  q.access_key = @access_key
  q.query = query

  # We then create the Request
  req = LanguageLayer::BatchRequest.new(q)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.post('/batch', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    if (res[LanguageLayer::BatchResponse::ERROR_EXPR])
      raise LanguageLayer::BatchException.new res[LanguageLayer::BatchResponse::ERROR_EXPR]
    end

    # We just return the parsed binary response
    return res.parsed_response

  rescue => e
    puts e.inspect
    return

  end
end

#detect(query, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/language_detection.rb', line 38

def detect(query, options = {})

  if query.nil?
    raise LanguageLayer::MissingArgumentException.new 'query'
    return
  end

  # Create a shallow copy so we don't manipulate the original reference
  q = options.dup

  # Populate the Query
  q.access_key = @access_key
  q.query = URI.escape(query)

  # We then create the Request
  req = LanguageLayer::DetectRequest.new(q)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.get('/detect', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    if (!res[LanguageLayer::DetectResponse::SUCCESS_EXPR])
      raise LanguageLayer::DetectException.new res[LanguageLayer::DetectResponse::ERROR_EXPR]
    end

    # We just return the parsed binary response
    return res.parsed_response

  rescue => e
    puts e.inspect
    return

  end
end

#languages(options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/language_detection.rb', line 124

def languages(options = {})

  # Create a shallow copy so we don't manipulate the original reference
  q = options.dup

  # Populate the Query
  q.access_key = @access_key

  # We then create the Request
  req = LanguageLayer::LanguagesRequest.new(q)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.get('/languages', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    if (res[LanguageLayer::LanguagesResponse::ERROR_EXPR])
      raise LanguageLayer::LanguagesException.new res[LanguageLayer::LanguagesResponse::ERROR_EXPR]
    end

    # We just return the parsed binary response
    return res.parsed_response

  rescue => e
    puts e.inspect
    return

  end
end