Module: Passtools::Request

Included in:
Pass, Template
Defined in:
lib/passtools/request.rb

Instance Method Summary collapse

Instance Method Details

#construct_url(path) ⇒ Object



41
42
43
44
# File 'lib/passtools/request.rb', line 41

def construct_url(path)
  raise "You must configure API url before calling" if Passtools.url.to_s.empty?
  Passtools.url + path
end

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



34
35
36
37
38
39
# File 'lib/passtools/request.rb', line 34

def delete_request(path, params = {})
  url = construct_url(path)
  params.merge!(:api_key => Passtools.api_key )
  response = RestClient.delete(url, headers.merge(:params => params))
  MultiJson.load(response)
end

#download_file(path, filename) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/passtools/request.rb', line 11

def download_file(path, filename)
  filepath = Pathname.new(Passtools.download_dir.to_s)
  raise "Download directory is not defined or does not exist" unless filepath.exist?
  url = construct_url(path)
  params = {:api_key => Passtools.api_key}
  response = RestClient.get(url, headers.merge(:params => params))
  File.open(filepath.join(filename), 'w:ascii-8bit') {|f| f.write(response) }
end

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



4
5
6
7
8
9
# File 'lib/passtools/request.rb', line 4

def get(path, params = {})
  url = construct_url(path)
  params.merge!(:api_key => Passtools.api_key)
  response = RestClient.get(url, headers.merge(:params => params))
  MultiJson.load(response)
end

#headersObject



46
47
48
49
# File 'lib/passtools/request.rb', line 46

def headers
  { :user_agent => "passtools-gem-#{Passtools::VERSION}",
    :accept => :json }
end

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



20
21
22
23
24
25
# File 'lib/passtools/request.rb', line 20

def post(path, params = {})
  url = construct_url(path)
  params.merge!(:api_key => Passtools.api_key )
  response = RestClient.post(url, params, headers.merge(:multipart => true) )
  MultiJson.load(response)
end

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



27
28
29
30
31
32
# File 'lib/passtools/request.rb', line 27

def put(path, params = {})
  url = construct_url(path)
  params.merge!(:api_key => Passtools.api_key )
  response = RestClient.put(url, params, headers.merge(:multipart => true) )
  MultiJson.load(response)
end