class DigitalBiblePlatform::Client

Attributes

api_key[R]

Public Class Methods

new(api_key, defaults={}) click to toggle source
# File lib/digital_bible_platform.rb, line 11
def initialize(api_key, defaults={})
  @api_key = api_key
  @defaults = {
    # API Defaults
    protocol:       'http', 
    audio_encoding: 'mp3',
    callback:       false,

    # Media Defaults
    language:   "English",
    version:    :esv,
    collection: :complete,
    drama:      :drama,
    media:      :audio,
  }.merge(defaults)
end

Public Instance Methods

books(overrides={}) click to toggle source
# File lib/digital_bible_platform.rb, line 39
def books(overrides={})
  options = @defaults.merge(overrides)
  connect!('/library/book', { dam_id: DamId.partial(options) })
end
url_for(book_short_name, chapter=1, overrides={}) click to toggle source
# File lib/digital_bible_platform.rb, line 29
def url_for(book_short_name, chapter=1, overrides={})      
  response = connect!('/audio/path', {
    dam_id: DamId.full( @defaults.merge(overrides.merge({book:book_short_name}) )),
    book_id:    book_short_name,
    chapter_id: chapter,
  })
  file = response.find {|book| book.book_id==book_short_name}.path
  "#{base_url}/#{file}"
end

Private Instance Methods

base_url() click to toggle source
# File lib/digital_bible_platform.rb, line 58
def base_url
  @cdns ||= connect!('/audio/location')
  cdn = @cdns.sort_by(&:priority).first {|_cdn| _cdn.protocal==@defaults[:protocal]}
  "#{cdn.protocol}://#{cdn.server}#{cdn.root_path}"
end
connect!(path, options={}) click to toggle source
# File lib/digital_bible_platform.rb, line 46
def connect!(path, options={})
  request_body = {key:@api_key, v:'2'}.merge(options)
  request_body.merge!({reply:'jsonp',callback:@defaults[:callback]}) if @defaults[:callback]
  response = Typhoeus.get("http://dbt.io#{path}", params:request_body, followlocation: true)
  reply = Oj.load( response.body )
  
  case reply
    when Hash  then Hashie::Mash.new(reply)
    when Array then reply.map {|hash| Hashie::Mash.new(hash)}
  end
end