Class: Uploader::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
# File 'lib/uploader/client.rb', line 7

def initialize(url)
  if url[-1] == "/"
    url.chop!
  end
  @url = url
end

Instance Method Details

#create_get_rq_params(params) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/uploader/client.rb', line 14

def create_get_rq_params(params)
  params_str = ""
  params.each do |key, value|
    params_str += "#{key}=#{value}&"
  end
  params_str[0..-2]
end

#get(path, params = {}) ⇒ Object



22
23
24
# File 'lib/uploader/client.rb', line 22

def get(path, params={})
  RestClient.get "#{@url}/#{path}?#{create_get_rq_params(params)}"
end

#get_json(path, params = {}) ⇒ Object

get and return json



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

def get_json(path, params={})
  resp = get(path, params)
  JSON.parse(resp)
end

#item_create(name, file_path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/uploader/client.rb', line 46

def item_create(name, file_path)
  request = RestClient::Request.new(
    :method  => :post,
    :url     => "#{@url}/items/create",
    :payload => {
      :multipart => true,
      :name      => name,
      :file      => File.new(file_path)
    })
  resp = request.execute
  JSON.parse(resp)
end

#post(path, params = {}) ⇒ Object



32
33
34
# File 'lib/uploader/client.rb', line 32

def post(path, params={})
  RestClient.post("#{@url}/#{path}", params)
end

#post_json(path, params = {}) ⇒ Object

post and return json



37
38
39
40
# File 'lib/uploader/client.rb', line 37

def post_json(path, params={})
  resp = post(path, params)
  JSON.parse(resp)
end

#put(path, params = {}) ⇒ Object



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

def put(path, params={})
  RestClient.put("#{rails_server_url}#{path}", params)
end