Get ip address from domain name or get domain name from ip in ruby

resolv class can let us get ip from domain name or get domain name from ip in a ruby program.

example

require 'resolv'
p Resolv.getaddress "www.ruby-lang.org"
p Resolv.getname "210.251.121.214"

result

"54.235.116.125"
"ci.ruby-lang.org"

O

分類: ruby | 標籤: | 在〈Get ip address from domain name or get domain name from ip in ruby〉中留言功能已關閉

How to ping for reachability of remote host in Ruby

1.if you has a firewall, remember to open icmp-type 8 first

iptables -A INPUT -i $EXTIF -p icmp --icmp-type 8 -j ACCEPT

2.use net-ping gem,of course, you should install it first.

gem install net-ping
require 'net/ping'

def up?(host)
    check = Net::Ping::External.new(host)
    check.ping?
end

host = 'google.com'
puts up?(host)

3.use system ping command

def up?(host, timeout=5)
  system "ping -c 1 -t #{timeout} #{host} >/dev/null"
end

p up?('localhost')

references:

https://stackoverflow.com/questions/21081639/how-to-ping-for-reachability-of-remote-host-in-ruby

O

分類: ruby | 標籤: | 發佈留言

WordPress之ruby介面:rubypress

昨天試用了mediawiki-butt後,想到是不是也有wordpress的ruby介面,搜尋了一下,還真的有,就是本文要介紹的rubypress。

https://github.com/zachfeldman/rubypress

1.安裝

gem install rubypress

2.馬上試用

require 'rubypress'
wp = Rubypress::Client.new(
  host: "example.com",
  username: "user",
  password: "passwor"
)

wp.newPost(
  content: {
    post_status: "publish",
    post_date: Time.now,
    post_content: "This is the body",
    post_title: "RubyPress is the best!",
    post_name: "/rubypress-is-the-best",
    post_author: 1, # 1 if there is only the admin user, otherwise the user's id
    terms_names: {
      category: ['Category One','Category Two','Category Three'],
      post_tag: ['Tag One','Tag Two', 'Tag Three']
    }
  }
)  

3.去wordpress看是否文章已出現。

rubypress.jpg


4.又例如,取得id為425的post之標題和內容

post = wp.getPost(post_id: 425)  
p "標題:" + post["post_title"]
p "內容:" + post["post_content"]

結果為:

標題:Wordpress之ruby介面:rubypress
內容:<!-- wp:paragraph -->
<p>[rdp-wiki-embed url='http://apache:8080/mediawiki/index.php/Wordpress%E4%B9%8Bruby%E4%BB%8B%E9%9D%A2%EF%BC%9Arubypress']</p>
<!-- /wp:paragraph -->


注意事項:

如果你有使用多站網路的功能,也不需使用blog_id,只要建立連結時,host設成要連結的blog網址即可。我使用blog_id,不管設的值為何,都是第一個blog,只有設host才成功。

api詳細文件在https://codex.wordpress.org/XML-RPC_WordPress_API

分類: ruby | 標籤: | 發佈留言