Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Get a bible passage from the Living Stones / Seek-First Web Service with Ruby

require 'net/http'

LS_BASE_URL = 'http://www.seek-first.com/Bible.php?q=&passage=Seek'

def lookup_ls(reference, translation) # living stones (KJV, ASV, YLT, AKJV, WEB)
  return if reference.nil? or reference.empty?
  url = LS_BASE_URL + '&p=' + URI.escape(reference) + '&version=' + translation
  result = Net::HTTP.get(URI.parse(url))
  url = /<!\-\-\s*(http:\/\/api\.seek\-first\.com.+?)\s*\-\->/.match(result)[1]
  result = Net::HTTP.get(URI.parse(url)).gsub(/\s+/, ' ').gsub(/“|�/, '"').gsub(/‘|’/, "'").gsub('*', '')
  text = result.scan(/<Text>(.+?)<\/Text>/).map { |p| p[0].strip.gsub(/<.+?>/, '') }.join(' ') rescue nil
  return {:reference => reference, :text => text}
end

Get a Bible passage from the ESV Web Service with Ruby

require 'net/http'

BASE_URL = 'http://www.gnpcb.org/esv/share/get/'
OPTIONS = {
  'key' => 'IP',
  'action' => 'doPassageQuery',
  'include-verse-numbers' => '0',
  'include-short-copyright' => '0',
  'output-format' => 'plain-text',
  'include-passage-horizontal-lines' => '0',
  'include-heading-horizontal-lines' => '0',
  'include-passage-references' => '1',
  'include-first-verse-numbers' => '0',
  'include-footnotes' => '0',
  'include-footnote-links' => '0',
  'include-headings' => '0',
  'include-subheadings' => '0',
  'line-length' => '0',
}

def combine_refs(refs)
  combined = refs.first
  refs[1..-1].each do |ref|
    if combined.index(ref.gsub(/\:.*$/, '')) == 0
      combined += ',' + /\:(.*)$/.match(ref)[1]
    elsif combined.index(ref.gsub(/\d+\:.*$/, '')) == 0
      combined += ';' + /\d+\:.*$/.match(ref)[0]
    else
      return nil # couldn't do it - fail *not* gracefully
    end
  end
  combined
end

def lookup_esv(reference)
  return if reference.nil? or reference.empty?
  url = BASE_URL + '?' + OPTIONS.map { |name, value| "#{name}=#{value}" }.join('&') + '&passage=' + URI.escape(reference)
  result = Net::HTTP.get(URI.parse(url))
  if result =~ /^ERROR/
    puts 'Error retrieving verse from web service'
    nil
  else
    refs = []
    result.scan(/^\S.+$/) {|ref| refs << ref }
    refs.each {|ref| result.gsub! ref, '' }
    reference = combine_refs(refs)
    text = result.gsub(/\s+/, ' ').strip
    text = text.gsub(/"/, '') if (text.count('"') == 1 and text[0..0] == '"')
    return {:reference => reference, :text => text}
  end
end


Usage:

> lookup_esv('John 3:16')
=> {:text=>"For God so loved the world, that he gave his only Son, that whoever
believes in him should not perish but have eternal life.", :reference=>"John 3:16"}

wsdl the google API (search google with ruby)

sign up to get a key first

require 'soap/wsdlDriver'
$KCODE = "UTF8"
key = 'LVJnAm5QFHblahblahblah your key here'

#create driver
wsdl = "http://api.google.com/GoogleSearch.wsdl"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
query = "your query string here"
start = 0
max = 10
  
# see http://dev.ctor.org/soap4r/browser/trunk/sample/wsdl/googleSearch/wsdlDriver.rb
@results = driver.doGoogleSearch( key, query, start, max, true, "", true, 'lang_en', '','')
snippets = @results.resultElements.collect { |r| r.snippet } # you can get all kinds'a' info here
self.update_attribute(:html, snippets.join("\n")) # or whatever

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS