Class: Neuron::Client::AdminConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/neuron-client/admin_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, key) ⇒ AdminConnection

Returns a new instance of AdminConnection.



7
8
9
10
# File 'lib/neuron-client/admin_connection.rb', line 7

def initialize(url, key)
  @url = url
  @key = key
end

Instance Method Details

#delete(path = "", attrs = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/neuron-client/admin_connection.rb', line 72

def delete(path="", attrs={})
  format = attrs.delete(:format) || :json
  RestClient.delete("#{@url}/#{[path, format].select(&:present?).join(".")}?#{query_string(attrs)}",
    format.present? ? {:content_type => format, :accept => format} : {}) do |response, request, result, &block|
    case response.code
    when 200
      return (format == :json ? Yajl.load(response.to_str) : response.to_str)
    when 204
      return nil
    else
      raise "Error : #{response.code} - #{response.to_str}"
    end
  end
end

#get(path = "", attrs = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/neuron-client/admin_connection.rb', line 20

def get(path="", attrs={})
  format = attrs.delete(:format) || :json
  RestClient.get("#{@url}/#{[path, format].select(&:present?).join(".")}?#{query_string(attrs)}",
    format.present? ? {:content_type => format, :accept => format} : {}) do |response, request, result, &block|
    # follow redirection
    if [301, 302, 307].include? response.code
      response.follow_redirection(request, result, &block)
    end

    case response.code
    when 200
      return (format == :json ? Yajl.load(response.to_str) : response.to_str)
    else
      raise "Error : #{response.inspect}"
    end
  end
end

#post(path = "", form = {}, attrs = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/neuron-client/admin_connection.rb', line 38

def post(path="", form={}, attrs={})
  format = attrs.delete(:format) || :json
  RestClient.post("#{@url}/#{[path, format].select(&:present?).join(".")}?#{query_string(attrs)}",
    (format == :json ? Yajl.dump(form) : form),
    format.present? ? {:content_type => format, :accept => format} : {}) do |response, request, result, &block|
    case response.code
    when 201
      return (format == :json ? Yajl.load(response.to_str) : response.to_str)
    when 422
      throw :errors, format == :json ? Yajl.load(response.to_str) : response.to_str
    else
      raise "Error : #{response.code} - #{response.to_str}"
    end
  end
end

#put(path = "", form = {}, attrs = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/neuron-client/admin_connection.rb', line 54

def put(path="", form={}, attrs={})
  format = attrs.delete(:format) || :json
  RestClient.put("#{@url}/#{[path, format].select(&:present?).join(".")}?#{query_string}",
    (format == :json ? Yajl.dump(form) : form),
    format.present? ? {:content_type => format, :accept => format} : {}) do |response, request, result, &block|
    case response.code
    when 200
      return (format == :json ? Yajl.load(response.to_str) : response.to_str)
    when 204
      return nil
    when 422
      throw :errors, format == :json ? Yajl.load(response.to_str) : response.to_str
    else
      raise "Error : #{response.code} - #{response.to_str}"
    end
  end
end

#query_string(attrs = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/neuron-client/admin_connection.rb', line 12

def query_string(attrs={})
  q = []
  {:api_key => @key}.merge(attrs).each do |key, value|
    q << "#{key}=#{value.nil? ? '' : CGI::escape(value.to_s)}"
  end
  q.join("&")
end