Module: LingotekClient::API

Defined in:
lib/lingotek-client/api.rb

Constant Summary collapse

@@url =
nil
@@key =
nil
@@secret =
nil
@@external_id =
nil
@@validate =
nil

Class Method Summary collapse

Class Method Details

.connect(url, key, secret, external_id, validate = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lingotek-client/api.rb', line 17

def connect(url, key, secret, external_id, validate = false)
  @@url = URI.parse(url)
  @@key = key
  @@secret = secret
  @@external_id = external_id
  @@validate = validate
  generate_token
  RestClient.reset_before_execution_procs
  RestClient.add_before_execution_proc do |req, params|
    @@access_token.sign! req
  end
end

.generate_tokenObject



46
47
48
49
# File 'lib/lingotek-client/api.rb', line 46

def generate_token
  consumer = OAuth::Consumer.new(@@key, @@secret, :site => site, :request_token_path => "", :authorize_path => "", :access_token_path => "", :http_method => :post)
  @@access_token = OAuth::AccessToken.new(consumer)
end

.method_missing(method, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lingotek-client/api.rb', line 31

def method_missing(method, *args, &block)
  inputs = args.first || {}
  if args[1].nil?
    validate = @@validate
  else
    validate = args[1] 
  end

  LingotekClient::Schema.validate!(method, inputs.keys ) if validate
  # retrive the file params
  file_upload_params = inputs.select { |k,v|  v.is_a? File }
  inputs.delete_if { |k,v|  v.is_a? File }
  post(method, inputs, file_upload_params)
end

.post(method, params = {}, file_upload_params = {}) ⇒ Object



52
53
54
55
# File 'lib/lingotek-client/api.rb', line 52

def post(method, params = {}, file_upload_params = {})
  params[:externalId] ||= @external_id
  RestClient.post url_path(method, params), file_upload_params
end

.siteObject



77
78
79
# File 'lib/lingotek-client/api.rb', line 77

def site
  "#{@@url.scheme}://#{@@url.host}:#{@@url.port}"
end

.uri_encode_www_form(params) ⇒ Object



61
62
63
64
65
# File 'lib/lingotek-client/api.rb', line 61

def uri_encode_www_form(params)
  query = URI.encode_www_form(params.select { |k,v| (!v.is_a?(Array)) } )
  query << '&' unless query.empty?
  query << uri_encode_www_form_array(params.select { |k,v| v.is_a? Array } )
end

.uri_encode_www_form_array(params) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/lingotek-client/api.rb', line 67

def uri_encode_www_form_array(params)
  str = ""
  params.each do |k,v|
    v.each do |single_value|
      str << "#{k}=#{single_value}&"
    end
  end
  str.gsub(/\&$/, '')
end

.url_path(method, params) ⇒ Object



57
58
59
# File 'lib/lingotek-client/api.rb', line 57

def url_path(method, params)
  "#{site}#{@@url.path}#{method}?#{uri_encode_www_form(params)}"
end