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

About this user

Tim Morgan http://timmorgan.org

« 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

Combine Bible verse references

Combines references from the same Bible book into one reference, i.e. Col 3:23 + Col 3:24 + Col 4:1 = Col 3:23,24;4:1

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

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"}
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS