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-1 of 1 total  RSS 

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-1 of 1 total  RSS