Class: Neuron::Client::Connection
- Defined in:
- lib/neuron-client/connection.rb
Instance Method Summary collapse
- #delete(path = "", attrs = {}) ⇒ Object
- #get(path = "", attrs = {}) ⇒ Object
-
#initialize(url, key) ⇒ Connection
constructor
A new instance of Connection.
- #post(path = "", form = {}, attrs = {}) ⇒ Object
- #put(path = "", form = {}, attrs = {}) ⇒ Object
- #query_string(attrs = {}) ⇒ Object
Constructor Details
#initialize(url, key) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 |
# File 'lib/neuron-client/connection.rb', line 7 def initialize(url, key) @url = url @key = key end |
Instance Method Details
#delete(path = "", attrs = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/neuron-client/connection.rb', line 66 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) 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/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 |
# File 'lib/neuron-client/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) else raise "Error : #{response.code} - #{response.to_str}" end end end |
#put(path = "", form = {}, attrs = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/neuron-client/connection.rb', line 52 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) 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/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)}" end q.join("&") end |