lingotek-client 0.1.0 → 0.1.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,27 +1,29 @@
1
1
  require 'oauth'
2
- require 'rest-client'
2
+ require_relative 'rest-client-patch'
3
3
 
4
4
  require_relative './schema'
5
5
 
6
6
  module LingotekClient
7
7
  class Api
8
8
 
9
- def initialize(url, key, secret, external_id)
9
+ def initialize(url, key, secret, external_id, validate = false)
10
10
  @url = URI.parse(url)
11
11
  @key = key
12
12
  @secret = secret
13
13
  @external_id = external_id
14
+ @validate = validate
14
15
  generate_token
15
- RestClient.reset_before_execution_procs
16
- RestClient.add_before_execution_proc do |req, params|
17
- @access_token.sign! req
18
- end
19
16
  end
20
17
 
21
18
  def method_missing(method, *args, &block)
22
19
  inputs = args.first || {}
20
+ if args[1].nil?
21
+ validate = @validate
22
+ else
23
+ validate = args[1]
24
+ end
23
25
 
24
- LingotekClient::Schema.validate!(method, inputs.keys )
26
+ LingotekClient::Schema.validate!(method, inputs.keys ) if validate
25
27
  # retrive the file params
26
28
  file_upload_params = inputs.select { |k,v| v.is_a? File }
27
29
  inputs.delete_if { |k,v| v.is_a? File }
@@ -35,14 +37,45 @@ module LingotekClient
35
37
 
36
38
  private
37
39
 
38
-
39
40
  def post(method, params = {}, file_upload_params = {})
40
41
  params[:externalId] ||= @external_id
41
- RestClient.post(url_path(method, params), file_upload_params)
42
+ client = RestClient::Resource.new url_path(method, params)
43
+ client.add_before_execution_proc do |req, params|
44
+ @access_token.sign! req
45
+ end
46
+ client.post(file_upload_params)
42
47
  end
43
48
 
49
+
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
+
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
60
+
44
61
  def url_path(method, params)
45
- "#{site}#{@url.path}#{method}?#{URI.encode_www_form(params)}"
62
+ "#{site}#{@url.path}#{method}?#{uri_encode_www_form(params)}"
63
+ end
64
+
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
70
+
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}&"
76
+ end
77
+ end
78
+ str.gsub(/\&$/, '')
46
79
  end
47
80
 
48
81
  def site
@@ -68,6 +68,7 @@ module APISchema
68
68
  findLabel: { required: [:name, :limit], optional: []},
69
69
  findTM: { required: [:tmVaultId, :sourceLang, :targetLang], optional: [:searchText, :startDate, :endDate]},
70
70
  flushDocumentProgress: { required: [:documentId], optional: []},
71
+ getAnalysis: { required: [:group], optional: []},
71
72
  getClientCommunityPreference: { required: [:key], optional: []},
72
73
  getCMSKey: { required: [], optional: []},
73
74
  getCommunityDictionary: { required: [:type], optional: []},
@@ -0,0 +1,133 @@
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
@@ -1,5 +1,5 @@
1
1
  module LingotekClient
2
- end
2
+ end
3
3
 
4
4
  require "lingotek-client/version"
5
5
  require "lingotek-client/api"
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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.7.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.8.7
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.8.7
78
+ - !ruby/object:Gem::Dependency
79
+ name: rdoc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
62
94
  description: Client lib for lingotek apis
63
95
  email:
64
96
  - fribeiro@lingotek.com
@@ -70,9 +102,9 @@ extra_rdoc_files:
70
102
  files:
71
103
  - lib/lingotek-client/version.rb
72
104
  - lib/lingotek-client/api.rb
105
+ - lib/lingotek-client/rest-client-patch.rb
73
106
  - lib/lingotek-client/schema.rb
74
107
  - lib/lingotek-client/api_schema.rb
75
- - lib/VERSION
76
108
  - lib/lingotek-client.rb
77
109
  - update_schema/update_schema
78
110
  - update_schema/table.html
@@ -80,7 +112,7 @@ files:
80
112
  - VERSION
81
113
  - LICENSE
82
114
  - README.md
83
- homepage:
115
+ homepage: http://lingotek.com
84
116
  licenses: []
85
117
  post_install_message:
86
118
  rdoc_options:
data/lib/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0