Performing an HTTP PATCH using Ruby curb - ruby

I'm trying to do an HTTP PATCH using curb. Looking through the code, there doesn't seem to be a method exposed for this. Is there any way to use curb to do a PATCH? If not, what other libraries or methods are there in Ruby to accomplish this?

With curb latest version (v0.8.1) PATCH is supported even though it is not explicitly available within the Curl::Easy interface (see lib/curl/easy.rb).
You can find a shortcut method here:
# see lib/curl.rb
module Curl
# ...
def self.patch(url, params={}, &block)
http :PATCH, url, postalize(params), nil, &block
end
# ...
end
With it you can perform a PATCH request as follow:
curl = Curl.patch("http://www.example.com/baz", {:foo => "bar"})
Under the hood, the PATCH verb is simply passed to the easy interface as follow:
curl = Curl::Easy.new(url)
# `http` is a method implemented within the C extensions of curb
# see `ruby_curl_easy_perform_verb_str`. It allows to set the HTTP
# verb by calling `curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, verb)`
# and perform the request right after
curl.http(:PATCH)

Related

Manuals of Ruby packages listing their methods?

For example, I tried to find a manual for OAuth package here but I get Ooops page missing here. I tried rim utility as instructed in Manual pages for Ruby functions from command line but version not documented
= OAuth
(from gem oauth-0.5.3)
------------------------------------------------------------------------------
------------------------------------------------------------------------------
= Constants:
OUT_OF_BAND:
request tokens are passed between the consumer and the provider out of band
(i.e. callbacks cannot be used), per section 6.1.1
PARAMETERS:
required parameters, per sections 6.1.1, 6.3.1, and 7
RESERVED_CHARACTERS:
reserved character regexp, per section 5.1
VERSION:
[not documented]
Because of missing manuals, I have tried to find tools to list methods of packages like in IDE like Dropdown list for ruby methods of a package in RubyMine IDE? and Vim plugin to autocomplete method names of Ruby packages. I haven't yet found a solution there and, here, I want to keep the focus on the manuals and proper ways to find them in Ruby.
Is there some central place for Ruby package manuals like R's cran? And if not, how do authors of public Ruby packages usually document their plugins? Is the easiest way to see which methods each package has to read the source?
This is not the easiest way but you can unpack any gem file corresponding to a package where you may be able to find demonstrations about the usage of each package.
Example about OAuth
You can download the gem file and unpack it to read the source in the following way
wget https://rubygems.org/downloads/oauth-0.5.3.gem
gem unpack oauth-0.5.3.gem
where you find the README with a demonstration about the usage.
README.rdoc
== Demonstration of usage
We need to specify the oauth_callback url explicitly, otherwise it defaults to "oob" (Out of Band)
#callback_url = "http://127.0.0.1:3000/oauth/callback"
Create a new consumer instance by passing it a configuration hash:
#consumer = OAuth::Consumer.new("key","secret", :site => "https://agree2")
Start the process by requesting a token
#request_token = #consumer.get_request_token(:oauth_callback => #callback_url)
session[:token] = request_token.token
session[:token_secret] = request_token.secret
redirect_to #request_token.authorize_url(:oauth_callback => #callback_url)
When user returns create an access_token
hash = { oauth_token: session[:token], oauth_token_secret: session[:token_secret]}
request_token = OAuth::RequestToken.from_hash(#consumer, hash)
#access_token = #request_token.get_access_token
#photos = #access_token.get('/photos.xml')
Now that you have an access token, you can use Typhoeus to interact with the OAuth provider if you choose.
require 'oauth/request_proxy/typhoeus_request'
oauth_params = {:consumer => oauth_consumer, :token => access_token}
hydra = Typhoeus::Hydra.new
req = Typhoeus::Request.new(uri, options) # :method needs to be specified in options
oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => uri))
req.options[:headers].merge!({"Authorization" => oauth_helper.header}) # Signs the request
hydra.queue(req)
hydra.run
#response = req.response
All functions in Ruby OAuth find . -iname "*.rb"
./lib/oauth/cli/authorize_command.rb
./lib/oauth/cli/base_command.rb
./lib/oauth/cli/help_command.rb
./lib/oauth/cli/query_command.rb
./lib/oauth/cli/sign_command.rb
./lib/oauth/cli/version_command.rb
./lib/oauth/cli.rb
./lib/oauth/client/action_controller_request.rb
./lib/oauth/client/em_http.rb
./lib/oauth/client/helper.rb
./lib/oauth/client/net_http.rb
./lib/oauth/client.rb
./lib/oauth/consumer.rb
./lib/oauth/core_ext.rb
./lib/oauth/errors/error.rb
./lib/oauth/errors/problem.rb
./lib/oauth/errors/unauthorized.rb
./lib/oauth/errors.rb
./lib/oauth/helper.rb
./lib/oauth/oauth.rb
./lib/oauth/oauth_test_helper.rb
./lib/oauth/request_proxy/action_controller_request.rb
./lib/oauth/request_proxy/base.rb
./lib/oauth/request_proxy/curb_request.rb
./lib/oauth/request_proxy/em_http_request.rb
./lib/oauth/request_proxy/jabber_request.rb
./lib/oauth/request_proxy/mock_request.rb
./lib/oauth/request_proxy/net_http.rb
./lib/oauth/request_proxy/rack_request.rb
./lib/oauth/request_proxy/rest_client_request.rb
./lib/oauth/request_proxy/typhoeus_request.rb
./lib/oauth/request_proxy.rb
./lib/oauth/server.rb
./lib/oauth/signature/base.rb
./lib/oauth/signature/hmac/sha1.rb
./lib/oauth/signature/plaintext.rb
./lib/oauth/signature/rsa/sha1.rb
./lib/oauth/signature.rb
./lib/oauth/token.rb
./lib/oauth/tokens/access_token.rb
./lib/oauth/tokens/consumer_token.rb
./lib/oauth/tokens/request_token.rb
./lib/oauth/tokens/server_token.rb
./lib/oauth/tokens/token.rb
./lib/oauth/version.rb
./lib/oauth.rb

How do I allow the request with this path to pass the webmock?

I want to mock NetHTTP requests but some should be allowed.
require "open-uri"
require "webmock"
WebMock.enable!
When I declare an allowed request like this:
WebMock.disable_net_connect! allow: /\Ahttps:\/\/graph\.facebook\.com\/v2\.8\/debug_token\?access_token=/
and call:
open("https://graph.facebook.com/v2.8/debug_token?access_token=qwerty", &:read)
I get this:
HTTP connections are disabled. Unregistered request: GET https://graph.facebook.com/v2.8/debug_token?access_token=qwerty with headers ...
You can stub this request with the following snippet:
stub_request(:get, "https://graph.facebook.com/v2.8/debug_token?access_token=qwerty").
...
It also fails with regex like this:
/\Ahttps:\/\/graph\.facebook\.com\//
but does not fail with this:
/\Ahttps:\/\/graph\.facebook\.com/
How to allow the full regex that I initially wanted? Why even the \/ after hostname fails matching?
Your version of Webmock (1.22.6) has a bug when using open-uri:
https://github.com/bblimke/webmock/issues/600
You can fix this bug by updating your webmock gem to the latest 1.x or 2.x

Get chromes console log via Ruby WebDriver

This question has been previously answered in Java (Get chrome's console log)
However, I am using the Ruby bindings and was wondering if that provided similar functionality?
I have looked at the Ruby source code but cannot see any mention or reference to LoggingPreferences.
By the way, I am using RemoteWebDriver and passing in a desired capability object. Presumably I want to set the logging preferences in that object, but I am struggling to see where.
Apologies for late response.
I originally achieved it by adding the following to Webdriver;
module Selenium
module WebDriver
class Options
#
# Returns the available logs for this webDriver instance
#
def available_log_types
#bridge.getAvailableLogTypes
end
#
# Returns the requested log
#
# #param type [String] The required log type
#
# #return [Array] An array of log entries
#
def get_log(type)
#bridge.getLog(type)
end
end
end
end
When "required" this resulted in the following being supported;
driver.manage.get_log(:browser)
However, Version 2.38 of the selenium ruby gem exposes the logging API (although experimental).
http://selenium.googlecode.com/git/rb/CHANGES
https://code.google.com/p/selenium/wiki/Logging
Therefore, from 2.38 onwards the following should work WITHOUT the above extension;
driver.manage.logs.get :browser
You can use this code as well
require 'selenium-webdriver'
console_logs = #browser.driver.manage.logs.get(:browser)
puts = console_logs
ReportBuilder.build_report
Use command -f json -o my_report_file.json to generate reports.

undefined method `configure' for Savon:Module

I'm getting the above error in a gem with this code snippet
Savon.configure do |config|
config.log = false
config.log_level = :error
HTTPI.log = false
end
This code used to pass in past runs on Travis, so I'm not sure why this changed when I altered the Readme.
Part of this confusion comes from my situation--inheriting a gem to maintain--along with this line in the gemspec:
gem.add_dependency 'savon'
There's no version number specified, so the newest run switched over to using Savon 2, which ditched the Savon.configure global behavior. If you're in the same boat as me, changing this line to the last pre-2.0 version of Savon will resolve the issue:
gem.add_dependency 'savon', '~>1.2.0'
Then bundle install and you should be good.
Or you want to upgrade your code. I know I do.
Savon.configure was removed from Savon 2.0 because the "problem was global state". The quickest way to keep the behavior the same in your app would be to define a app-level global hash in the same place. You'd then pass this hash into every Savon.client call you make. For instance:
# Where Savon.configure was called
APP_OPTS = {
# disable request logging, silences HTTPI as well
log: false,
# Don't log Laundry xmls to STDOUT
log_level: :error,
#... etc
}
# Elsewhere
#client = Savon::Client.new(APP_OPTS)
I'd consider this a starting point to migrating to the 2.0 configuration style. Ideally, you should always consider the client-specific 2.0 options available when initializing each Savon client.

Sinatra - Accessing request in Rack::ResponseHeaders

I want to access request in the Rack::ResponseHeaders. I am using Sinatra in my app.
Below is my code:
use Rack::ResponseHeaders do |headers|
# Manipulation of request variables.
# Setting request headers.
end
The question is that in order to manipulate variables in request, I need to have the request variable first.
Please suggest.
First thing is, you need to install the gem rack-contrib via rubygems:
$ gem install rack-contrib
This gem contains contributed rack utilities. Then you need to require this gem in your app:
require 'rack/contrib'
It may be enough to only require the response headers utility (not tested):
require 'rack/contrib/response_headers'
Then you can use this utility to tap into the headers, for example:
use Rack::ResponseHeaders do |headers| # tap into headers
unless headers['cache-control'] # if header not set,
headers['cache-control'] = "public, max-age=1800" # set it to ...
end
end
Let me know whether this is working for you.

Resources