Class: Dagger::Client
- Inherits:
-
Object
- Object
- Dagger::Client
- Defined in:
- lib/dagger.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(uri, opts = {}) ⇒ Object
-
#initialize(http) ⇒ Client
constructor
A new instance of Client.
- #request(method, uri, data, opts = {}) ⇒ Object
- #response ⇒ Object
Constructor Details
#initialize(http) ⇒ Client
Returns a new instance of Client.
61 62 63 |
# File 'lib/dagger.rb', line 61 def initialize(http) @http = http end |
Class Method Details
.init(uri, opts) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dagger.rb', line 45 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
#get(uri, opts = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dagger.rb', line 65 def get(uri, opts = {}) opts[:follow] = 10 if opts[:follow] == true path = uri[0] == '/' ? uri : Utils.parse_uri(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] @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 response end |
#request(method, uri, data, opts = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/dagger.rb', line 84 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 @resp, @data = @http.send(*args) response end |
#response ⇒ Object
111 112 113 114 |
# File 'lib/dagger.rb', line 111 def response raise 'No response yet!' unless @resp @response ||= build_response(@resp, @data || @resp.body) # 1.8 vs 1.9 style responses end |