So, I've been following this guide: https://docs.github.com/pt/developers/webhooks-and-events/webhooks/creating-webhooks
This is my code:
require 'sinatra'
require 'json'
require 'openssl'
post '/payload' do
request.body.rewind
header_token = request.env["zzzzzzzzzzzzzzzzzzzz"]
payload_body = request.body.read
verify_signature(payload_body)
push = JSON.parse(payload_body)
"I got some JSON: #{push.inspect}"
end
def verify_signature(payload_body)
signature = 'sha256=' + (OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'),
ENV['jksdaskçjdaskçdhaskdhakdhaskdhaskdhaskdaskdasdkh'],
payload_body))
return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE_256'])
end
When I load http://localhost:4567 i get :
Sinatra doesn’t know this ditty.
If I change Post to Get (trying to see if it is working) :
no implicit conversion of nil into String file: payload.rb location:
hexdigest line: 15
I'm pretty new to this and I'm attempting to create a prestashop module for a payment gateway, but this webhook thing is messing with me.
Im I doing something wrong? Is there a easy way? Is it everything correct?
Thanks
Related
I am calling an API in Sensu custom metric-collection-check, but when I tried to parse the response its throw the below error
Request returned undefined method `parse' for Sensu::Plugin::Metric::CLI::JSON:Class
While the same API parse working in standard-check when it extends from Sensu::Plugin::Check
Here is the check
#!/opt/sensu/embedded/bin/ruby
require 'socket'
require 'active_support/core_ext/hash'
require 'json'
require "rest-client"
require 'sensu-plugin/metric/cli'
class SomeName < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-s SCHEME',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.stream"
def run
response = ""
begin
response =::JSON.parse RestClient::Request.execute(method: :get, url: "http://localhost:3001/stats" )
rescue Exception => e
critical "#{config[:scheme]}.name Metric check failed, Request returned #{e.message}"
end
output "#{config[:scheme]}.somekey", response["somekey"]
ok
end
end
Working with the following class.
class SomeName < Sensu::Plugin::Check::CLI
Seems like something wrong with sense metric plugin, any suggestion would be appreciated.
Fix the issue by calling global method using :: Operators also it does not require to import require 'json' as sensu-plugin come with JSON class.
For a metric, you can subclass one of the following:
Sensu::Plugin::Metric::CLI::JSON
sensu-plugin Checks and Metrics
response =::JSON.parse RestClient::Request.execute(method: :get, url: "http://localhost:3001/stats" )
I am having issues with the following code. I am not getting any errors but for some reason my results are coming back empty every time I use a search keyword that has a space in the string. I have tried using urleescape but that also doesn't work. Does anyone have any insight?
require 'httparty'
require 'uri'
class Recipe
include HTTParty
base_uri 'recipepuppy.com'
default_params onlyImages: "1"
format :json
#Uses request parameter to send to API
def self.for(q)
q = URI.escape(q)
p q
get("/api", query: {q: q})["results"]
end
end
puts Recipe.for "chocolate"
puts Recipe.for "apple pie"
I'm make a little program in sinatra and I'm wanted to perfom some dynamic call of post, with diynamic uri, so I make a Connexion class like this:
class Connexion
def initialize(path)
#path = path
end
def sinatraPost
post "/#{#path}" do
# some code
end
end
end
But when I'm launch sinatraPost, I've got this error:
undefined method `post' for #<Connexion:0x000000026206b8> (NoMethodError)
How can I call the sinatra post method in my class ?
EDIT: Okay ! So, I change my strategy, I have this following code:
class Webhook < Sinatra::Base
get '/:name' do
# compare with names array
end
end
Webhook.run!
Thank's to everyone !
It looks like you're going about this the wrong way. If you want to set up your app to receive a POST request, you'll need routing logic in your controller. Sinatra controllers normally look like this:
require 'sinatra'
get '/route1' do
# do stuff
end
post '/route2' do
# do stuff
end
If you're using a modular app, you'll want to have your app inherit from Sinatra::Base. See the Sinatra docs for more.
Making a post request is different, and doesn't rely on Sinatra methods.
require 'net/http'
uri = URI("http://google.com")
headers = {}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, headers)
response = http.request(request)
Or something like that. Good luck!
I'm attempting to build a gem for interacting w/ the Yahoo Placemaker API but I'm running into an issue. When I attempt to run the following code I get:
NameError: uninitialized constant Yahoo::Placemaker::Net
from /Users/Kyle/.rvm/gems/ruby-1.9.2-p290/gems/yahoo-placemaker-0.0.1/lib/yahoo-placemaker.rb:17:in `extract'
from (irb):4
from /Users/Kyle/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
yahoo-placemaker.rb
require "yahoo-placemaker/version"
require 'json'
require 'ostruct'
require 'net/http'
module Yahoo
module Placemaker
def self.extract (text = '')
host = 'wherein.yahooapis.com'
payload = {
'documentContent' => text,
'appid' => APP_ID,
'outputType' => 'json',
'documentType' => 'text/plain'
}
req = Net::HTTP::Post.new('/v1/document')
req.body = to_url_params(payload)
response = Net::HTTP.new(host).start do |http|
http.request(req)
end
json = JSON.parse(response.body)
Yahoo::Placemaker::Result.new(json)
end
end
end
I have yet to figure out how exactly constant name resolution works in Ruby (I think the rules are a bit messy here), but from my experience it could well be that Net is looked up in the current namespace instead of the global one. Try using the fully qualified name:
::Net::HTTP::Post.new
A similar problem could occur in this line:
Yahoo::Placemaker::Result
You should replace it with either ::Yahoo::Placemaker::Result or better Result (as it lives in the current namespace).
Try requiring net/http before. Ruby is falling back to find it in the module if it isn't defined.
require 'net/http'
I'm using the following code to download a page through a POST request:
require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.split("Date")
The URL I originally used has been replaced with example.com
It works great, but when I try to call split (last line) it returns an error:
<main>': undefined methodsplit' for # (NoMethodError)
I'm new to ruby, so I'm confused about this error.
The method you are calling returns a HTTPResponse object, so you need to leverage that object's methods to get what you want. maybe something like:
require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.body.split("Date")
Notice the body method.
Or, if you want to see all the data returned:
require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.inspect
Hope this helps!