lingotek-client 0.1.2 → 0.2.0

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.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.0
2
+ * Major changes
3
+ * adding resources to apis
4
+ * change api class to be static
1
5
  == 0.1.2
2
6
  * bug fix
3
7
  * remove rake dependency
data/README.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ == Lingotek Client Sample
2
+ require 'lingotek-client'
3
+ include LingotekClient
4
+ include LingotekClient::Resource
5
+
6
+ API::connect('http://10.0.11.112:8080/lingopoint/api/4/', 'mykey', 'mysecret', 'myuser')
7
+
8
+ Project.all.each do |project|
9
+ puts project.name
10
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -1,5 +1,11 @@
1
1
  module LingotekClient
2
2
  end
3
3
 
4
- require "lingotek-client/version"
5
- require "lingotek-client/api"
4
+ require 'lingotek-client/version'
5
+ require 'lingotek-client/api'
6
+
7
+ require 'lingotek-client/resource/base'
8
+ require 'lingotek-client/resource/project'
9
+ require 'lingotek-client/resource/document'
10
+ require 'lingotek-client/resource/workflow'
11
+ require 'lingotek-client/resource/translation_target'
@@ -1,91 +1,83 @@
1
1
  require 'oauth'
2
- require_relative 'rest-client-patch'
2
+ require 'rest-client'
3
+
3
4
 
4
5
  require_relative './schema'
5
6
 
6
7
  module LingotekClient
7
- class Api
8
-
9
- def initialize(url, key, secret, external_id, validate = false)
10
- @url = URI.parse(url)
11
- @key = key
12
- @secret = secret
13
- @external_id = external_id
14
- @validate = validate
15
- generate_token
16
- end
17
-
18
- def method_missing(method, *args, &block)
19
- inputs = args.first || {}
20
- if args[1].nil?
21
- validate = @validate
22
- else
23
- validate = args[1]
8
+ module API
9
+
10
+ @@url = nil
11
+ @@key = nil
12
+ @@secret = nil
13
+ @@external_id = nil
14
+ @@validate = nil
15
+
16
+ class << self
17
+ def connect(url, key, secret, external_id, validate = false)
18
+ @@url = URI.parse(url)
19
+ @@key = key
20
+ @@secret = secret
21
+ @@external_id = external_id
22
+ @@validate = validate
23
+ generate_token
24
+ RestClient.reset_before_execution_procs
25
+ RestClient.add_before_execution_proc do |req, params|
26
+ @@access_token.sign! req
27
+ end
24
28
  end
25
29
 
26
- LingotekClient::Schema.validate!(method, inputs.keys ) if validate
27
- # retrive the file params
28
- file_upload_params = inputs.select { |k,v| v.is_a? File }
29
- inputs.delete_if { |k,v| v.is_a? File }
30
- post(method, inputs, file_upload_params)
31
- end
32
-
33
- def generate_token
34
- consumer = OAuth::Consumer.new(@key, @secret, site: site, :request_token_path => "", :authorize_path => "", :access_token_path => "", :http_method => :post)
35
- @access_token = OAuth::AccessToken.new(consumer)
36
- end
37
30
 
38
- private
31
+ def method_missing(method, *args, &block)
32
+ inputs = args.first || {}
33
+ if args[1].nil?
34
+ validate = @@validate
35
+ else
36
+ validate = args[1]
37
+ end
39
38
 
40
- def post(method, params = {}, file_upload_params = {})
41
- params[:externalId] ||= @external_id
42
- client = RestClient::Resource.new url_path(method, params)
43
- client.add_before_execution_proc do |req, params|
44
- @access_token.sign! req
39
+ LingotekClient::Schema.validate!(method, inputs.keys ) if validate
40
+ # retrive the file params
41
+ file_upload_params = inputs.select { |k,v| v.is_a? File }
42
+ inputs.delete_if { |k,v| v.is_a? File }
43
+ post(method, inputs, file_upload_params)
45
44
  end
46
- client.post(file_upload_params)
47
- end
48
45
 
46
+ def generate_token
47
+ consumer = OAuth::Consumer.new(@@key, @@secret, :site => site, :request_token_path => "", :authorize_path => "", :access_token_path => "", :http_method => :post)
48
+ @@access_token = OAuth::AccessToken.new(consumer)
49
+ end
49
50
 
50
- # def post(method, params = {}, file_upload_params = {})
51
- # params[:externalId] ||= @external_id
52
- # client = RestClient::Resource.new url_path(method, params)
53
- # client.post(file_upload_params)
54
- # end
55
51
 
56
- # def post(method, params = {}, file_upload_params = {})
57
- # params[:externalId] ||= @external_id
58
- # RestClient.post(url_path(method, params), file_upload_params)
59
- # end
52
+ def post(method, params = {}, file_upload_params = {})
53
+ params[:externalId] ||= @external_id
54
+ RestClient.post url_path(method, params), file_upload_params
55
+ end
60
56
 
61
- def url_path(method, params)
62
- "#{site}#{@url.path}#{method}?#{uri_encode_www_form(params)}"
63
- end
57
+ def url_path(method, params)
58
+ "#{site}#{@@url.path}#{method}?#{uri_encode_www_form(params)}"
59
+ end
64
60
 
65
- def uri_encode_www_form(params)
66
- query = URI.encode_www_form(params.select { |k,v| (!v.is_a?(Array)) } )
67
- query << '&' unless query.empty?
68
- query << uri_encode_www_form_array(params.select { |k,v| v.is_a? Array } )
69
- end
61
+ def uri_encode_www_form(params)
62
+ query = URI.encode_www_form(params.select { |k,v| (!v.is_a?(Array)) } )
63
+ query << '&' unless query.empty?
64
+ query << uri_encode_www_form_array(params.select { |k,v| v.is_a? Array } )
65
+ end
70
66
 
71
- def uri_encode_www_form_array(params)
72
- str = ""
73
- params.each do |k,v|
74
- v.each do |single_value|
75
- str << "#{k}=#{single_value}&"
67
+ def uri_encode_www_form_array(params)
68
+ str = ""
69
+ params.each do |k,v|
70
+ v.each do |single_value|
71
+ str << "#{k}=#{single_value}&"
72
+ end
76
73
  end
74
+ str.gsub(/\&$/, '')
77
75
  end
78
- str.gsub(/\&$/, '')
79
- end
80
76
 
81
- def site
82
- "#{@url.scheme}://#{@url.host}:#{@url.port}"
77
+ def site
78
+ "#{@@url.scheme}://#{@@url.host}:#{@@url.port}"
79
+ end
83
80
  end
84
-
85
81
  end
86
82
  end
87
83
 
88
- # client = LingotekClient::Api.new('http://10.0.11.152:8080/lingopoint/api/4/', 'test', 'test123', 'shane@lingotek.com')
89
- # puts client.listLanguageSkills
90
- # puts client.listAssignedProjects
91
-
@@ -0,0 +1,33 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class Base
4
+ attr_accessor :data
5
+
6
+ ERRORS = 'errors'
7
+ RESULTS = 'results'
8
+ FAIL = 'fail'
9
+
10
+ def initialize(attrs)
11
+ @data = attrs
12
+ end
13
+
14
+ def self.all
15
+ action_name = "#{key}s"
16
+ objects = JSON.parse(LingotekClient::API::send("list#{action_name}"))[action_name.downcase]
17
+ objects.map { |obj| self.new obj }
18
+ end
19
+
20
+ def self.find(id)
21
+ action_name = key
22
+ object = self.new(JSON.parse(Lingotek::API::api.send("get#{action_name}", "#{action_name.downcase}Id" => id)))
23
+ raise Exception.new "#{Key} not found" if object.id == nil
24
+ object
25
+ end
26
+
27
+ def self.key
28
+ "#{self.name.split("::").last}"
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class Document < Base
4
+ attr_accessor :name, :description, :source_language, :tm_vault_id, :percent_complete, :source_character_count, :id , :source_word_count, :source_sentence_count, :unique_word_count, :unique_sentence_count, :duplicate_word_count, :duplicate_sentence_count, :format_tag_count, :translation_target_refs
5
+ def initialize(attrs)
6
+ super(attrs)
7
+ @id = attrs['id']
8
+ @name = attrs['name']
9
+ @description = attrs['description']
10
+ @source_language = attrs['sourceLanguage']
11
+ @tm_vault_id = attrs['tmVaultId']
12
+ @percent_complete = attrs['percentComplete']
13
+ @source_character_count = attrs['sourceCharacterCount']
14
+ @source_word_count = attrs['sourceWordCount'].to_i
15
+ @source_sentence_count = attrs['sourceSentenceCount'].to_i
16
+ @unique_word_count = attrs['uniqueWordCount'].to_i
17
+ @unique_sentence_count = attrs['uniqueSentenceCount'].to_i
18
+ @duplicate_word_count = attrs['duplicateWordCount'].to_i
19
+ @duplicate_sentence_count = attrs['duplicateSentenceCount'].to_i
20
+ @format_tag_count = attrs['formatTagCount'].to_i
21
+ @translation_target_refs = attrs['translationTargets']
22
+ end
23
+
24
+ def all
25
+ raise Exception.new "Method not available"
26
+ end
27
+
28
+ def translation_targets
29
+ Lingotek::API::TranslationTarget.find_by_document_id(@id)
30
+ end
31
+
32
+ def type
33
+ @name.split('.').last
34
+ end
35
+
36
+ def source_language
37
+ Language.new(@source_language)
38
+ end
39
+
40
+ def due_date
41
+ #TODO calculate the document due_date base on the lastest due date from all targets
42
+ Time.now
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class Project < Base
4
+ attr_accessor :name, :state, :company_id, :percent_complete, :document_ids, :created_at, :id, :workflow_id, :due_date
5
+
6
+ FILENAME = 'download.zip'
7
+
8
+ ACTIVE = 'Active'
9
+ COMPLETED = 'Completed'
10
+
11
+ def initialize(attrs)
12
+ super(attrs)
13
+ @id = attrs['id']
14
+ @name = attrs['name']
15
+ @state = attrs['state']
16
+ @company_id = attrs['companyId']
17
+ @percent_complete = attrs['percentComplete']
18
+ @document_ids = attrs['documents'].map { |doc| doc['id'] }
19
+ @created_at = attrs['createdDate']
20
+ @workflow_id = attrs['workflowId']
21
+ @due_date = attrs['due_date']
22
+ end
23
+
24
+ def workflow
25
+ @workflow ||= Lingotek::API::Workflow.find @workflow_id if @workflow_id && @workflow_id != 0
26
+ end
27
+
28
+ def documents
29
+ @document_ids.map { |doc_id| Lingotek::API::Document.find(doc_id) }
30
+ end
31
+
32
+ def self.download_documents(id, target)
33
+ FileUtils.mkdir_p("#{target}/#{id}")
34
+ File.open(FILENAME, 'wa') do |f|
35
+ f.write(Lingotek::API::api.downloadProjectDocuments projectId: id)
36
+ end
37
+ end
38
+
39
+ def active?
40
+ @state == ACTIVE
41
+ end
42
+
43
+ def completed?
44
+ @state == COMPLETED
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class TranslationTarget < Base
4
+ attr_accessor :id, :language, :due_date, :description, :match_sentences, :match_words, :phases
5
+
6
+ def initialize(attrs)
7
+ super(attrs)
8
+ @id = attrs['id']
9
+ @language = attrs['language']
10
+ @due_date = attrs['due_date']
11
+ @description = attrs['description']
12
+ @match_sentences= attrs['matchSentneces']
13
+ @match_words= attrs['matchWords']
14
+ @phases = attrs['phases']
15
+ end
16
+
17
+ def self.find_by_document_id(id)
18
+ objects = JSON.parse(Lingotek::API::api.listTranslationTargets( documentId: id ) )
19
+ objects = objects['translationTargets']
20
+ objects.map { |obj| self.new obj }
21
+ end
22
+
23
+ def current_phase
24
+ @phase.first if @phase
25
+ end
26
+
27
+ def country_code
28
+ @language.split('_').last.downcase
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class User < Base
4
+ attr_accessor :username
5
+ def initialize(opts = {})
6
+ @username = opts[:username]
7
+ end
8
+
9
+
10
+ def valid_credentials?(password)
11
+ User.valid_credentials?(@username, password)
12
+ end
13
+
14
+ def self.valid_credentials?(username, password)
15
+ JSON.parse(Lingotek::API::api.validateCredentials(externalId: username, password: password))['result'] == 'success'
16
+ end
17
+
18
+ def enable_login(password)
19
+ enable_login(@username, password)
20
+ end
21
+
22
+ def self.enable_login(username, password)
23
+ JSON.parse(Lingotek::API::api.allowLocalLogin(externalId: username, password: password))['result'] == 'success'
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module LingotekClient
2
+ module Resource
3
+ class Workflow < Base
4
+ attr_accessor :id, :name , :desc, :mod, :steps
5
+
6
+ def initialize(attrs)
7
+ super(attrs)
8
+ @id = attrs['id']
9
+ @name = attrs['name']
10
+ @desc = attrs['desc']
11
+ @mod = attrs['mod']
12
+ @steps = attrs['steps']
13
+ end
14
+
15
+ def self.find(id)
16
+ action_name = key
17
+ object = JSON.parse(Lingotek::API::api.send("get#{action_name}", "id" => id))
18
+ raise Exception.new "API call failed" if object[Lingotek::API::Base::RESULTS] == Lingotek::API::Base::FAIL
19
+ object = self.new object[action_name.downcase]
20
+ raise Exception.new "#{Key} not found" if object.id == nil
21
+ object
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  require 'json'
2
- require_relative './api_schema'
2
+ require 'lingotek-client/api_schema'
3
3
 
4
4
  module LingotekClient
5
5
  class Schema
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lingotek-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -82,21 +82,27 @@ executables: []
82
82
  extensions: []
83
83
  extra_rdoc_files:
84
84
  - LICENSE
85
- - README.md
85
+ - README.rdoc
86
+ - CHANGELOG.rdoc
86
87
  files:
87
- - lib/lingotek-client/version.rb
88
+ - lib/lingotek-client.rb
89
+ - lib/lingotek-client/api_schema.rb
88
90
  - lib/lingotek-client/api.rb
89
- - lib/lingotek-client/rest-client-patch.rb
91
+ - lib/lingotek-client/version.rb
92
+ - lib/lingotek-client/resource/user.rb
93
+ - lib/lingotek-client/resource/project.rb
94
+ - lib/lingotek-client/resource/document.rb
95
+ - lib/lingotek-client/resource/translation_target.rb
96
+ - lib/lingotek-client/resource/base.rb
97
+ - lib/lingotek-client/resource/workflow.rb
90
98
  - lib/lingotek-client/schema.rb
91
- - lib/lingotek-client/api_schema.rb
92
- - lib/lingotek-client.rb
93
- - update_schema/update_schema
94
99
  - update_schema/table.html
95
100
  - update_schema/test
101
+ - update_schema/update_schema
96
102
  - VERSION
97
- - CHANGELOG.rdoc
98
103
  - LICENSE
99
- - README.md
104
+ - README.rdoc
105
+ - CHANGELOG.rdoc
100
106
  homepage: http://lingotek.com
101
107
  licenses: []
102
108
  post_install_message:
@@ -123,4 +129,3 @@ signing_key:
123
129
  specification_version: 3
124
130
  summary: Lib that allows to connect with lingotek apis
125
131
  test_files: []
126
- has_rdoc:
data/README.md DELETED
File without changes
@@ -1,133 +0,0 @@
1
- require 'rest-client'
2
-
3
- module RestClient
4
- class Resource
5
- attr_reader :before_execution_procs
6
-
7
- def initialize(url, options={}, backwards_compatibility=nil, &block)
8
- @url = url
9
- @block = block
10
- if options.class == Hash
11
- @options = options
12
- else # compatibility with previous versions
13
- @options = { :user => options, :password => backwards_compatibility }
14
- end
15
- @before_execution_procs = []
16
- end
17
-
18
- def add_before_execution_proc( & block )
19
- @before_execution_procs << block
20
- end
21
-
22
- def reset_before_execution_procs
23
- @before_execution_procs = []
24
- end
25
-
26
- def post(payload, additional_headers={}, &block)
27
- headers = (options[:headers] || {}).merge(additional_headers)
28
- args = options.merge(
29
- :method => :post,
30
- :url => url,
31
- :payload => payload,
32
- :headers => headers)
33
- request = Request.new(args)
34
- @before_execution_procs.each do |before_proc|
35
- request.add_before_execution_proc &before_proc
36
- end
37
- request.execute( &(block || @block) )
38
- end
39
-
40
- end
41
-
42
- class Request
43
- attr_reader :before_execution_procs
44
-
45
- def initialize args
46
- @method = args[:method] or raise ArgumentError, "must pass :method"
47
- @headers = args[:headers] || {}
48
- if args[:url]
49
- @url = process_url_params(args[:url], headers)
50
- else
51
- raise ArgumentError, "must pass :url"
52
- end
53
- @cookies = @headers.delete(:cookies) || args[:cookies] || {}
54
- @payload = Payload.generate(args[:payload])
55
- @user = args[:user]
56
- @password = args[:password]
57
- @timeout = args[:timeout]
58
- @open_timeout = args[:open_timeout]
59
- @block_response = args[:block_response]
60
- @raw_response = args[:raw_response] || false
61
- @verify_ssl = args[:verify_ssl] || false
62
- @ssl_client_cert = args[:ssl_client_cert] || nil
63
- @ssl_client_key = args[:ssl_client_key] || nil
64
- @ssl_ca_file = args[:ssl_ca_file] || nil
65
- @tf = nil # If you are a raw request, this is your tempfile
66
- @max_redirects = args[:max_redirects] || 10
67
- @processed_headers = make_headers headers
68
- @args = args
69
- @before_execution_procs = []
70
- end
71
-
72
- def add_before_execution_proc( & block )
73
- @before_execution_procs << block
74
- end
75
-
76
- def reset_before_execution_procs
77
- @before_execution_procs = []
78
- end
79
-
80
- def transmit uri, req, payload, & block
81
- setup_credentials req
82
-
83
- net = net_http_class.new(uri.host, uri.port)
84
- net.use_ssl = uri.is_a?(URI::HTTPS)
85
- if (@verify_ssl == false) || (@verify_ssl == OpenSSL::SSL::VERIFY_NONE)
86
- net.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
- elsif @verify_ssl.is_a? Integer
88
- net.verify_mode = @verify_ssl
89
- net.verify_callback = lambda do |preverify_ok, ssl_context|
90
- if (!preverify_ok) || ssl_context.error != 0
91
- err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
92
- raise SSLCertificateNotVerified.new(err_msg)
93
- end
94
- true
95
- end
96
- end
97
- net.cert = @ssl_client_cert if @ssl_client_cert
98
- net.key = @ssl_client_key if @ssl_client_key
99
- net.ca_file = @ssl_ca_file if @ssl_ca_file
100
- net.read_timeout = @timeout if @timeout
101
- net.open_timeout = @open_timeout if @open_timeout
102
-
103
- # disable the timeout if the timeout value is -1
104
- net.read_timeout = nil if @timeout == -1
105
- net.out_timeout = nil if @open_timeout == -1
106
-
107
- RestClient.before_execution_procs.each do |before_proc|
108
- before_proc.call(req, args)
109
- end
110
-
111
- @before_execution_procs.each do |before_proc|
112
- before_proc.call(req, args)
113
- end
114
-
115
- log_request
116
-
117
- net.start do |http|
118
- if @block_response
119
- http.request(req, payload ? payload.to_s : nil, & @block_response)
120
- else
121
- res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
122
- log_response res
123
- process_result res, & block
124
- end
125
- end
126
- rescue EOFError
127
- raise RestClient::ServerBrokeConnection
128
- rescue Timeout::Error
129
- raise RestClient::RequestTimeout
130
- end
131
- end
132
-
133
- end