Confused about soap4r custom headers - ruby

I am trying to set up a web service to Celltrust's SMS Gateway. I have their SDK and I'm trying to use soap4r to create the custom headers that it needs. I'm not sure exactly what I need to do to get it in their format, since they don't provide me with an actual XML document showing what they need; all they say is that in the header you need to give a Username and Password.
Given the following PHP example code, how would I do this in Ruby?
$URN = "urn:notify.soap.primemessage.com";
$WSDL="http://pmgateway.net/pmws/services/TxTMessageService?wsdl";
//SOAP elements (don’t edit, and case sensitive!)
$CTUSERNAME = "Username";
$CTPASSWORD = "Password";
$CTNICKNAME = "nickname";
$DESTINATION = "destination";
$MESSAGE = "message";
$USER_ID = "USERNAME"; //your username at CellTrust
$NICKNAME = "NICKNAME"; //your nickname at Celltrust
$PASSWORD = "PASSWORD"; //your password at Celltrust
//create user and password SOAP header elements
$UserHdr = new SoapHeader( $URN, $CTUSERNAME, $USER_ID, false);
$PassHdr = new SoapHeader( $URN, $CTPASSWORD, $PASSWORD, false);
// call the method here
I've created a derivative class of SOAP::Header::SimpleHandler. What I'm not sure of is whether I need to specify the namespace twice (once for Username, once for Password) or how it is being generated. The documentation doesn't give any name for the namespace and I'm not sure how to do the call to "new SoapHeader" in Ruby properly.
Can anyone help me with this?

You need to create a subclass of SOAP::Header::SimpleHandler, as you mention:
class MyHandler < SOAP::Header::SimpleHandler
def initialize(namespace, name, value)
super(XSD::QName.new(namespace, name))
#value = value
end
def on_simple_outbound
#value
end
end
Then just do:
namespace = 'INSERT_NAMESPACE_URL_HERE'
# 'driver' below is the Soap4R driver for the service
driver.headerhandler << MyHandler.new(namespace, 'Username', 'INSERT_USERNAME_HERE')
driver.headerhandler << MyHandler.new(namespace, 'Password', 'INSERT_PASSWORD_HERE')

Related

Laravel easier way to save multiple input fields to database

I am trying to save a long form data to database. Till now i am getting the form value from request object and setting it to database model.
This works perfectly fine. But I want to know if there is another way to initialise the model efficiently without need to set each value. My model has one to one relation.
I have been doing like this. But i don't think this is the right way to do
//Student details
$studentDetail->student_first_name = $request->input('studentFirstName');
$studentDetail->student_last_name = $request->input('studentLastName');
$studentDetail->student_phone_number = $request->input('studentPhoneNumber');
$studentDetail->student_date_of_birth = $request->input('studentDOB');
$studentDetail->student_email = $request->input('studentEmail');
$studentDetail->save();
$studentAddress = new Address();
$studentAddress->address_1 = $request->input('studentAddress1');
$studentAddress->address_2 = $request->input('studentAddress2');
$studentAddress->city = $request->input('studentCity');
$studentAddress->state = $request->input('studentState');
$studentAddress->country = $request->input('studentCountry');
$studentAddress->post_code = $request->input('studentPostCode');
$studentDetail->addresses()->save($studentAddress);
$visaDetails = new Visa();
$visaDetails->passport_number = $request->input("visaPassportNumber");
$visaDetails->visa_number = $request->input("visaVisaNumber");
$visaDetails->visa_class = $request->input("visaVisaClass");
$visaDetails->visa_grant_date = $request->input("visaVisaGrantDate");
$visaDetails->visa_expiry_date = $request->input("visaVisaExpiryDate");
$studentDetail->visaDetails()->save($visaDetails);
//
$instituteDetails = new Institute();
$instituteDetails->institute_name = $request->input("instituteName");
$instituteDetails->institute_location = $request->input("instituteLocation");
$instituteDetails->institute_phone1 = $request->input("institutePhone1");
$instituteDetails->institute_phone2 = $request->input("institutePhone2");
$instituteDetails->institute_email = $request->input("instituteEmail");
// dd($instituteDetails->courses);
$courseDetails = new Course();
$courseDetails->course_level = $request->input("courseLevel");
$courseDetails->course_name = $request->input("courseName");
$courseDetails->course_fee = $request->input("courseFee");
$courseDetails->course_concession_fee = $request->input("courseConcessionFee");
$courseDetails->course_duration = $request->input("courseDuration");
$courseDetails->course_commencement_date = $request->input("courseCommencementDate");
$studentDetail->instituteDetails()->save($instituteDetails);
$instituteDetails->courses()->save($courseDetails);
Any idea on making this process faster??
Simply set create your models using mass assignment, so:
So in your model StudentDetail:
class StudentDetail{
protected $fillable = [
'student_first_name',
'student_last_name',
'student_phone_number',
'student_date_of_birth',
'student_email',
];
//...
//... rest of your model
}
Then tweak your HTML inputs to have in their names the user array like so for example:
<input type="text" id="foo" name="student[student_first_name]">
<input type="text" id="foo" name="student[student_last_name]">
.....
Tip: for validation, you have to treat it with dot notation, so your rule could be:
'student.student_first_name' => 'required|humanName|string|max:255',
Now simply do the following in your controller:
$studentDetail = StudentDetail::create($request->input('student'));
Now you made do the same for your address and other models.
The GIST: After mass assignment enabled for your models you could end up having ONLY the following couple lines of code doing it all for you and it's way more fun and full of dynamism ;) IMHO!
$relatedModels = ['Address', 'Visa', 'Institute', 'Course'];
foreach ($relatedModels as $relatedModel) {
$relatedModelClass = 'App\\'.$relatedModel; //adjust the namespace of your models here.
$ormRelatedModel = $relatedModelClass::create(strtolower($request->input($relatedModel)));
$studentDetail->{strtolower(str_plural($relatedModel)) . 'Details'}()->save($ormRelatedModel);
}
please note that in this case your relations names should be changed a bit like addresses function within your StudentDetail class/model should be changed to addressesDetails or just remove the .'Details' from my sample code above and remove it from your other relations names, i.e: change instituteDetails() to institute(). and make the relation names plural please!
I just tested it and it's working,
Cheers!

Magento Observer - Getting Newsletter Subscribtion from Order Object

What can I call from an $observer object to determine if the guest or customer clicked the subscribe checkbox on checkout? So far I have this:
public function collectCustomerData($observer)
{
$this->observer = $observer;
$this->_order = $this->observer->getEvent()->getOrder();
$this->_address = $this->_order->getShippingAddress();
$this->data['first_name'] = $this->_address->getFirstname();
$this->data['last_name'] = $this->_address->getLastname();
$this->data['city'] = $this->_address->getCity();
$this->data['email'] = $this->_order->getCustomerEmail();
}
but I need to add $this->data['is_newsletter'] from either $this->_order(Mage_Sales_Model_Order) or $this->_address(Mage_Sales_Model_Order_Address), or pull in another model that has that information through static factory methods such as Mage::getModel() if I need to
I figured it out, I had to bring the newsletter/subscriber model into the equation. The following code returned either 1 or null:
$email = $this->_order->getCustomerEmail();
$isNewsletter = Mage::getModel('newsletter/subscriber')
->loadByEmail($email)
->getSubscriberStatus();
Based on that I was able to set the opt in flag appropriately:
$this->data['is_newsletter'] = $isNewsletter;

Codeigniter sending out multiple emails

I am using codeigniters email class inside an Expression Engine plugin (EE runs off CI).
For some reason each time the plugin is run it sends out 2 emails, instead of 1. The emails are identical.
{exp:cdwd_emailer:questionnaire type="{segment_3}" entry_id="{segment_4}"}
Here is the function being called by the above.
public function questionnaire() {
$type = $this->EE->TMPL->fetch_param('type');
$typeLower = str_replace("-", " ", $type);
$typeUpper = ucwords($typeLower);
print_r($type);
$entry_id = $this->EE->TMPL->fetch_param('entry_id');
$subject = $typeUpper.' Questionnaire Submission';
$fromEmail = 'email#email.com';
$fromName = 'Test Name';
$toEmail = 'email#email.com';
$message = '
<p>A new '.$typeLower.' has been submitted.</p>
<p>Please click here to view this submission</p>
';
$this->EE->load->library('email');
$this->EE->email->set_mailtype("html");
$this->EE->email->from($fromEmail, $fromName);
$this->EE->email->to($toEmail);
$this->EE->email->subject($subject);
$this->EE->email->message($message);
$this->EE->email->send();
}
Can anyone tell me why? I can't figure it out. I printed out the contents of the type and entry_id params to check only 1 of each are being collected.
Thanks
I think you have to clear you email object after sending mail.
As per Expression Engine, you have to call:
ee()->email->clear();
For your case:
$this->EE->email->clear();

Ruby Quick Class Instance Scope

I have a quick problem that probably comes down to something stupid. I have a class that extends OAuth::AccessToken and uses instance variables (#) so that each time it constructs an object, those variables will be unique that instance. However, when I try to return the final object from this class, I get an error. A quick example:
require 'oauth'
class OauthFigshare < OAuth::AccessToken
def initialize (consumerkey, consumersecret, accesstoken, accesstokensecret)
#consumerkey = consumerkey
#consumersecret = consumersecret
#accesstoken = accesstoken
#accesstokensecret = accesstokensecret
#apiurl = "http://api.figshare.com"
#consumer = OAuth::Consumer.new(#consumerkey,#consumersecret,{:site=> #apiurl})
#token = { :oauth_token => #accesstoken, :oauth_token_secret => #accesstokensecret}
puts #consumer.class
puts #token
#client = OAuth::AccessToken.from_hash(#consumer, #token)
puts #client
puts #client.get('/v1/my_data/articles')
return #client
end
end
The problem is that when I check inside the class to see if the token is working, it does. However, when I check against the constructed object outside the class, it doesn't work.
#client.get(url) returns Net::HTTPOk calling from in the class
auth = OauthFigshare.new(inputs)
auth.get(url)
This returns Net::HTTPUnauthorized
What am I not getting about scope here?
Edit to include actual class
The return value of the initialize method is not used. It seems like you actually want to override self.new instead.

why DataType.EmailAddress converted link automatically?

I'm using a model with DataType.EmailAddress. I would like to modify the address link in the run time, however it already has email link automatically that prevents my modification.
#{
var subject = "";
if (Model.Name.Length > 30)
{
subject = Model.Name.Substring(0, 30) + "...";
}
else
{
subject = Model.Name;
}
}
model => model.email
But I got
<a href="mailto:emailaddress">emailaddress</a>
instead of
emailaddress
Why the email address is converted into link form automatically? And how to stop it? I would like to keep the datatype to use validation though.
You're trying to print the value of the property: #model.Email.
DisplayFor is not what you want.
Also, you need to URL-encode the subject parameter, including the space after Re:.

Resources