I'm developing a Jenkins plugin in Ruby. You're supposed to be able to configure every node that connects to the server so that an email is sent to a specified address when the node loses its connection to the master. EmailNodeProperty adds a field to enter an email address:
#
# Save an email property for every node
#
class EmailNodeProperty < Jenkins::Slaves::NodeProperty
require 'java'
import 'hudson.util.FormValidation'
display_name "Email notification"
attr_accessor :email
def initialize(attrs = {})
#email = attrs['email']
end
def doCheckEmail value
puts " ENP.doCheckEmail:#{value}"
end
end
When you configure a node, there's a field named email where you can enter an email address. I want this field to be validated when you enter an address.
When you save the configuration, an EmailNodeProperty is created whence (that's right) you can access the email address.
MyComputerListener's offline gets called when a node loses its connection:
class MyComputerListener
include Jenkins::Slaves::ComputerListener
include Jenkins::Plugin::Proxy
def online(computer, listener)
end
def offline(computer)
#Do nothing when the Master shuts down
if computer.to_s.match('Master') == nil
list = computer.native.getNode().getNodeProperties()
proxy = list.find {"EmailNodeProperty"}
if proxy.is_a?(Jenkins::Plugin::Proxy)
rubyObject = proxy.getTarget()
email = rubyObject.email #<= Accesses the email from EmailNodeProperty
[...]
end
end
end
end
MyComputerListener finds the email address and sends an email.
Does anybody know if it is possible to validate the form in Ruby? According to the Jenkins wiki, this is what's supposed to be implemented (FIELD is supposed to be exchanged for the field name, so I guess it should be doCheckEmail):
public FormValidation doCheckFIELD(#QueryParameter String value) {
if(looksOk(value))
return FormValidation.ok();
else
return FormValidation.error("There's a problem here");
}
How would you do this in Ruby? Where should the method be implemented? In EmailNodeProperty or in MyComputerListener? How do you handle the QueryParameter? The # would make it an intstance variable in Ruby. (What is a Queryparameter?)
Any help would be much appreciated!
/Jonatan
This simply doesn't exist today, and we badly need to add it. This has been brought up a couple of times already in Thursday morning's hack session, so it's high on the TODO list. But as of ruby-runtime plugin 0.10, this just isn't possible. Sorry to let you down.
Related
I had a previous question that helped me loop through all users where a certain question is met.
However, I'm realizing I can't hard code that condition. I need to somehow get that data from the submitted form, which doesn't seem to be possible in the mailer.
In other words, I'm trying to loop through all users where the user's state is equal to the home_state of the candidate being entered. Basically when the candidate is created, I want to get the home_state of that candidate, and then loop through all users, and for each user that has same state as that candidate, I want to send them the email via this mailer.
Here's my candidate_mailer.rb file
class CandidateMailer < ApplicationMailer
default from: 'wesleycreations#gmail.com'
def self.send_request(row)
#candidate = Candidate.new(candidate_params) # if I can access this here, how to I create the
# following array?
emails = []
User.where(state: #candidate.home_state).each do |u|
emails << u.email # To insert the user email into the array
end
emails.each do |email|
new_request(email,row).deliver_now
end
end
def new_request(email, row)
#candidate = row
mail(to: email, subject: 'New request')
end
end
But the
#candidate = Candidate.new(candidate_params)
obviously doesn't work because the params aren't available in the mailer.
Here in the candidates_controller.rb I have this
def create
#candidate = Candidate.new(candidate_params) #of course here I can access params
if #candidate.save
row = #candidate
CandidateMailer.send_request(row)
else
render('new')
end
end
SO the question is, how do I access params in rails mailer? And if I can't, then how do I refactor my code so that the lines that check if the user meets certain condition is done in the controller?
I was able to figure this out by doing this. after I saved the candidate, I saved the candidate to a global variable. and THEN I send the mailer.
def create
#candidate = Candidate.new(candidate_params)
if #candidate.save
row = #candidate
$candidate = #candidate
end
CandidateMailer.send_request(row)
else
end
end
This way the mailer had access to the new candidate that been created, and I was able to check my condition in there.
So in my mailer, when I use $candidate.home_state, it returned the correct state, mail went out, and made me very happy :)
emails = []
User.where(state: $candidate.home_state).each do |u|
emails << u.email # To insert the user email into the array
end
I have been tasked with creating a Ruby API that retrieves youtube URL's. However, I am not sure of the proper way to create an 'API'... I did the following code below as a Sinatra server that serves up JSON, but what exactly would be the definition of an API and would this qualify as one? If this is not an API, how can I make in an API? Thanks in advance.
require 'open-uri'
require 'json'
require 'sinatra'
# get user input
puts "Please enter a search (seperate words by commas):"
search_input = gets.chomp
puts
puts "Performing search on YOUTUBE ... go to '/videos' API endpoint to see the results and use the output"
puts
# define query parameters
api_key = 'my_key_here'
search_url = 'https://www.googleapis.com/youtube/v3/search'
params = {
part: 'snippet',
q: search_input,
type: 'video',
videoCaption: 'closedCaption',
key: api_key
}
# use search_url and query parameters to construct a url, then open and parse the result
uri = URI.parse(search_url)
uri.query = URI.encode_www_form(params)
result = JSON.parse(open(uri).read)
# class to define attributes of each video and format into eventual json
class Video
attr_accessor :title, :description, :url
def initialize
#title = nil
#description = nil
#url = nil
end
def to_hash
{
'title' => #title,
'description' => #description,
'url' => #url
}
end
def to_json
self.to_hash.to_json
end
end
# create an array with top 3 search results
results_array = []
result["items"].take(3).each do |video|
#video = Video.new
#video.title = video["snippet"]["title"]
#video.description = video["snippet"]["description"]
#video.url = video["snippet"]["thumbnails"]["default"]["url"]
results_array << #video.to_json.gsub!(/\"/, '\'')
end
# define the API endpoint
get '/videos' do
results_array.to_json
end
An "API = Application Program Interface" is, simply, something that another program can reliably use to get a job done, without having to busy its little head about exactly how the job is done.
Perhaps the simplest thing to do now, if possible, is to go back to the person who "tasked" you with this task, and to ask him/her, "well, what do you have in mind?" The best API that you can design, in this case, will be the one that is most convenient for the people (who are writing the programs which ...) will actually have to use it. "Don't guess. Ask!"
A very common strategy for an API, in a language like Ruby, is to define a class which represents "this application's connection to this service." Anyone who wants to use the API does so by calling some function which will return a new instance of this class. Thereafter, the program uses this object to issue and handle requests.
The requests, also, are objects. To issue a request, you first ask the API-connection object to give you a new request-object. You then fill-out the request with whatever particulars, then tell the request object to "go!" At some point in the future, and by some appropriate means (such as a callback ...) the request-object informs you that it succeeded or that it failed.
"A whole lot of voodoo-magic might have taken place," between the request object and the connection object which spawned it, but the client does not have to care. And that, most of all, is the objective of any API. "It Just Works.™"
I think they want you to create a third-party library. Imagine you are schizophrenic for a while.
Joe wants to build a Sinatra application to list some YouTube videos, but he is lazy and he does not want to do the dirty work, he just wants to drop something in, give it some credentials, ask for urls and use them, finito.
Joe asks Bob to implement it for him and he gives him his requirements: "Bob, I need YouTube library. I need it to do:"
# Please note that I don't know how YouTube API works, just guessing.
client = YouTube.new(api_key: 'hola')
video_urls = client.videos # => ['https://...', 'https://...', ...]
And Bob says "OK." end spends a day in his interactive console.
So first, you should figure out how you are going to use your not-yet-existing lib, if you can – sometimes you just don't know yet.
Next, build that library based on the requirements, then drop it in your Sinatra app and you're done. Does that help?
I'm setting up Backburner as a work queue, and my job items need to return JSON for the resulting data they create. I'm not sure how to structure this. As a test I've tried doing:
class PrintJob
include Backburner::Performable
def self.print(text)
puts text
return "results"
end
end
Backburner.configure do |config|
config.beanstalk_url = ["beanstalk://127.0.0.1"]
# etc
end
val = PrintJob.async.print('some cool text')
puts val
and running Backburner.work inside IRB. The puts works but the return value comes back as true instead of "results".
Is there a way to get return values out of async methods? Or should I try a different approach, e.g. having one queue for jobs and another for results? If so, how can I associate the result 'job' with the original work it belongs to?
Note: I'm eventually using Sinatra and not Rails.
Okay so I am trying to create an object for each found email domain in a text file. So far I have the matching system working and now have ran into a problem creating the objects on the fly. Here is what I got so far.
# domain = emails domain name (e.g. 'example.com')
# Agency = class for domain
if (domain + "Object").nil? == false
domain = Agency.new(domain + "Object")
#agencyList << domain
domain.addEmail(match)
puts "false"
elsif (domain + "Object").nil? == true
domain.addEmail(match)
puts "true"
end
end
end
So basically I want to check if the email domain already has an object created for it. If it doesn't, create an object using the domain name and send the matched up with the object method addEmail. If it does send the match to object method addEmail. I don't want to use hashes because I want the matches in separate arrays.
I have tried many things and I think I am in over my head. This is my first ruby script. Any help would be greatly appreciated.
I think you just want to check whether the object is in your agency list. Something like:
if #agencyList.any? {|agency| agency.domain == domain }
agency = Agency.new(domain)
#agencyList << domain
agency.addEmail(match)
puts "false"
else
domain.addEmail(match)
puts "true"
end
I'm developing a web service (in Ruby) which needs to do a number of
different things for each message it receives.
Before my web service can process a message it must do different things:
sanitizing (e.g. remove HTML/JS)
check format (e.g. valid email provided?)
check IP in blacklist
invoke 3rd party web service
plus 10-30 other things
I'm thinking about implementing a filter/composite filter architecture
where each step/phase is a filter. For instance, I could have these filters
Sanitize input filter
Email filter
Country code filter
Blacklist filter
Each filter should be possible to reject a message, so I'm considering
that a filter should raise/throw exceptions.
This will give a lot of flexibility and hopefully a codebase that are
easy to understand.
How would you did this? And what are pros and cons of above design?
I would leave Exceptions for the cases when the filter itself actually broke down (e.g blacklist not available etc) and indicate the valid/invalid state either by true/false return values or, as you also suggested, throwing a tag.
If you don't want to stop at first failure, but execute all filters anyway, you should choose the boolean return type and conjunct them together (success &= next_filter(msg))
If I understood your situation correctly, the filter can both modify the message or check some other source for validity (e.g blacklist).
So I would do it like this:
module MessageFilters
EmailValidator = ->(msg) do
throw :failure unless msg.txt =~ /#/
end
HTMLSanitizer = ->(msg) do
# this filter only modifies message, doesn't throw anything
# msg.text.remove_all_html!
end
end
class Message
attr_accessor :filters
def initialize
#filters = []
end
def execute_filters!
begin
catch(:failure) do
filters.each{|f| f.call self}
true # if all filters pass, this is returned, else nil
end
rescue => e
# Handle filter errors
end
end
end
message = Message.new
message.filters << MessageFilters::EmailValidator
message.filters << MessageFilters::HTMLSanitizer
success = message.execute_filters! # returns either true or nil