Creating Gist from a Ruby script - ruby

Is there a way to create an Anonymous (public or private) gist using net/http or net/https?

This worked for me.
require 'net/http'
require 'json'
uri = URI("https://api.github.com/gists")
payload = {
'description' => "My test gist",
'public' => true,
'files' => {
'test.txt' => {
'content' => "This is a test!\n\nI am making a public gist."
}
}
}
req = Net::HTTP::Post.new(uri.path)
req.body = payload.to_json
puts req.inspect
puts req.body.inspect
# GitHub API is strictly via HTTPS, so SSL is mandatory
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
http.request(req)
end
puts res.inspect
puts res.body.inspect
Result: My test gist

This gem will do the trick for you https://github.com/defunkt/gist!
For the record, it does require net/https.

Related

team city api returns json example

I'm struggling with getting results from the team city api in JSON
require 'open-uri'
url = ".../app/rest/buildQueue/"
c = Curl::Easy.new(url) do |curl|
curl.headers["Content-type"] = "application/json"
curl.http_auth_types = :basic
curl.username = 'user'
curl.password = 'password'
end
c.perform
puts c.body_str
I get a bunch of xml text
You need to use the Accept header to control the response type:
e.g (command line)
curl --url http://xxx/app/rest/buildQueue/ -H Accept:"application/json"
Documentation Reference
also you can use "net/http"
require 'net/http'
require 'uri'
url = URI('http://localhost:8111/httpAuth/app/rest/agents')
req = Net::HTTP::Get.new(url)
req['Accept'] = 'application/json'
req.basic_auth 'admin', 'admin'
res = Net::HTTP.start(url.hostname, url.port) {|http|
http.request(req)
}
puts res.body

Getting Headers from a Ruby Net::HTTP Request before making the request

In Ruby, how can I get hold of the HTTP Request Headers that will be sent by a net/http(s) or open-uri request BEFORE it actually makes the request.
In some cases, headers are used when creating a signed string in a URI. Surely there is some way to acquire the request headers that will be sent. These should include the "Host:" header for example.
see http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#label-Setting+Headers
Works well in ruby 2.0.0 - but you are correct, different behavior in 1.9.3
Ruby 2.0.0
require 'net/http'
uri = URI('http://github.com/ruby')
http_request = Net::HTTP::Get.new(uri)
http_request.each_header { |header| puts header }
# => accept-encoding
# => accept
# => user-agent
# => host
http_response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(http_request)
end
Ruby 1.9.3
require 'uri'
require 'net/http'
uri = URI.parse('http://github.com/ruby')
http_request = Net::HTTP::Get.new(uri.path)
http_request.each_header { |header| puts header }
# => accept
# => user-agent
http_response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(http_request)
end
http_request.each_header { |header| puts header }
# => accept
# => user-agent
# => host
The Net::HTTP classes all seem to use Net::HTTPHeader as a mixin. You should be able to use to_hash() on the request object to get all headers at once, or each_header() / each() to iterate one header at a time.
Here's some code I wrote to help.
def get_request_headers(request)
http_method = request.method
path = request.path
request.to_hash.merge("method" => [http_method]).merge("path" => [path])
end
So now, you can run something like this.
url = URI("http://www.google.com")
request, response = Net::HTTP.start(uri(ico).host) do |http|
request = Net::HTTP::Get.new(uri(ico))
response = http.request request
[request, response]
end
get_request_headers(request)
=> {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["www.google.com"], "method"=>["GET"], "path"=>["/"]}
request.to_hash give us a few headers for free, but there's more information stored in the instance variables for the request and response classes.
With the following code, you can check if there's anything else you'd like to merge into the basic request hash.
request.instance_variables.each do |variable|
puts "#{variable}: #{request.instance_variable_get(variable)}"
end
=> #method: GET
=> #request_has_body: false
=> #response_has_body: true
=> #uri: http://www.google.com
=> #path: /
=> #decode_content: true
=> #header: {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["www.google.com"]}
=> #body:
=> #body_stream:
=> #body_data:
=> [:#method, :#request_has_body, :#response_has_body, :#uri, :#path, :#decode_content, :#header, :#body, :#body_stream, :#body_data]
Note that I've pulled out the method and path for the get_request_headers method.
Finally, you can do the same for the response.
def get_response_headers(response)
code = response.code
http_version = response.http_version
message = response.message
response.to_hash.merge("code" => [code]).merge("http_version" => [http_version]).merge("message" => [message])
end
get_response_headers(response)
=> {"date"=>["Thu, 06 May 2021 14:34:27 GMT"], "expires"=>["-1"], "cache-control"=>["private, max-age=0"], "content-type"=>["text/html; charset=ISO-8859-1"], "p3p"=>["CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\""], "server"=>["gws"], "content-length"=>["6067"], "x-xss-protection"=>["0"], "x-frame-options"=>["SAMEORIGIN"], "set-cookie"=>["NID=215=dYowhmNSD9_CnKYLtsFI3uWVGy8ca8PKJTE8VY6_92q7tU5Y_AOWLsaabXxlSPBjc2QjOr4xXVX5SGMCrccTCnBR9pbdsKkrpVTV5TMqrV6H09ChxGjBr5mHVdZkgjOxswiXu72TF3eAX0uhXqloDb-5gmZ6NJ4w1YDKQKNoDp4; expires=Fri, 05-Nov-2021 14:34:27 GMT; path=/; domain=.google.com; HttpOnly"], "code"=>["200"], "http_version"=>["1.1"], "message"=>["OK"]}

New York Public Library search query

I am attempting to access the NY Public Library digital collection through their api.
The ruby code I am using to make the call is:
require 'sinatra'
require 'pp'
require 'httparty'
get '/' do
url = "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
headers = { "Authorization" => "Token token=mytoken" }
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = http.request(request)
puts response
#response = response.body
erb :index
end
So, a couple of questions:
This produces no result, nothing comes back with the puts response in the terminal, not even an error message. What am I doing wrong?
In any case, how do I attach a query at the end of the uri string? BY adding a ? followed by the query? So, for example, ?leonardodavinci
Recommend you play around at the irb command prompt, e.g.
irb [enter]
Looks like not authorized perhaps? I eventually get => nil myself with:
irb(main):003:0> require 'sinatra'
=> true
irb(main):004:0> require 'pp'
=> true
irb(main):005:0> require 'httparty'
=> false
irb(main):006:0> url = "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
=> "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
irb(main):007:0> uri = URI.parse(url)
=> #<URI::HTTP:0x9826744 URL:http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json>
irb(main):008:0> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP api.repo.nypl.org:80 open=false>
irb(main):009:0> headers = { "Authorization" => "Token token=mytoken" }
=> {"Authorization"=>"Token token=mytoken"}
irb(main):010:0> request = Net::HTTP::Get.new(uri.request_uri, headers)
=> #<Net::HTTP::Get GET>
irb(main):011:0> response = http.request(request)
=> #<Net::HTTPUnauthorized 401 Authorization Required readbody=true>
irb(main):012:0> puts response
#<Net::HTTPUnauthorized:0x9938a74>
=> nil
irb(main):013:0>

RUBY - SSL, Basic Auth, and POST

I'm having quite a hard time with this -- seems like there are a few snippets of code lying around that I can't seem to piece together. I'm simply trying to POST key/value pairs, but getting Connection refused - connect(2) (Errno::ECONNREFUSED). Help!
require 'net/http'
require 'net/https'
require 'uri'
#http = Net::HTTP.new('https://my.url.com/path', 443)
#http.use_ssl = true
#http.start() { |http|
req = Net::HTTP.post_form(
URI.parse('https:///my.url.com/path'),
{'key1' => 'value1', 'key2' => 'value2'}
)
req.basic_auth 'username', 'password'
response = http.request(req)
puts response.body
}
HTTP#post_form will execute directly, ignoring your other settings. Try this instead:
require 'net/http'
require 'uri'
url = URI.parse('https://my.url.com/path')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'key1' => 'val1', 'key2' => 'val2'})
resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp
You are likely to run into trouble with the server's certificates. See my other post for instructions on how to get/configure them.
Found this question during my search for a solultion, and just to be clear emboss' code as is is non functional. What I got working is this (inside of a larger Sinatra application):
require 'net/http'
require 'net/https'
require 'uri'
set :api_username, 'usr'
set :api_passwor, 'pswd'
def do_auth_check params
puts params.inspect
url = URI.parse("https://my_auth_site.com:443/authenticate")
req = Net::HTTP::Post.new(url.path)
req.basic_auth options.api_username, options.api_password
req.set_form_data({'username' => params[:name], 'password' => params[:pass]})
sock = Net::HTTP.new(url.host, url.port)
sock.use_ssl = true
res = sock.start {|http| http.request(req) }
# you're on your own down here to identify success/failure, but for me 2xx/3xx was ok and 401/404/500/etc would be failure
return true if res.code.to_i < 400
return "Error logging in"
end

devise authentication via ruby script

I have small application, and I will have some external applications put data to this service over http with rest. I already have it working but without authentication. In portal I use devise, and my question is: how to (example desired) authenticate to portal from ruby script level? What to add to following script to authenticate first? I want to protect this controller with devise and then I need authentication part to following script.
require "net/http"
require "json"
#host = "localhost"
#port = 3000
#post_ws = "/external/rd"
#req_body = {
"device" => {
"name" => "device_json",
"operating_system_id" => "7",
"hash_string" => "jfsg3k4ovj0j02jv",
"user_id" => "1"
}
}.to_json
req = Net::HTTP::Post.new(#post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = #req_body
response = Net::HTTP.new(#host, #port).start {|http| http.request(req) }
Regards,
Mateusz
Here is solution:
I used token_authenticatable from devise.
Here is one great answer how to implement it with json. I had some trouble and described them in this question. There is also answer.
Here goes example code:
require "net/http"
require "json"
#host = "localhost"
#port = 3000
#post_sign = "/users/sign_in.json"
#post_ws = "/external/rd"
#req_sign = {
"user" => {
"email" => "some#email.com",
"password" => "123456"
}
}.to_json
sig = Net::HTTP::Post.new(#post_sign, initheader = {'Content-Type' => 'application/json'})
sig.body = #req_sign
http = Net::HTTP.new(#host, #port).start
resp1 = http.request(sig)
puts "Response: #{resp1.code} , Message: #{resp1.message} , Body: #{resp1.body}"
if resp1.code == "200" then
puts "logged in"
json_resp = JSON.parse(resp1.body)
#auth_token = json_resp['auth_token']
#req_body = {
"device" => {
"name" => "device_json",
"operating_system_id" => "7",
"hash_string" => "jfsg3k4ovj0j02jv"
},
"auth_token" => #auth_token
}.to_json
req = Net::HTTP::Post.new(#post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = #req_body
response = http.request(req)
puts "Response: #{response.code} , Message: #{response.message} , Body: #{response.body}"
end
Regards,
Mateusz

Resources