translationApiClient 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ module TranslationApiClient
2
+ #
3
+ class TranslationResponse < BaseObject
4
+ attr_accessor :warning, :error, :request_id, :outputs
5
+ # attribute mapping from ruby-style variable name to JSON key
6
+ def self.attribute_map
7
+ {
8
+
9
+ # Warning at request level
10
+ :'warning' => :'warning',
11
+
12
+ # Error at request level
13
+ :'error' => :'error',
14
+
15
+ # Request identifier to use to get the status, the result of the request and to cancel it in asynchronous mode
16
+ :'request_id' => :'requestId',
17
+
18
+ # Outputs of translation
19
+ :'outputs' => :'outputs'
20
+
21
+ }
22
+ end
23
+
24
+ # attribute type
25
+ def self.swagger_types
26
+ {
27
+ :'warning' => :'string',
28
+ :'error' => :'string',
29
+ :'request_id' => :'string',
30
+ :'outputs' => :'array[TranslationOutput]'
31
+
32
+ }
33
+ end
34
+
35
+ def initialize(attributes = {})
36
+ return if !attributes.is_a?(Hash) || attributes.empty?
37
+
38
+ # convert string to symbol for hash key
39
+ attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
40
+
41
+
42
+ if attributes[:'warning']
43
+ @warning = attributes[:'warning']
44
+ end
45
+
46
+ if attributes[:'error']
47
+ @error = attributes[:'error']
48
+ end
49
+
50
+ if attributes[:'requestId']
51
+ @request_id = attributes[:'requestId']
52
+ end
53
+
54
+ if attributes[:'outputs']
55
+ if (value = attributes[:'outputs']).is_a?(Array)
56
+ @outputs = value
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,100 @@
1
+ module TranslationApiClient
2
+ #
3
+ class TranslationStatus < BaseObject
4
+ attr_accessor :error, :cancelled, :created_at, :description, :expire_at, :finished_at, :finished_steps, :status, :total_steps
5
+ # attribute mapping from ruby-style variable name to JSON key
6
+ def self.attribute_map
7
+ {
8
+
9
+ # Error of the request
10
+ :'error' => :'error',
11
+
12
+ # Is the request cancelled
13
+ :'cancelled' => :'cancelled',
14
+
15
+ # Creation time of the request (ms since the Epoch)
16
+ :'created_at' => :'createdAt',
17
+
18
+ # Description
19
+ :'description' => :'description',
20
+
21
+ # Expiration time of the request (ms since the Epoch)
22
+ :'expire_at' => :'expireAt',
23
+
24
+ # Completion time of the request (ms since the Epoch)
25
+ :'finished_at' => :'finishedAt',
26
+
27
+ # Number of finished steps
28
+ :'finished_steps' => :'finishedSteps',
29
+
30
+ # Status of the request
31
+ :'status' => :'status',
32
+
33
+ # Number of steps to complete
34
+ :'total_steps' => :'totalSteps'
35
+
36
+ }
37
+ end
38
+
39
+ # attribute type
40
+ def self.swagger_types
41
+ {
42
+ :'error' => :'string',
43
+ :'cancelled' => :'boolean',
44
+ :'created_at' => :'number',
45
+ :'description' => :'string',
46
+ :'expire_at' => :'number',
47
+ :'finished_at' => :'number',
48
+ :'finished_steps' => :'int',
49
+ :'status' => :'string',
50
+ :'total_steps' => :'int'
51
+
52
+ }
53
+ end
54
+
55
+ def initialize(attributes = {})
56
+ return if !attributes.is_a?(Hash) || attributes.empty?
57
+
58
+ # convert string to symbol for hash key
59
+ attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
60
+
61
+
62
+ if attributes[:'error']
63
+ @error = attributes[:'error']
64
+ end
65
+
66
+ if attributes[:'cancelled']
67
+ @cancelled = attributes[:'cancelled']
68
+ end
69
+
70
+ if attributes[:'createdAt']
71
+ @created_at = attributes[:'createdAt']
72
+ end
73
+
74
+ if attributes[:'description']
75
+ @description = attributes[:'description']
76
+ end
77
+
78
+ if attributes[:'expireAt']
79
+ @expire_at = attributes[:'expireAt']
80
+ end
81
+
82
+ if attributes[:'finishedAt']
83
+ @finished_at = attributes[:'finishedAt']
84
+ end
85
+
86
+ if attributes[:'finishedSteps']
87
+ @finished_steps = attributes[:'finishedSteps']
88
+ end
89
+
90
+ if attributes[:'status']
91
+ @status = attributes[:'status']
92
+ end
93
+
94
+ if attributes[:'totalSteps']
95
+ @total_steps = attributes[:'totalSteps']
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,90 @@
1
+ # module Swagger
2
+ class Object
3
+
4
+ unless Object.method_defined? :blank?
5
+ def blank?
6
+ respond_to?(:empty?) ? empty? : !self
7
+ end
8
+ end
9
+
10
+ unless Object.method_defined? :present?
11
+ def present?
12
+ !blank?
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ class String
19
+
20
+ unless String.method_defined? :underscore
21
+ def underscore
22
+ self.gsub(/::/, '/').
23
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
24
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
25
+ tr("-", "_").
26
+ downcase
27
+ end
28
+ end
29
+
30
+ unless String.method_defined? :camelize
31
+ def camelize(first_letter_in_uppercase = true)
32
+ if first_letter_in_uppercase != :lower
33
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
34
+ else
35
+ self.to_s[0].chr.downcase + camelize(self)[1..-1]
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ class Hash
43
+
44
+ unless Hash.method_defined? :stringify_keys
45
+ def stringify_keys
46
+ inject({}) do |options, (key, value)|
47
+ options[key.to_s] = value
48
+ options
49
+ end
50
+ end
51
+ end
52
+
53
+ unless Hash.method_defined? :stringify_keys!
54
+ def stringify_keys!
55
+ self.replace(self.stringify_keys)
56
+ end
57
+ end
58
+
59
+ unless Hash.method_defined? :symbolize_keys
60
+ def symbolize_keys
61
+ inject({}) do |options, (key, value)|
62
+ options[(key.to_sym rescue key) || key] = value
63
+ options
64
+ end
65
+ end
66
+ end
67
+
68
+ unless Hash.method_defined? :symbolize_keys!
69
+ def symbolize_keys!
70
+ self.replace(self.symbolize_keys)
71
+ end
72
+ end
73
+
74
+ unless Hash.method_defined? :symbolize_and_underscore_keys
75
+ def symbolize_and_underscore_keys
76
+ inject({}) do |options, (key, value)|
77
+ options[(key.to_s.underscore.to_sym rescue key) || key] = value
78
+ options
79
+ end
80
+ end
81
+ end
82
+
83
+ unless Hash.method_defined? :symbolize_and_underscore_keys!
84
+ def symbolize_and_underscore_keys!
85
+ self.replace(self.symbolize_and_underscore_keys)
86
+ end
87
+ end
88
+
89
+ end
90
+ # end
@@ -0,0 +1,78 @@
1
+ require 'logger'
2
+ require 'json'
3
+
4
+ module TranslationApiClient
5
+ module Swagger
6
+ class << self
7
+ attr_accessor :logger
8
+
9
+ # A Swagger configuration object. Must act like a hash and return sensible
10
+ # values for all Swagger configuration options. See Swagger::Configuration.
11
+ attr_accessor :configuration
12
+
13
+ attr_accessor :resources
14
+
15
+ # Call this method to modify defaults in your initializers.
16
+ #
17
+ # @example
18
+ # Swagger.configure do |config|
19
+ # config.api_key['api_key'] = '1234567890abcdef' # api key authentication
20
+ # config.username = 'wordlover' # http basic authentication
21
+ # config.password = 'i<3words' # http basic authentication
22
+ # config.format = 'json' # optional, defaults to 'json'
23
+ # end
24
+ #
25
+ def configure
26
+ yield(configuration) if block_given?
27
+
28
+ # Configure logger. Default to use Rails
29
+ self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
30
+
31
+ # remove :// from scheme
32
+ configuration.scheme.sub!(/:\/\//, '')
33
+
34
+ # remove http(s):// and anything after a slash
35
+ configuration.host.sub!(/https?:\/\//, '')
36
+ configuration.host = configuration.host.split('/').first
37
+
38
+ # Add leading and trailing slashes to base_path
39
+ configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
40
+ configuration.base_path = "" if configuration.base_path == "/"
41
+ end
42
+
43
+ def authenticated?
44
+ Swagger.configuration.auth_token.present?
45
+ end
46
+
47
+ def de_authenticate
48
+ Swagger.configuration.auth_token = nil
49
+ end
50
+
51
+ def authenticate
52
+ return if Swagger.authenticated?
53
+
54
+ if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
55
+ raise ClientError, "Username and password are required to authenticate."
56
+ end
57
+
58
+ request = Swagger::Request.new(
59
+ :get,
60
+ "account/authenticate/{username}",
61
+ :params => {
62
+ :username => Swagger.configuration.username,
63
+ :password => Swagger.configuration.password
64
+ }
65
+ )
66
+
67
+ response_body = request.response.body
68
+ Swagger.configuration.auth_token = response_body['token']
69
+ end
70
+ end
71
+ end
72
+
73
+ class ServerError < StandardError
74
+ end
75
+
76
+ class ClientError < StandardError
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ module TranslationApiClient
2
+ module Swagger
3
+ class Configuration
4
+ attr_accessor :format, :key, :key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
5
+
6
+ # Defaults go in here..
7
+ def initialize
8
+ @format = 'json'
9
+ @scheme = 'https'
10
+ @host = 'api-platform.systran.net'
11
+ @base_path = '/'
12
+ @user_agent = "ruby-swagger-#{Swagger::VERSION}"
13
+ @inject_format = false
14
+ @force_ending_format = false
15
+ @camelize_params = true
16
+
17
+ # keys for API key authentication (param-name => api-key)
18
+ @key = {}
19
+ # api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
20
+ @key_prefix = {}
21
+
22
+ # whether to verify SSL certificate, default to true
23
+ # Note: do NOT set it to false in production code, otherwise you would
24
+ # face multiple types of cryptographic attacks
25
+ @verify_ssl = true
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,272 @@
1
+ module TranslationApiClient
2
+ module Swagger
3
+ class Request
4
+ require 'uri'
5
+ require 'addressable/uri'
6
+ require 'typhoeus'
7
+
8
+ attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
9
+
10
+ # All requests must have an HTTP method and a path
11
+ # Optionals parameters are :params, :headers, :body, :format, :host
12
+ def initialize(http_method, path, attributes={})
13
+ attributes[:format] ||= Swagger.configuration.format
14
+ attributes[:params] ||= {}
15
+
16
+ # Set default headers
17
+ default_headers = {
18
+ 'Content-Type' => "application/#{attributes[:format].downcase}",
19
+ 'User-Agent' => Swagger.configuration.user_agent
20
+ }
21
+
22
+ # Merge argument headers into defaults
23
+ attributes[:headers] = default_headers.merge(attributes[:headers] || {})
24
+
25
+ # Stick in the auth token if there is one
26
+ if Swagger.authenticated?
27
+ attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token})
28
+ end
29
+
30
+ self.http_method = http_method.to_sym
31
+ self.path = path
32
+ attributes.each do |name, value|
33
+ send("#{name.to_s.underscore.to_sym}=", value)
34
+ end
35
+
36
+ update_params_for_auth!
37
+ end
38
+
39
+ # Update hearder and query params based on authentication settings.
40
+ def update_params_for_auth!
41
+ (@auth_names || []).each do |auth_name|
42
+ case auth_name
43
+ when 'apiKey'
44
+ @params ||= {}
45
+ @params['key'] = get_key_with_prefix('key')
46
+ when 'accessToken'
47
+ @headers ||= {}
48
+ @headers['Authorization'] = get_key_with_prefix('Authorization')
49
+
50
+ end
51
+ end
52
+ end
53
+
54
+ # Get API key (with prefix if set).
55
+ # @param [String] param_name the parameter name of API key auth
56
+ def get_key_with_prefix(param_name)
57
+ Swagger.configuration.key
58
+ end
59
+
60
+ # Construct a base URL
61
+ def url(options = {})
62
+ u = Addressable::URI.new(
63
+ :scheme => Swagger.configuration.scheme,
64
+ :host => Swagger.configuration.host,
65
+ :path => self.interpreted_path,
66
+ :query => self.query_string.sub(/\?/, '')
67
+ ).to_s
68
+ puts ' url'
69
+
70
+ puts u
71
+ # Drop trailing question mark, if present
72
+ u.sub! /\?$/, ''
73
+ u
74
+ end
75
+
76
+ # Iterate over the params hash, injecting any path values into the path string
77
+ # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
78
+ def interpreted_path
79
+ p = self.path.dup
80
+
81
+ # Stick a .{format} placeholder into the path if there isn't
82
+ # one already or an actual format like json or xml
83
+ # e.g. /words/blah => /words.{format}/blah
84
+ if Swagger.configuration.inject_format
85
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
86
+ p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
87
+ end
88
+ end
89
+
90
+ # Stick a .{format} placeholder on the end of the path if there isn't
91
+ # one already or an actual format like json or xml
92
+ # e.g. /words/blah => /words/blah.{format}
93
+ if Swagger.configuration.force_ending_format
94
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
95
+ p = "#{p}.#{format}"
96
+ end
97
+ end
98
+
99
+ p = p.sub("{format}", self.format.to_s)
100
+
101
+ URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
102
+ end
103
+
104
+ # Massage the request body into a state of readiness
105
+ # If body is a hash, camelize all keys then convert to a json string
106
+ def body=(value)
107
+ if value.is_a?(Hash)
108
+ value = value.inject({}) do |memo, (k,v)|
109
+ memo[k.to_s.camelize(:lower).to_sym] = v
110
+ memo
111
+ end
112
+ end
113
+ @body = value
114
+ end
115
+
116
+ # If body is an object, JSONify it before making the actual request.
117
+ # For form parameters, remove empty value
118
+ def outgoing_body
119
+ # http form
120
+ if headers['Content-Type'] == 'application/x-www-form-urlencoded' || headers['Content-Type'] == 'multipart/form-data'
121
+ data = form_params.dup
122
+ data.each do |key, value|
123
+ data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
124
+ end
125
+ data
126
+ elsif @body # http body is JSON
127
+ @body.is_a?(String) ? @body : @body.to_json
128
+ else
129
+ nil
130
+ end
131
+ end
132
+
133
+ # Construct a query string from the query-string-type params
134
+ def query_string
135
+ # Iterate over all params,
136
+ # .. removing the ones that are part of the path itself.
137
+ # .. stringifying values so Addressable doesn't blow up.
138
+ query_values = {}
139
+ self.params.each_pair do |key, value|
140
+ next if self.path.include? "{#{key}}" # skip path params
141
+ next if value.blank? && value.class != FalseClass # skip empties
142
+ if Swagger.configuration.camelize_params
143
+ key = key.to_s.camelize(:lower).to_sym
144
+ end
145
+ query_values[key] = value.to_s
146
+ end
147
+
148
+ # We don't want to end up with '?' as our query string
149
+ # if there aren't really any params
150
+ return "" if query_values.blank?
151
+
152
+ # Addressable requires query_values to be set after initialization..
153
+ qs = Addressable::URI.new
154
+ qs.query_values = query_values
155
+ qs.to_s
156
+ end
157
+
158
+ def make
159
+ #TODO use configuration setting to determine if debugging
160
+ #logger = Logger.new STDOUT
161
+ #logger.debug self.url
162
+ if self.outgoing_body.nil? || self.outgoing_body.empty?
163
+ self.headers['Content-Type'] = 'application/json'
164
+ end
165
+ request_options = {
166
+ :ssl_verifypeer => Swagger.configuration.verify_ssl,
167
+ :headers => self.headers.stringify_keys
168
+ }
169
+ response = case self.http_method.to_sym
170
+ when :get,:GET
171
+ Typhoeus::Request.get(
172
+ self.url,
173
+ #request_options,
174
+ request_options.merge(:body => self.outgoing_body)
175
+ )
176
+ when :post,:POST
177
+ Typhoeus::Request.post(
178
+ self.url,
179
+ request_options.merge(:body => self.outgoing_body)
180
+ )
181
+
182
+ when :patch,:PATCH
183
+ Typhoeus::Request.patch(
184
+ self.url,
185
+ request_options.merge(:body => self.outgoing_body)
186
+ )
187
+
188
+ when :put,:PUT
189
+ Typhoeus::Request.put(
190
+ self.url,
191
+ request_options.merge(:body => self.outgoing_body)
192
+ )
193
+
194
+ when :delete,:DELETE
195
+ Typhoeus::Request.delete(
196
+ self.url,
197
+ request_options.merge(:body => self.outgoing_body)
198
+ )
199
+ end
200
+ Response.new(response)
201
+ end
202
+
203
+ def response
204
+ self.make
205
+ end
206
+
207
+ def response_code_pretty
208
+ return unless @response.present?
209
+ @response.code.to_s
210
+ end
211
+
212
+ def response_headers_pretty
213
+ return unless @response.present?
214
+ # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
215
+ @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
216
+ end
217
+
218
+ # return 'Accept' based on an array of accept provided
219
+ # @param [Array] header_accept_array Array fo 'Accept'
220
+ # @return String Accept (e.g. application/json)
221
+ def self.select_header_accept header_accept_array
222
+ if header_accept_array.empty?
223
+ return
224
+ elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 }
225
+ 'application/json' # look for json data by default
226
+ else
227
+ header_accept_array.join(',')
228
+ end
229
+ end
230
+
231
+ # return the content type based on an array of content-type provided
232
+ # @param [Array] content_type_array Array fo content-type
233
+ # @return String Content-Type (e.g. application/json)
234
+ def self.select_header_content_type content_type_array
235
+ if content_type_array.empty?
236
+ 'application/json' # use application/json by default
237
+
238
+ elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 }
239
+ 'application/json' # use application/json if it's included
240
+ else
241
+ content_type_array[0]; # otherwise, use the first one
242
+ end
243
+ end
244
+
245
+ # static method to convert object (array, hash, object, etc) to JSON string
246
+ # @param model object to be converted into JSON string
247
+ # @return string JSON string representation of the object
248
+ def self.object_to_http_body model
249
+ return if model.nil?
250
+ _body = nil
251
+ if model.is_a?(Array)
252
+ _body = model.map{|m| object_to_hash(m) }
253
+ else
254
+ _body = object_to_hash(model)
255
+ end
256
+ _body.to_json
257
+ end
258
+
259
+ # static method to convert object(non-array) to hash
260
+ # @param obj object to be converted into JSON string
261
+ # @return string JSON string representation of the object
262
+ def self.object_to_hash obj
263
+ if obj.respond_to?(:to_hash)
264
+ obj.to_hash
265
+ else
266
+ obj
267
+ end
268
+ end
269
+
270
+ end
271
+ end
272
+ end