Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created July 26, 2011 01:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/1105733 to your computer and use it in GitHub Desktop.
Save ahoward/1105733 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
Main {
name 'gist'
description <<-__
command line script for writing and reading to http://gist.github.com/
__
examples <<-__
. gist a file, display the url on stdout. also copy url to clipboard.
~> gist a.rb
. gist a file. printing gisted content on stdout. note that this
behaviour makes gist work great as a file in vim/emacs. the location
of the gist is still copied to the clipboard.
~> gist a.rb | cat
. same, but privately
~> gist a.rb | cat
. same, but specify ext
~> gist a.txt --ext .rb | cat
. same, but give the file name (ext is derived)
~> gist javascript.txt --name awesome.js
__
option('--private', '-p'){
default false
}
option('--name=name', '-n'){
default 'gist.rb'
}
option('--ext=ext', '-e'){
default '.rb'
}
option('--debug', '-d'){
}
def run
abort('no auth!') if auth.empty?
@private = !!param['private'].given?
@name = param['name'].value
@ext = param['ext'].value
content = process(argv)
@private = !!@private
@name = File.basename(@name)
@ext = @ext.sub(/^[.]*/, '.')
options = {
:private => @private,
:name => @name,
:ext => @ext
}
gisted = gist(content, options)
if STDIN.tty?
STDOUT.puts(gisted.location)
else
STDOUT.write(gisted)
end
end
def process(*paths)
paths.flatten!
paths.compact!
paths.uniq!
stdin = paths.delete('-')
stdin = true if paths.empty?
content = stdin ? STDIN.read : ''
exts = []
names = []
paths.each do |path|
content += IO.read(path)
basename = File.basename(path)
base, ext = basename.split(/[.]/, 2)
names.push(basename)
exts.push(ext) if ext
end
unless params['ext'].given?
ext = exts.last
@ext = ".#{ ext }" if ext
end
unless params['name'].given?
name = names.last
@name = name if name
end
content
end
def gist(content, options = {})
url = URI.parse('https://gist.github.com/gists')
form = form_for(content, options)
req = post(url, form)
location = req['Location']
class << content
attr_accessor :location
end
content.location = location
copy(location)
content
end
def post(url, params)
url = URI.parse(url.to_s) unless url.is_a?(URI)
req = Net::HTTP::Post.new(url.path)
req.form_data = params
http = Net::HTTP.new(url.host, url.port)
main = self
http.instance_eval do
@debug_output = $stderr if main.params['debug'].given?
http.use_ssl = true
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.start{|http| http.request(req)}
end
def form_for(content, options)
form = {}
form['file_contents[gistfile1]'] = content.to_s
form['file_ext[gistfile1]'] = options[:ext]
form['file_name[gistfile1]'] = options[:name]
form['private'] = 'on' if options[:private]
form.merge(auth)
end
def auth
return @auth if defined?(@auth)
user = `git config --global github.user`.strip
token = `git config --global github.token`.strip
@auth = user.empty? ? {} : {:login => user, :token => token}
end
def copy(content)
@copied = content.to_s.strip
program =
case RUBY_PLATFORM
when /darwin/
pbcopy
when /linux/
xclip
end
IO.popen(program, 'r+'){|pipe| pipe.write(@copied)} if program
@copied
end
def which(program)
stdout = `which #{ program }`.strip
path = stdout == '' ? nil : stdout
end
def pbcopy
return @pbcopy if defined?(@pbcopy)
@pbcopy = which(:pbcopy)
end
def xclip
return @xclip if defined?(@xclip)
@xclip = which(:xclip)
end
mode(:read) do
def run
ids = argv
ids.each do |id|
url = 'https://gist.github.com/%s.txt' % id
content = get(url).body
STDOUT.write(content)
end
end
def get(url)
url = URI.parse(url.to_s) unless url.is_a?(URI)
req = Net::HTTP::Get.new(url.path)
http = Net::HTTP.new(url.host, url.port)
main = self
http.instance_eval do
@debug_output = $stderr if main.params['debug'].given?
http.use_ssl = true
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.start{|http| http.request(req)}
end
end
}
BEGIN {
require 'open-uri'
require 'net/http'
require 'net/https'
require 'rubygems'
require 'main'
}
@Sainozhenko
Copy link

👆

@pratikshah98
Copy link

{
"status_code": 200,
"message": "Delivery options",
"response": [
{
"water_vendor_holiday": {
"timeslotsTemp": [],
"isHoliday": 0
},
"water_vendor_id": "62e8ece53710e99fe8e4f250",
"vendor_locations": [
{
"_id": "635f9530f555282a18783668",
"email": "sonali123@gmail.com",
"attributeId": "62e8ece53710e99fe8e4f250",
"time_slots": [
"08:00 am to 05:00 pm",
"08:00 am to 07:00 pm"
],
"name": "Cool blue",
"city": "xx",
"landmark": "xx",
"area": "xx",
"detail_address": "xxx",
"mobile_no": "783776499",
"country_code": "+255"
}
],
"user_locations": {
"_id": "6411ad4f6f0c6187b080b1db",
"email": "s@gmail.com",
"created_on": "2023-03-15T11:20:58.322Z",
"attributeId": "63ea30cd871526476c215df2",
"time_slots": [
"02:00 pm to 04:00 pm"
],
"future_time_slots": [],
"name": "adfg",
"city": "ds",
"landmark": "sal",
"area": "sld",
"detail_address": "addl",
"mobile_no": "(+255)-998-877-112-1",
"country_code": "+255",
"lat": "12.00",
"lng": "13.00",
"address_type": "SokoniVendor",
"minimum_order": 0,
"__v": 0,
"user_id": "64abd6a3a47ee823d4965809"
},
"delivery_charge": {
"deliveryCharge": 0,
"deliveryNotAllowed": false,
"geoWaterMessage": "\n"
},
"delivery_charge_old": {
"deliveryCharge": 0,
"deliveryNotAllowed": false,
"geoWaterMessage": "\n"
},
"water_vendor_status": 0,
"water_vendor_status_text": "PRE ORDER",
"delivery_branch_id": "62e8ece53710e99fe8e4f250",
"day_wise_data": [
{
"date_to_show": "2023-10-12",
"day_to_show": "Thursday",
"time_slot": [
{
"_id": "63281a605a592016da617e9f",
"starttime": 13,
"endtime": 14,
"starthour": 13,
"startminute": 0,
"endhour": 14,
"endminute": 0,
"display_txt": "01:00 pm to 02:00 pm"
},
{
"_id": "63281b025a592016da617ea8",
"starttime": 14,
"endtime": 15,
"starthour": 14,
"startminute": 0,
"endhour": 15,
"endminute": 0,
"display_txt": "02:00 pm to 03:00 pm"
},
{
"_id": "6393252b2d4f1d3934c562fb",
"starttime": 15,
"endtime": 16,
"starthour": 15,
"startminute": 0,
"endhour": 16,
"endminute": 0,
"display_txt": "03:00 pm to 04:00 pm"
},
{
"_id": "639331ba6933f215841004ca",
"starttime": 16,
"endtime": 17,
"starthour": 16,
"startminute": 0,
"endhour": 17,
"endminute": 0,
"display_txt": "04:00 pm to 05:00 pm"
}
]
},
{
"date_to_show": "2023-10-15",
"day_to_show": "Sunday",
"time_slot": [
{
"_id": "63281a605a592016da617e9f",
"starttime": 13,
"endtime": 14,
"starthour": 13,
"startminute": 0,
"endhour": 14,
"endminute": 0,
"display_txt": "01:00 pm to 02:00 pm"
},
{
"_id": "63281b025a592016da617ea8",
"starttime": 14,
"endtime": 15,
"starthour": 14,
"startminute": 0,
"endhour": 15,
"endminute": 0,
"display_txt": "02:00 pm to 03:00 pm"
},
{
"_id": "6393252b2d4f1d3934c562fb",
"starttime": 15,
"endtime": 16,
"starthour": 15,
"startminute": 0,
"endhour": 16,
"endminute": 0,
"display_txt": "03:00 pm to 04:00 pm"
},
{
"_id": "639331ba6933f215841004ca",
"starttime": 16,
"endtime": 17,
"starthour": 16,
"startminute": 0,
"endhour": 17,
"endminute": 0,
"display_txt": "04:00 pm to 05:00 pm"
}
]
},
{
"date_to_show": "2023-10-16",
"day_to_show": "Monday",
"time_slot": [
{
"_id": "63281a605a592016da617e9f",
"starttime": 13,
"endtime": 14,
"starthour": 13,
"startminute": 0,
"endhour": 14,
"endminute": 0,
"display_txt": "01:00 pm to 02:00 pm"
},
{
"_id": "63281b025a592016da617ea8",
"starttime": 14,
"endtime": 15,
"starthour": 14,
"startminute": 0,
"endhour": 15,
"endminute": 0,
"display_txt": "02:00 pm to 03:00 pm"
},
{
"_id": "6393252b2d4f1d3934c562fb",
"starttime": 15,
"endtime": 16,
"starthour": 15,
"startminute": 0,
"endhour": 16,
"endminute": 0,
"display_txt": "03:00 pm to 04:00 pm"
},
{
"_id": "639331ba6933f215841004ca",
"starttime": 16,
"endtime": 17,
"starthour": 16,
"startminute": 0,
"endhour": 17,
"endminute": 0,
"display_txt": "04:00 pm to 05:00 pm"
}
]
}
]
}
],
"is_multi_vendor": 0,
"total_delivery_charge": "",
"delivery_type": "Pick_Up_Delivery"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment