Created
January 10, 2019 09:12
-
-
Save mensfeld/d5cb2258c7a6a1749e4edc52fb0bea0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
class Api | |
HOST = 'localhost' | |
PORT = 3000 | |
def initialize | |
@http = Net::HTTP.new(HOST, PORT) | |
end | |
def get | |
request nil, 'GET' | |
end | |
def add(data) | |
request data, 'ADD' | |
end | |
def del(data) | |
request data, 'DEL' | |
end | |
private | |
def request(data, cmd) | |
Net::HTTP::Post | |
.new('/', 'Content-Type': 'application/json') | |
.tap { |req| req.body = "#{data},#{cmd}" } | |
.yield_self(&@http.method(:request)) | |
.yield_self(&:body) | |
end | |
end | |
client = Api.new | |
client.get | |
client.add('12313131') | |
client.add('msg') | |
client.del('msg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment