Class: Lingohub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lingohub/client.rb

Overview

A Ruby class to call the lingohub REST API. You might use this if you want to manage your lingohub apps from within a Ruby program, such as Capistrano.

Example:

require 'lingohub'
lingohub = Lingohub::Client.new('[email protected]', 'mypass')
lingohub.create('myapp')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
# File 'lib/lingohub/client.rb', line 35

def initialize(options)
  @user       = options[:username]
  @password   = options[:password]
  @auth_token = options[:auth_token]
  @host       = options[:host] || 'api.lingohub.com'
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



28
29
30
# File 'lib/lingohub/client.rb', line 28

def host
  @host
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/lingohub/client.rb', line 28

def password
  @password
end

#userObject

Returns the value of attribute user.



28
29
30
# File 'lib/lingohub/client.rb', line 28

def user
  @user
end

Class Method Details

.auth(options) ⇒ Object



30
31
32
33
# File 'lib/lingohub/client.rb', line 30

def self.auth(options)
  client = new(options)
  OkJson.decode client.post('/sessions', {}, :accept => 'json').to_s
end

.gem_version_stringObject



24
25
26
# File 'lib/lingohub/client.rb', line 24

def self.gem_version_string
  "lingohub-gem/#{version}"
end

.versionObject



20
21
22
# File 'lib/lingohub/client.rb', line 20

def self.version
  Lingohub::VERSION
end

Instance Method Details

#api_uri_partObject



99
100
101
# File 'lib/lingohub/client.rb', line 99

def api_uri_part
  "#{Lingohub::API_VERSION}"
end

#credentialsObject



42
43
44
# File 'lib/lingohub/client.rb', line 42

def credentials
  @auth_token.nil? ? {:username => @user, :password => @password} : {:username => @auth_token, :password => ""}
end

#delete(uri, extra_headers = { }) ⇒ Object

:nodoc:



68
69
70
# File 'lib/lingohub/client.rb', line 68

def delete(uri, extra_headers={ }) # :nodoc:
  process(:delete, uri, extra_headers)
end

#get(uri, extra_headers = { }) ⇒ Object

:nodoc:



56
57
58
# File 'lib/lingohub/client.rb', line 56

def get(uri, extra_headers={ }) # :nodoc:
  process(:get, uri, extra_headers)
end

#lingohub_headersObject

:nodoc:



103
104
105
106
107
108
109
110
111
112
# File 'lib/lingohub/client.rb', line 103

def lingohub_headers # :nodoc:
  {
    'X-lingohub-API-Version'     => '1',
    'User-Agent'                 => self.class.gem_version_string,
    'X-Ruby-Version'             => RUBY_VERSION,
    'X-Ruby-Platform'            => RUBY_PLATFORM,
    'content_type'               => 'json',
    'accept'                     => 'json'
  }
end

#post(uri, payload = "", extra_headers = { }) ⇒ Object

:nodoc:



60
61
62
# File 'lib/lingohub/client.rb', line 60

def post(uri, payload="", extra_headers={ }) # :nodoc:
  process(:post, uri, extra_headers, payload)
end

#process(method, uri, extra_headers = { }, payload = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lingohub/client.rb', line 72

def process(method, uri, extra_headers={ }, payload=nil)
  headers = lingohub_headers.merge(extra_headers)
  args     = [method, payload, headers].compact
  
   if credentials[:password] == nil || credentials[:password].empty?
      uri = uri + ((uri.include?('?')) ? "&" : "?")
      uri = uri + "auth_token=#{credentials[:username]}"
    end

  #puts "---- URI --- #{uri} - #{args} - credentials #{credentials}"
  response = resource(uri, credentials).send(*args)

  response
end

#project(title) ⇒ Object



46
47
48
49
50
# File 'lib/lingohub/client.rb', line 46

def project(title)
  project = self.projects[title]
  raise(Lingohub::Command::CommandFailed, "=== You aren't associated for a project named '#{title}'") if project.nil?
  project
end

#projectsObject



52
53
54
# File 'lib/lingohub/client.rb', line 52

def projects
  return Lingohub::Models::Projects.new(self)
end

#put(uri, payload, extra_headers = { }) ⇒ Object

:nodoc:



64
65
66
# File 'lib/lingohub/client.rb', line 64

def put(uri, payload, extra_headers={ }) # :nodoc:
  process(:put, uri, extra_headers, payload)
end

#resource(uri, credentials) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lingohub/client.rb', line 87

def resource(uri, credentials)
  RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
  if uri =~ /^https?/
    RestClient::Resource.new(uri, :user => credentials[:username], :password => credentials[:password])
  else
    host_uri = host =~ /^https?/ ? "#{host}/#{api_uri_part}" : "http://#{host}/#{api_uri_part}"
    
    #puts host_uri + "/" + uri
    RestClient::Resource.new(host_uri, :user => credentials[:username], :password => credentials[:password])[uri]
  end
end