Skip to content

Instantly share code, notes, and snippets.

@mensfeld
Created January 10, 2019 09:12
Show Gist options
  • Save mensfeld/d5cb2258c7a6a1749e4edc52fb0bea0d to your computer and use it in GitHub Desktop.
Save mensfeld/d5cb2258c7a6a1749e4edc52fb0bea0d to your computer and use it in GitHub Desktop.
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