In the soap response below (from SoapUI), under the parent SearchForReservationResponse tag, I am trying to pull out the values of Reservation id, Restaurant id and Location id with Savon 2.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchForReservationResponse xmlns="http://schemas.livebookings.net/OneFormat/Aggregator/Internal/1/0/">
<Reservation id="34639536" status="Confirmed">
<DiningDateAndTime>2015-07-01T17:00:00</DiningDateAndTime>
<Restaurant id="25200">
<Name>Eat Food - UK Demo Website - Bookatable.com</Name>
<Location id="35839">
<Name>Bar</Name>
</Location>
</Restaurant>
<Size>2</Size>
<Created>2015-07-01T13:22:17.41</Created>
<SessionId>DINNER</SessionId>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<ConfirmationNumber>JWRW5HR5</ConfirmationNumber>
<AllowedToCancelOnline>true</AllowedToCancelOnline>
<RestaurantPhoneNumber type="Main">+44 7951300529</RestaurantPhoneNumber>
</Reservation>
</SearchForReservationResponse>
</soap:Body>
</soap:Envelope>
Below is my attempt for trying to access Reservation id. After a lot of googling I found that the new Savon 2 syntax uses #attrib, but I keep getting errors as I think I am not using this Ruby nested hash syntax correctly - I find it very confusing and am fairly new to Ruby. If you could help me out here it would be much appreciated!
require 'savon'
class SearchReservation
attr_reader :reservation_id
def client
client = Savon.client(wsdl: "http://example-wsdl-url", follow_redirects: :follow_redirects)
end
def main_method(confirm_number, email)
message = {'ConfirmationNumber' => "JWRW5HR5", 'EMail' => "jon#" }
response = client.call(:search_for_reservation, message: message)
data = response.body(:search_for_reservation_response => { #attrib => {:reservation => :id} })
if data
#reservation_id = data[:id]
end
end
end
search = SearchReservation.new
puts search.main_method("JWRW5HR5", "jon#")
N.B. the email value jon# doesn't have to be a valid email address (used just for testing purposes) - it returns a valid response in SoapUI.
My last syntax error trace in the terminal/console:
/~/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/response.rb:36:in `body': wrong number of arguments (1 for 0) (ArgumentError)
from search.rb:13:in `main_method'
from search.rb:22:in `<main>'
My final solution pulling out all the values I need. Of note: even though dining_date_and_time was presented as xml when putting response.body, when it is extracted using #dining_date_and_time = data[:dining_date_and_time] the date and time values get printed cleanly. This is key once porting to a fully-fledged Rails app.
Despite all the discussion online about attributes, ids within an xml tag can be pulled by just ensuring you are far enough down the "tree" (ie. location id falls under location tag) and then specifying the key: in this case the api displays it as :#id for each nested case.
N.B, this only returns the last value called each time in console. But all values should be pulled in views once integrated into Rails. (Hopefully!)
require 'savon'
class SearchClass
def client
client = Savon.client(wsdl: "http://wsdl-example-url", follow_redirects: :follow_redirects)
end
def return_data(confirm_number, email)
message = {'ConfirmationNumber' => confirm_number, 'EMail' => email }
response = client.call(:search_for_reservation, message: message)
data = response.to_hash[:search_for_reservation_response][:reservation]
#reservation_id = data[:#id]
#dining_date_and_time = data[:dining_date_and_time]
#size = data[:size]
#session_id = data[:session_id]
#first_name = data[:first_name]
#last_name = data[:last_name]
#confirm_number = data[:confirmation_number]
#allowed_to_cancel_online = data[:allowed_to_cancel_online]
#restaurant_phone_number = data[:restaurant_phone_number]
data2 = response.to_hash[:search_for_reservation_response][:reservation][:restaurant]
#restaurant_id = data2[:#id]
#restaurant_name = data2[:name]
data3 = response.to_hash[:search_for_reservation_response][:reservation][:restaurant][:location]
#location_id = data3[:#id]
#location_name = data3[:name]
end
end
search = SearchClass.new
puts search.return_data("JWRW5HR5", "jon#")
Related
I am using the updateCustomerPaymentProfile endpoint to try to update a payment profile. This works well, except for the presence of one field: defaultPaymentProfile.
There are two different issues.
1. Authorize.Net Ruby SDK typing issue
The docs say defaultPaymentProfile is an expected field, but the type does not allow it in ruby SDK, see in the official source code:
https://github.com/AuthorizeNet/sdk-ruby/blob/002019e03a94ef582aa82983edf6a7a1a22b2316/lib/authorize...
I opened a Github issue about this.
I then monkey patched the type as following:
module AuthorizeNet::API
class CustomerPaymentProfileExType
xml_accessor :defaultPaymentProfile
end
end
After that, it accepts to send the request but I receive an error response as following:
AuthorizeNetException: E00003: The element 'paymentProfile' in namespace
'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element
'defaultPaymentProfile' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd
Which is the second issue...
2. Authorize.Net API not accepting the defaultPaymentProfile when updating a payment profile
For the record, I dumped the raw XML that is sent to the API once I patched the SDK to be able to actually reach the API:
<updateCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>REDACTED</name>
<transactionKey>REDACTED</transactionKey>
</merchantAuthentication>
<customerProfileId>REDACTED</customerProfileId>
<paymentProfile>
<customerType>individual</customerType>
<billTo>
<firstName>REDACTED</firstName>
<lastName>REDACTED</lastName>
<address>REDACTED</address>
<city>REDACTED</city>
<state>REDACTED</state>
<zip>REDACTED</zip>
<country>REDACTED</country>
<phoneNumber>REDACTED</phoneNumber>
</billTo>
<payment>
<creditCard>
<cardNumber>XXXX4242</cardNumber>
<expirationDate>2022-03</expirationDate>
</creditCard>
</payment>
<customerPaymentProfileId>REDACTED</customerPaymentProfileId>
<!-- This XML passes fine without the line below -->
<defaultPaymentProfile>true</defaultPaymentProfile>
</paymentProfile>
<validationMode>liveMode</validationMode>
</updateCustomerPaymentProfileRequest>
This looks exactly the same as the request suggested by the official API docs, yet, the server respond with a E00003 error that I already shared above.
Notes
As a reference, the block of ruby code that I am using:
profile = AuthorizeNet::API::CustomerPaymentProfileExType.new
profile.customerPaymentProfileId = current_profile.customerPaymentProfileId
profile.billTo = billTo
profile.payment = AuthorizeNet::API::PaymentType.new(
AuthorizeNet::API::CreditCardType.new(
cc_data.cardNumber, cc_data.expirationDate
)
)
profile.taxId = user.tax_id if user.tax_id
profile.defaultPaymentProfile = true
profile.customerType = 'individual'
request = AuthorizeNet::API::UpdateCustomerPaymentProfileRequest.new
request.paymentProfile = profile
request.customerProfileId = customer_profile_id
request.validationMode = AuthorizeNet::API::ValidationModeEnum::LiveMode
response = transaction.update_customer_payment_profile(request)
What am I doing wrong?
The order of the elements matter. Move
<defaultPaymentProfile>true</defaultPaymentProfile>`
above
<customerPaymentProfileId>REDACTED</customerPaymentProfileId>`
<updateCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>REDACTED</name>
<transactionKey>REDACTED</transactionKey>
</merchantAuthentication>
<customerProfileId>REDACTED</customerProfileId>
<paymentProfile>
<customerType>individual</customerType>
<billTo>
<firstName>REDACTED</firstName>
<lastName>REDACTED</lastName>
<address>REDACTED</address>
<city>REDACTED</city>
<state>REDACTED</state>
<zip>REDACTED</zip>
<country>REDACTED</country>
<phoneNumber>REDACTED</phoneNumber>
</billTo>
<payment>
<creditCard>
<cardNumber>XXXX4242</cardNumber>
<expirationDate>2022-03</expirationDate>
</creditCard>
</payment>
<defaultPaymentProfile>true</defaultPaymentProfile>
<customerPaymentProfileId>REDACTED</customerPaymentProfileId>
</paymentProfile>
<validationMode>liveMode</validationMode>
</updateCustomerPaymentProfileRequest>
Thanks to #John Conde, I could fix the issue. The request payload is refused because the properties are not in the right order. 🤷
If you want to set an existing payment profile as the default one using the ruby SDK, you can do so by defining yourself the correct type, as following:
module AuthorizeNet::API
class CustomerPaymentProfileExTypePatched
include ROXML
xml_accessor :customerType
xml_accessor :billTo, as: AuthorizeNet::API::CustomerAddressType
xml_accessor :payment, as: AuthorizeNet::API::PaymentType
xml_accessor :driversLicense, as: AuthorizeNet::API::DriversLicenseType
xml_accessor :taxId
xml_accessor :defaultPaymentProfile
xml_accessor :subsequentAuthInformation
xml_accessor :customerPaymentProfileId
def initialize(customerType = nil, billTo = nil, payment = nil, driversLicense = nil, taxId = nil, defaultPaymentProfile = nil, subsequentAuthInformation = nil, customerPaymentProfileId = nil)
#customerType = customerType
#billTo = billTo
#payment = payment
#driversLicense = driversLicense
#taxId = taxId
#defaultPaymentProfile = defaultPaymentProfile
#subsequentAuthInformation = subsequentAuthInformation
#customerPaymentProfileId = customerPaymentProfileId
end
end
end
Then you can just use this type in your code:
profile = AuthorizeNet::API::CustomerPaymentProfileExTypePatched.new
So here's what my code (pretty much) looks like to create a message using the google-api-ruby-client:
service ||= Google::Apis::GmailV1::GmailService.new
message = RMail::Message.new
message.header['To'] = params[:gmail][:to]
message.header['From'] = current_user_google_user_id
message.header['Subject'] = params[:gmail][:subject]
message.header['Subject'] = params[:gmail][:subject]
message.body = params[:gmail][:body]
service.send_user_message(
current_user_google_user_id,
upload_source: StringIO.new(message.to_s),
content_type: 'message/rfc822',
thread_id: params[:gmail][:thread_id]
)
It obviously fails because of where I have thread_id. If I remove that line, things work fine, but I'm not able to keep things scoped to a thread. How should I be passing the thread ID to the GmailService?
Looking at the source on GitHub for send_user_message shows that it doesn't take a thread_id as a parameter. However the Message class does have it as an attribute.
So perhaps trying this should work:
service ||= Google::Apis::GmailV1::GmailService.new
message = RMail::Message.new
message.header['To'] = params[:gmail][:to]
message.header['From'] = current_user_google_user_id
message.header['Subject'] = params[:gmail][:subject]
message.header['Subject'] = params[:gmail][:subject]
message.body = params[:gmail][:body]
message.thread_id = params[:gmail][:thread_id]
service.send_user_message(
current_user_google_user_id,
upload_source: StringIO.new(message.to_s),
content_type: 'message/rfc822'
)
I have been trying for days to pull down activity data from the Withings API using the OAuth Ruby gem. Regardless of what method I try I consistently get back a 503 error response (not enough params) even though I copied the example URI from the documentation, having of course swapped out the userid. Has anybody had any luck with this in the past. I hope it is just something stupid I am doing.
class Withings
API_KEY = 'REMOVED'
API_SECRET = 'REMOVED'
CONFIGURATION = { site: 'https://oauth.withings.com', request_token_path: '/account/request_token',
access_token_path: '/account/access_token', authorize_path: '/account/authorize' }
before do
#consumer = OAuth::Consumer.new API_KEY, API_SECRET, CONFIGURATION
#base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['SCRIPT_NAME']}"
end
get '/' do
#request_token = #consumer.get_request_token oauth_callback: "#{#base_url}/access_token"
session[:token] = #request_token.token
session[:secret] = #request_token.secret
redirect #request_token.authorize_url
end
get '/access_token' do
#request_token = OAuth::RequestToken.new #consumer, session[:token], session[:secret]
#access_token = #request_token.get_access_token oauth_verifier: params[:oauth_verifier]
session[:token] = #access_token.token
session[:secret] = #access_token.secret
session[:userid] = params[:userid]
redirect "#{#base_url}/activity"
end
get '/activity' do
#access_token = OAuth::AccessToken.new #consumer, session[:token], session[:secret]
response = #access_token.get("http://wbsapi.withings.net/v2/measure?action=getactivity&userid=#{session[:userid]}&startdateymd=2014-01-01&enddateymd=2014-05-09")
JSON.parse(response.body)
end
end
For other API endpoints I get an error response of 247 - The userid provided is absent, or incorrect. This is really frustrating. Thanks
So I figured out the answer after copious amount of Googleing and grasping a better understanding of both the Withings API and the OAuth library I was using. Basically Withings uses query strings to pass in API parameters. I though I was going about passing these parameters correctly when I was making API calls, but apparently I needed to explicitly set the OAuth library to use the query string scheme, like so
http_method: :get, scheme: :query_string
This is appended to my OAuth consumer configuration and all worked fine immediately.
I'm green when it comes to Ruby. Right now I'm mucking about with a script which connects to the Terremark eCloud API Explorer. I'm trying to use the httpclient gem, but I'm a bit confused as to how I'm supposed to construct my client.
#!/usr/bin/ruby
require "httpclient"
require 'base64'
require 'hmac-sha1'
require 'openssl'
# Method definitions
def get_date
# Get the time and date in the necessary format
result = Time.now.strftime('%a, %d %b %Y %H:%M:%S GMT')
end
def get_signature(action,date,headers,resource,user,pass)
string_to_sign = "#{action}
#{date}
#{headers}
#{resource}\n"
return Base64.encode64(OpenSSL::HMAC.digest('sha1', "#{user}:#{pass}", "#{string_to_sign}"))
end
# Initial variables
date = get_date
domain = "https://services.enterprisecloud.terremark.com"
password = 'password'
query = {}
tmrk_headers = Hash.new
tmrk_headers['x-tmrk-date: '] = date
tmrk_headers['x-tmrk-version: '] = '2013-06-01'
uri = '/cloudapi/spec/networks/environments/1'
url = "#{domain}#{uri}"
username = 'user#terremark.com'
verb = 'GET'
signature = get_signature(verb,date,tmrk_headers,uri,username,password)
tmrk_headers['Authorization: '] = "Basic \"#{signature}\""
puts signature
client = HTTPClient.new
client.get_content(url,query,tmrk_headers)
EDIT: This is no longer valid as I've moved beyond this error with some help:
Right now I'm not concerned about seeing what is returned from the connection. I'm just looking to create an error-free run. For instance, if I run the script without the client.get_content line it will return to a prompt without issue (giving me the impression that everything ran cleanly, if not uselessly).
How am I supposed to construct this? The httpclient documentation uses the example with external headers:
extheader = [['Accept', 'image/jpeg'], ['Accept', 'image/png']]
clnt.get_content(uri, query, extheader)
I'm making the assumption that the query is the URI that I've defined.
In all reality, it isn't set up right in the first place. I need to be able to include the string in the auth_header variable in the string to be signed but the signature is actually part of the variable. I've obviously created a hole in that regard.
Any assistance with this will be more than appreciated.
EDIT2: Removed strace pastebin. Adding Ruby backtrace:
/home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:1023:in `create_request': undefined method `each' for #<String:0x0000000207d1e8> (NoMethodError)
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:884:in `do_request'
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:959:in `follow_redirect'
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:594:in `get_content'
from ./test.rb:42:in `<main>'
EDIT3: Updated script; adding further backtrace after making necessary script modifications:
/
home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:975:in `success_content': unexpected response: #<HTTP::Message::Headers:0x00000001dddc58 #http_version="1.1", #body_size=0, #chunked=false, #request_method="GET", #request_uri=#<URI::HTTPS:0x00000001ddecc0 URL:https://services.enterprisecloud.terremark.com/cloudapi/spec/networks/environments/1>, #request_query={}, #request_absolute_uri=nil, #status_code=400, #reason_phrase="Bad Request", #body_type=nil, #body_charset=nil, #body_date=nil, #body_encoding=#<Encoding:US-ASCII>, #is_request=false, #header_item=[["Content-Type", "text/html; charset=us-ascii"], ["Server", "Microsoft-HTTPAPI/2.0"], ["Date", "Thu, 27 Mar 2014 23:12:53 GMT"], ["Connection", "close"], ["Content-Length", "339"]], #dumped=false> (HTTPClient::BadResponseError)
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:594:in `get_content'
from ./test.rb:52:in `<main>'
The issue that you're having as stated by your backtrace
/home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:1023:in `create_request': undefined method `each' for #<String:0x0000000207d1e8> (NoMethodError)
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:884:in `do_request'
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:959:in `follow_redirect'
from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:594:in `get_content'
from ./test.rb:42:in `<main>'
is that it seems like you're passing a String object to one of the arguments in get_content where it expects an object that responds to the method each.
From looking at the documentation of httpclient#get_content http://www.ruby-doc.org/gems/docs/h/httpclient-xaop-2.1.6/HTTPClient.html#method-i-get_content
It expects the second parameter to be a Hash or Array of arguments
From your code sample and showing only the relevant parts
uri = '/cloudapi/spec/networks/environments/1'
url = "https://services.enterprisecloud.terremark.com"
tmrk_headers = "x-tmrk-date:\"#{date}\"\nx-tmrk-version:2014-01-01"
auth_header = "Authorization: CloudApi AccessKey=\"#{access_key}\" SignatureType=\"HmacSHA1\" Signature=\"#{signature}\""
full_header = "#{tmrk_headers}\n#{auth_header}"
client = HTTPClient.new
client.get_content(url,uri,full_header)
There are two things that I see wrong with your code.
You're passing in a String value for the query. Specifically, you're passing in uri which has a value of what I'm assuming is the path that you want to hit.
For the extra headers parameter, you're passing in a String value which is in the full_header
What you need to do in order to fix this is pass in the full url for the first parameter.
This means it should look something like this:
url = "https://services.enterprisecloud.terremark.com/cloudapi/spec/networks/environments/1"
query = {} # if you have any parameters to pass in they should be here.
headers = {
"x-tmrk-date" => date, "x-tmrk-version" => "2014-01-01",
"Authorization" => "CloudApi AccessKey=#{access_key} SignatureType=HmacSHA1 Signature=#{signature}"
}
client = HTTPClient.new
client.get_content(url, query, headers)
I successfully setup a connection to my Rally project, but am not able to programatically set the "Submitted By" field when opening defects. I am misnaming the parameter, or it is not settable via the REST API. Any help on querying modifiable parameters, link to attribute names, or code to set the "Submitted By" parameter would be greatly appreciated.
My goal is to have a webpage where folks submit defects or stories, along with their email, and use that to populate their username in the "Submitted By" field. My current setup can open Stories and Defects but only anonymously since the second to last line has no effect.
I am using the Ruby API and the following is the ruby code that gets invoked when a user clicks on a button on the main page, which redirects them to localhost:4567/rally_defect where Sinatra is listening.
require 'rubygems'
require 'rally_rest_api'
require 'sinatra'
custom_headers = CustomHttpHeader.new
custom_headers.name = 'Mail 2 Rally'
custom_headers.version = '0.1'
custom_headers.vendor = 'Rally Software'
rally_server = 'https://rally1.rallydev.com/slm'
rally_username = <insert rally username>
rally_pwd = <insert rally password>
rally = RallyRestAPI.new(:base_url => rally_server,
:username => rally_username,
:password => rally_pwd,
:http_headers => custom_headers)
get '/rally_defect' do
rally.create(:defect, :name => "Test Defect",
:description => "This is a test",
:SubmittedBy => "test")
end
If you're getting started building this integration, I'd strongly recommend using rally_api instead of rally_rest_api.
rally_rest_api is in the process of being deprecated for a number of issues around performance and stability. There won't be any further development on rally_rest_api moving forward.
You can find documentation on rally_api here:
https://developer.help.rallydev.com/ruby-toolkit-rally-rest-api-json
rally_api does require Ruby 1.9.2 or higher.
rally_api's syntax is quite similar to rally_rest_api. The rally_api version of what you're attempting above would look something like the code sample below. Note that any "set-able" parameter can be set simply by including its name in the object hash and setting that to a value.
The best place to look for the Rally Webservices API object model, including Artifact attributes, allowed values, and whether they are write-able is the Webservices API documentation:
https://rally1.rallydev.com/slm/doc/webservice
When an attribute of a Rally Artifact is itself a Rally Object like a User, for example, either lookup the Object in Rally first (the example below does this), or, set the value to the REST URL reference to the object in Rally. For instance, if you know that user#company.com has ObjectID 12345678910 in Rally, then you can do:
user_ref = "/user/12345678910"
new_defect["SubmittedBy"] = user_ref
The code below shows the way to do a lookup into Rally to get the User object, given an email-formatted UserID, since I'm assuming you'll probably need to accomodate multiple different users.
require 'rubygems'
require 'rally_api'
require 'sinatra'
#Setting custom headers
headers = RallyAPI::CustomHttpHeader.new()
headers.name = 'Mail 2 Rally'
headers.vendor = "My Company"
headers.version = "0.1"
# Rally credentials
rally_username = <insert rally username>
rally_pwd = <insert rally password>
rally_server = 'https://rally1.rallydev.com/slm'
# Rally REST API config
config = {:base_url => rally_server}
config[:username] = rally_username
config[:password] = rally_pwd
config[:workspace] = "Workspace Name"
config[:project] = "Project Name"
config[:headers] = headers #from RallyAPI::CustomHttpHeader.new()
# New up rally connection config
#rally = RallyAPI::RallyRestJson.new(config)
# Lookup UserID for SubmittedBY
submitted_by_user_id = "user#company.com"
user_query = RallyAPI::RallyQuery.new()
user_query.type = :user
user_query.fetch = "ObjectID,UserName,DisplayName"
user_query.order = "UserName Asc"
user_query.query_string = "(UserName = \"#{submitted_by_user_id}\")"
# Query for user
user_query_results = #rally.find(user_query)
submitted_by_user = user_query_results.first
# Create the Defect
new_defect = {}
new_defect["Name"] = "Test Defect"
new_defect["Description"] = "This is a test"
new_defect["SubmittedBy"] = submitted_by_user
new_defect_create_result = #rally.create(:defect, new_defect)
puts "New Defect created FormattedID: #{new_defect_create_result.FormattedID}"