Class: CloudAlign::Client

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

Class Method Summary collapse

Class Method Details

.authorized_url(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cloudalign/client.rb', line 55

def authorized_url(path)
  url = CloudAlign.config.get("api_url", "http://api.cloudalign.com").sub(/\/$/, '')

  if path.starts_with?(url)
    uri = URI.parse(path)
  elsif path.starts_with?('/')
    uri = URI.parse(url)
    uri.path = path
  else
    return path
  end

  uri.user = CGI.escape(CloudAlign.config.get("api_user"))
  uri.password = CGI.escape(CloudAlign.config.get("api_password"))

  uri.to_s
end

.delete(path) ⇒ Object



25
26
27
# File 'lib/cloudalign/client.rb', line 25

def delete(path)
  RestClient.delete(authorized_url(path))
end

.get(path) ⇒ Object



15
16
17
# File 'lib/cloudalign/client.rb', line 15

def get(path)
  RestClient.get(authorized_url(path))
end

.get_file(path, output_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cloudalign/client.rb', line 29

def get_file(path, output_path)
  ::File.open(output_path, 'w') do |out|
    process_response = lambda do |response|
      response.read_body do |chunk|
        print "."
        out.write chunk
      end
    end

    RestClient::Request.execute(:method => :get, :url => authorized_url(path), :block_response => process_response)
  end
end

.get_json(path) ⇒ Object



19
20
21
22
23
# File 'lib/cloudalign/client.rb', line 19

def get_json(path)
  uri = URI.parse(path)
  uri.path += '.json' unless uri.path.match(/\.json$|\/$/)
  JSON.parse(get(uri.to_s))
end

.post(path, data) ⇒ Object



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

def post(path, data)
  RestClient.post(authorized_url(path), data)
end

.post_for_upload(path, data) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/cloudalign/client.rb', line 46

def post_for_upload(path, data)
  pp data
  RestClient.post(authorized_url(path), data) do |response, request, result, &block|
    if [301, 302, 303, 307].include? response.code
      return get_json(response.headers[:location])
    end
  end
end