marvelapi

commit a1bd1b04ea3dfeda84bce49fca04ac0724d8ffc7

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

lib/http: add a client

 lib/http/client.rb | 29 +++++++++++++++++++++++++++++


diff --git a/lib/http/client.rb b/lib/http/client.rb
new file mode 100644
index 0000000000000000000000000000000000000000..de5f626469599281021eebda798f64d65a38ebb9
--- /dev/null
+++ b/lib/http/client.rb
@@ -0,0 +1,29 @@
+require 'uri'
+require 'net/http'
+
+module HTTP
+  class Client
+    def self.get(url, params = nil)
+      uri = URI(url)
+      uri.query = encode_params(params)
+      res = Net::HTTP.get_response(uri)
+
+      parsed_body = nil
+
+      if res.is_a?(Net::HTTPSuccess)
+        begin
+          parsed_body = JSON.parse(res.body)
+        rescue
+        end
+      else
+        puts "There was an error when fetching data"
+      end
+
+      parsed_body
+    end
+
+    def self.encode_params(params)
+      URI.encode_www_form(params)
+    end
+  end
+end