class Elevate::HTTP::HTTPClient

Public Class Methods

new(base_url) click to toggle source
# File lib/elevate/http/http_client.rb, line 4
def initialize(base_url)
  @base_url = NSURL.URLWithString(base_url)
  @credentials = nil
end

Public Instance Methods

delete(path, &block) click to toggle source
# File lib/elevate/http/http_client.rb, line 21
def delete(path, &block)
  issue(:delete, path, nil, &block)
end
get(path, query={}, &block) click to toggle source
# File lib/elevate/http/http_client.rb, line 9
def get(path, query={}, &block)
  issue(:get, path, nil, query: query, &block)
end
post(path, body, &block) click to toggle source
# File lib/elevate/http/http_client.rb, line 13
def post(path, body, &block)
  issue(:post, path, body, &block)
end
put(path, body, &block) click to toggle source
# File lib/elevate/http/http_client.rb, line 17
def put(path, body, &block)
  issue(:put, path, body, &block)
end
set_credentials(username, password) click to toggle source
# File lib/elevate/http/http_client.rb, line 25
def set_credentials(username, password)
  @credentials = { username: username, password: password }
end

Private Instance Methods

issue(method, path, body, options={}, &block) click to toggle source
# File lib/elevate/http/http_client.rb, line 31
def issue(method, path, body, options={}, &block)
  url = url_for(path)

  options[:headers] ||= {}
  options[:headers]["Accept"] = "application/json"

  if @credentials
    options[:credentials] = @credentials
  end

  if body
    options[:body] = NSJSONSerialization.dataWithJSONObject(body, options:0, error:nil)
    options[:headers]["Content-Type"] = "application/json"
  end

  Elevate::HTTP.send(method, url, options)
end
url_for(path) click to toggle source
# File lib/elevate/http/http_client.rb, line 49
def url_for(path)
  path = CFURLCreateStringByAddingPercentEscapes(nil, path.to_s, "[]", ";=&,", KCFStringEncodingUTF8)

  NSURL.URLWithString(path, relativeToURL:@base_url).absoluteString
end