Class: Dagger::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Client

Returns a new instance of Client.



65
66
67
# File 'lib/dagger.rb', line 65

def initialize(http)
  @http = http
end

Class Method Details

.init(uri, opts) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dagger.rb', line 49

def self.init(uri, opts)
  uri  = Utils.parse_uri(uri)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.port == 443
    http.use_ssl = true
    http.verify_mode = opts[:verify_ssl] === false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  end

  [:open_timeout, :read_timeout, :ssl_version, :ciphers].each do |key|
    http.send("#{key}=", opts[key]) if opts.has_key?(key)
  end

  new(http)
end

Instance Method Details

#closeObject



144
145
146
# File 'lib/dagger.rb', line 144

def close
  @http.finish
end

#delete(uri, data, options = {}) ⇒ Object



102
103
104
# File 'lib/dagger.rb', line 102

def delete(uri, data, options = {})
  request(:delete, uri, data, options)
end

#get(uri, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dagger.rb', line 69

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

  path = uri[0] == '/' ? uri : Utils.parse_uri(uri).request_uri
  path.sub!(/\?.*|$/, '?' + Utils.encode(opts[:query])) if opts[:query]

  request = Net::HTTP::Get.new(path, DEFAULT_HEADERS.merge(opts[:headers] || {}))
  request.basic_auth(opts.delete(:username), opts.delete(:password)) if opts[:username]

  @http.start unless @http.started?
  resp, data = @http.request(request)

  if REDIRECT_CODES.include?(resp.code.to_i) && resp['Location'] && (opts[:follow] && opts[:follow] > 0)
    opts[:follow] -= 1
    puts "Following redirect to #{resp['Location']}"
    return get(resp['Location'], nil, opts)
  end

  @response = build_response(resp, data || resp.body)
end

#open(&block) ⇒ Object



138
139
140
141
142
# File 'lib/dagger.rb', line 138

def open(&block)
  @http.start do
    instance_eval(&block)
  end
end

#patch(uri, data, options = {}) ⇒ Object



98
99
100
# File 'lib/dagger.rb', line 98

def patch(uri, data, options = {})
  request(:patch, uri, data, options)
end

#post(uri, data, options = {}) ⇒ Object



90
91
92
# File 'lib/dagger.rb', line 90

def post(uri, data, options = {})
  request(:post, uri, data, options)
end

#put(uri, data, options = {}) ⇒ Object



94
95
96
# File 'lib/dagger.rb', line 94

def put(uri, data, options = {})
  request(:put, uri, data, options)
end

#request(method, uri, data, opts = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dagger.rb', line 106

def request(method, uri, data, opts = {})
  return get(uri, opts.merge(query: data)) if method.to_s.downcase == 'get'

  uri     = Utils.parse_uri(uri)
  headers = DEFAULT_HEADERS.merge(opts[:headers] || {})

  query = if data.is_a?(String)
    data
  elsif opts[:json]
    Oj.dump(data) # convert to JSON
    headers['Content-Type'] = 'application/json'
  else # querystring, then
    Utils.encode(data)
  end

  if opts[:username] # opts[:password] is optional
    str = [opts[:username], opts[:password]].compact.join(':')
    headers['Authorization'] = "Basic " + Base64.encode64(str)
  end

  args = [method.to_s.downcase, uri.path, query, headers]
  args.delete_at(2) if args[0] == 'delete' # Net::HTTP's delete does not accept data

  @http.start unless @http.started?
  resp, data = @http.send(*args)
  @response = build_response(resp, data || resp.body)
end

#responseObject



134
135
136
# File 'lib/dagger.rb', line 134

def response
  @response or raise 'Request not sent!'
end