Class: Linguist::Client

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

Overview

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

Example:

require 'linguist'
linguist = Linguist::Client.new('[email protected]', 'mypass')
linguist.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/linguist_ruby/client.rb', line 35

def initialize(options)
  @user       = options[:username]
  @password   = options[:password]
  @auth_token = options[:auth_token]
  @host       = options[:host] || 'lingui.st'
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.auth(options) ⇒ Object



30
31
32
33
# File 'lib/linguist_ruby/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/linguist_ruby/client.rb', line 24

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

.versionObject



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

def self.version
  Linguist::VERSION
end

Instance Method Details

#credentialsObject



42
43
44
# File 'lib/linguist_ruby/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/linguist_ruby/client.rb', line 68

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

#extract_warning(response) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/linguist_ruby/client.rb', line 98

def extract_warning(response)
#    return unless response
#    if response.headers[:x_heroku_warning] && @warning_callback
#      warning             = response.headers[:x_heroku_warning]
#      @displayed_warnings ||= { }
#      unless @displayed_warnings[warning]
#        @warning_callback.call(warning)
#        @displayed_warnings[warning] = true
#      end
#    end
end

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

:nodoc:



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

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

#linguist_headersObject

:nodoc:



110
111
112
113
114
115
116
117
118
119
# File 'lib/linguist_ruby/client.rb', line 110

def linguist_headers # :nodoc:
  {
    'X-Linguist-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/linguist_ruby/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
# File 'lib/linguist_ruby/client.rb', line 72

def process(method, uri, extra_headers={ }, payload=nil)
  headers = linguist_headers.merge(extra_headers)
#    payload  = auth_params.merge(payload)
  args     = [method, payload, headers].compact
  response = resource(uri, credentials).send(*args)

#    extract_warning(response)
  response
end

#project(title) ⇒ Object



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

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

#projectsObject



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

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

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

:nodoc:



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

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

#resource(uri, credentials) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/linguist_ruby/client.rb', line 82

def resource(uri, credentials)

  RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
  if uri =~ /^https?/
#      RestClient::Resource.new(uri, user, auth_token)
    RestClient::Resource.new(uri, :user => credentials[:username], :password => credentials[:password])
  elsif host =~ /^https?/
#      RestClient::Resource.new(host, user, auth_token)[uri]
    RestClient::Resource.new(host, :user => credentials[:username], :password => credentials[:password])[uri]
  else
#      RestClient::Resource.new("https://api.#{host}", user, password)[uri]
#      RestClient::Resource.new("http://localhost:3000/api/v1", user, auth_token)[uri]
    RestClient::Resource.new("http://localhost:3000/api/v1", :user => credentials[:username], :password => credentials[:password])[uri]
  end
end