Module: Dagger

Defined in:
lib/dagger.rb,
lib/dagger/version.rb,
lib/dagger/response.rb

Defined Under Namespace

Modules: Response

Constant Summary collapse

REDIRECT_CODES =
[301, 302, 303].freeze
DEFAULT_HEADERS =
{
  'Accept' => '*/*',
  'User-Agent' => "Dagger/#{VERSION} (Ruby Net::HTTP Wrapper, like curl)"
}
MAJOR =
0
MINOR =
5
PATCH =
1
VERSION =
[MAJOR, MINOR, PATCH].join('.')

Class Method Summary collapse

Class Method Details

.delete(uri, params = {}, options = {}) ⇒ Object



47
48
49
# File 'lib/dagger.rb', line 47

def self.delete(uri, params = {}, options = {})
  send_request('delete', uri, params, options)
end

.get(uri, query = nil, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dagger.rb', line 17

def self.get(uri, query = nil, opts = {})
  opts[:follow] = 10 if opts[:follow] == true

  uri       = parse_uri(uri)
  uri.query = encode(query) if query
  http      = client(uri, opts)
  request   = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADERS.merge(opts[:headers] || {}))

  if opts[:username] # && opts[:password]
    request.basic_auth(opts.delete(:username), opts.delete(:password))
  end

  resp, data = http.request(request)

  if REDIRECT_CODES.include?(resp.code.to_i) && resp['Location'] && (opts[:follow] && opts[:follow] > 0)
    opts[:follow] -= 1
    return get(resp['Location'], nil, opts)
  end

  build_response(resp, data || resp.body) # 1.8 vs 1.9 style responses
end

.post(uri, params = {}, options = {}) ⇒ Object



39
40
41
# File 'lib/dagger.rb', line 39

def self.post(uri, params = {}, options = {})
  send_request('post', uri, params, options)
end

.put(uri, params = {}, options = {}) ⇒ Object



43
44
45
# File 'lib/dagger.rb', line 43

def self.put(uri, params = {}, options = {})
  send_request('put', uri, params, options)
end

.request(method, url, params = {}, options = {}) ⇒ Object



51
52
53
54
# File 'lib/dagger.rb', line 51

def self.request(method, url, params = {}, options = {})
  return get(url, params, options) if method.to_s.downcase == 'get'
  send_request(method.to_s.downcase, url, params, options)
end