Class: ESV::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/esv_api/client.rb

Overview

Wrapper for the ESV API

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ESV::Client

Initializes a new API object



29
30
31
32
33
34
35
36
37
38
# File 'lib/esv_api/client.rb', line 29

def initialize(attrs={})
  attrs = ESV.options.merge(attrs)
  Config::VALID_OPTIONS_KEYS.each do |key|
    instance_variable_set("@#{key}".to_sym, attrs[key])
  end

  if ESV.should_use_caching?
    @dc = Dalli::Client.new('localhost:11211')
  end
end

Instance Method Details

#daily_verse(options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/esv_api/client.rb', line 115

def daily_verse( options={} )
  query = {
      'key' => self.api_key,
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/verse', { :query => query } )
end

#passage_query(passage, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/esv_api/client.rb', line 40

def passage_query(passage, options={})
  query = {
      'key' => self.api_key,
      'passage' => passage
  }

  query = options.merge( query )

  if ESV.should_use_caching?
    cache_key = Digest::MD5.hexdigest( "esv_api_" + query.to_s )

    cached_value = @dc.get( cache_key )

    return cached_value unless cached_value.nil?
  end

  esv_text = ESV::Client.get( self.endpoint + '/passageQuery', { :query => query } )

  @dc.set( cache_key, esv_text ) if ESV.should_use_caching?

  return esv_text
end

#query(q, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/esv_api/client.rb', line 63

def query( q, options={} )
  query = {
      'key' => self.api_key,
      'q' => q
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/query', { :query => query } )
end

#query_info(q, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/esv_api/client.rb', line 84

def query_info( q, options={} )
  query = {
      'key' => self.api_key,
      'q'   => q
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/queryInfo', { :query => query } )
end

#reading_plan_info(options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/esv_api/client.rb', line 95

def reading_plan_info( options={} )
  query = {
      'key' => self.api_key,
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/readingPlanQuery', { :query => query } )
end

#reading_plan_query(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/esv_api/client.rb', line 74

def reading_plan_query( options={} )
  query = {
      'key' => self.api_key,
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/readingPlanQuery', { :query => query } )
end

#verse(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/esv_api/client.rb', line 105

def verse( options={} )
  query = {
      'key' => self.api_key,
  }

  query = options.merge( query )

  ESV::Client.get( self.endpoint + '/verse', { :query => query } )
end