Module: Board::Client::Request
- Included in:
- Board::Client
- Defined in:
- lib/board/client/request.rb
Instance Method Summary collapse
- #get(path, params = {}) ⇒ Object
- #hash_to_query_string(hash) ⇒ Object
- #post(path, params) ⇒ Object
- #request(path, params, method) ⇒ Object
Instance Method Details
#get(path, params = {}) ⇒ Object
12 13 14 |
# File 'lib/board/client/request.rb', line 12 def get(path, params = {}) request path, params, :get end |
#hash_to_query_string(hash) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/board/client/request.rb', line 72 def hash_to_query_string(hash) params = '' stack = [] hash.each do |k, v| if v.is_a?(Hash) stack << [k,v] else params << "#{k}=#{v}&" end end stack.each do |parent, hash| hash.each do |k, v| if v.is_a?(Hash) stack << ["#{parent}[#{k}]", v] else params << "#{parent}[#{k}]=#{v}&" end end end params.chop! # trailing & params end |
#post(path, params) ⇒ Object
8 9 10 |
# File 'lib/board/client/request.rb', line 8 def post(path, params) request path, params, :post end |
#request(path, params, method) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/board/client/request.rb', line 16 def request(path, params, method) params.merge!(:user_credentials => @api_key) uri = URI.parse(@endpoint + path) http = Net::HTTP.new(uri.host, uri.port) if uri.port == 443 # ssl? http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.ca_path = @ssl[:ca_path] if @ssl[:ca_path] end case method when :get request = Net::HTTP::Get.new(uri.request_uri) when :post request = Net::HTTP::Post.new(uri.request_uri) end request.set_form_data(params) response = http.request(request) case response.code when /2../ if response.body.blank? true else Yajl::Parser.parse(response.body) end when '400' raise Client::BadRequest.new(response) when '401' raise Client::Unauthorized.new(response) when '403' raise Client::Forbidden.new(response) when '404' raise Client::NotFound.new(response) when '406' raise Client::NotAcceptable.new(response) when '409' raise Client::Conflict.new(response) when '422' raise Client::UnprocessableEntity.new(response) when '500' raise Client::InternalServerError.new(response) when '501' raise Client::NotImplemented.new(response) when '502' raise Client::BadGateway.new(response) when '503' raise Client::ServiceUnavailable.new(response) else raise Client::Error.new(response) end end |