Class: Linguist::Client
- Inherits:
-
Object
- Object
- Linguist::Client
- 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
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #credentials ⇒ Object
-
#delete(uri, extra_headers = { }) ⇒ Object
:nodoc:.
- #extract_warning(response) ⇒ Object
-
#get(uri, extra_headers = { }) ⇒ Object
:nodoc:.
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
-
#linguist_headers ⇒ Object
:nodoc:.
-
#post(uri, payload = "", extra_headers = { }) ⇒ Object
:nodoc:.
- #process(method, uri, extra_headers = { }, payload = nil) ⇒ Object
- #project(title) ⇒ Object
- #projects ⇒ Object
-
#put(uri, payload, extra_headers = { }) ⇒ Object
:nodoc:.
- #resource(uri, credentials) ⇒ Object
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() @user = [:username] @password = [:password] @auth_token = [:auth_token] @host = [:host] || 'lingui.st' end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
28 29 30 |
# File 'lib/linguist_ruby/client.rb', line 28 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
28 29 30 |
# File 'lib/linguist_ruby/client.rb', line 28 def password @password end |
#user ⇒ Object
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() client = new() OkJson.decode client.post('/sessions', {}, :accept => 'json').to_s end |
.gem_version_string ⇒ Object
24 25 26 |
# File 'lib/linguist_ruby/client.rb', line 24 def self.gem_version_string "linguist-gem/#{version}" end |
Instance Method Details
#credentials ⇒ Object
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_headers ⇒ Object
: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 |
#projects ⇒ Object
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 |