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

   1  
   2  require 'net/http'
   3  
   4  LS_BASE_URL = 'http://www.seek-first.com/Bible.php?q=&passage=Seek'
   5  
   6  def lookup_ls(reference, translation) # living stones (KJV, ASV, YLT, AKJV, WEB)
   7    return if reference.nil? or reference.empty?
   8    url = LS_BASE_URL + '&p=' + URI.escape(reference) + '&version=' + translation
   9    result = Net::HTTP.get(URI.parse(url))
  10    url = /<!\-\-\s*(http:\/\/api\.seek\-first\.com.+?)\s*\-\->/.match(result)[1]
  11    result = Net::HTTP.get(URI.parse(url)).gsub(/\s+/, ' ').gsub(/“|�/, '"').gsub(/‘|’/, "'").gsub('*', '')
  12    text = result.scan(/<Text>(.+?)<\/Text>/).map { |p| p[0].strip.gsub(/<.+?>/, '') }.join(' ') rescue nil
  13    return {:reference => reference, :text => text}
  14  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

   1  
   2  def combine_refs(refs)
   3    combined = refs.first
   4    refs[1..-1].each do |ref|
   5      if combined.index(ref.gsub(/\:.*$/, '')) == 0
   6        combined += ',' + /\:(.*)$/.match(ref)[1]
   7      elsif combined.index(ref.gsub(/\d+\:.*$/, '')) == 0
   8        combined += ';' + /\d+\:.*$/.match(ref)[0]
   9      else
  10        return nil # couldn't do it - fail *not* gracefully
  11      end
  12    end
  13    combined
  14  end

Get a Bible passage from the ESV Web Service with Ruby

   1  
   2  require 'net/http'
   3  
   4  BASE_URL = 'http://www.gnpcb.org/esv/share/get/'
   5  OPTIONS = {
   6    'key' => 'IP',
   7    'action' => 'doPassageQuery',
   8    'include-verse-numbers' => '0',
   9    'include-short-copyright' => '0',
  10    'output-format' => 'plain-text',
  11    'include-passage-horizontal-lines' => '0',
  12    'include-heading-horizontal-lines' => '0',
  13    'include-passage-references' => '1',
  14    'include-first-verse-numbers' => '0',
  15    'include-footnotes' => '0',
  16    'include-footnote-links' => '0',
  17    'include-headings' => '0',
  18    'include-subheadings' => '0',
  19    'line-length' => '0',
  20  }
  21  
  22  def combine_refs(refs)
  23    combined = refs.first
  24    refs[1..-1].each do |ref|
  25      if combined.index(ref.gsub(/\:.*$/, '')) == 0
  26        combined += ',' + /\:(.*)$/.match(ref)[1]
  27      elsif combined.index(ref.gsub(/\d+\:.*$/, '')) == 0
  28        combined += ';' + /\d+\:.*$/.match(ref)[0]
  29      else
  30        return nil # couldn't do it - fail *not* gracefully
  31      end
  32    end
  33    combined
  34  end
  35  
  36  def lookup_esv(reference)
  37    return if reference.nil? or reference.empty?
  38    url = BASE_URL + '?' + OPTIONS.map { |name, value| "#{name}=#{value}" }.join('&') + '&passage=' + URI.escape(reference)
  39    result = Net::HTTP.get(URI.parse(url))
  40    if result =~ /^ERROR/
  41      puts 'Error retrieving verse from web service'
  42      nil
  43    else
  44      refs = []
  45      result.scan(/^\S.+$/) {|ref| refs << ref }
  46      refs.each {|ref| result.gsub! ref, '' }
  47      reference = combine_refs(refs)
  48      text = result.gsub(/\s+/, ' ').strip
  49      text = text.gsub(/"/, '') if (text.count('"') == 1 and text[0..0] == '"')
  50      return {:reference => reference, :text => text}
  51    end
  52  end


Usage:

   1  
   2  > lookup_esv('John 3:16')
   3  => {:text=>"For God so loved the world, that he gave his only Son, that whoever
   4  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