Class: TRex::Metasaurus

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/t_rex/metasaurus.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, headers = {}) ⇒ Metasaurus

Returns a new instance of Metasaurus.



19
20
21
22
# File 'lib/t_rex/metasaurus.rb', line 19

def initialize(params = {}, headers = {})
  @params, @headers = params, headers
  scrub_params
end

Class Attribute Details

.metasaurus_pathObject

Hooks for rspec testing to clear memoization



11
12
13
# File 'lib/t_rex/metasaurus.rb', line 11

def metasaurus_path
  @metasaurus_path
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/t_rex/metasaurus.rb', line 7

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/t_rex/metasaurus.rb', line 7

def params
  @params
end

Class Method Details

.metasaurus_urlObject



14
15
16
17
# File 'lib/t_rex/metasaurus.rb', line 14

def self.metasaurus_url
  @metasaurus_path ||= TRex.configuration.metasaurus_url ||
    raise(TRexConfigurationError, 'No URL defined for Metasaurus')
end

Instance Method Details

#debugObject



24
25
26
# File 'lib/t_rex/metasaurus.rb', line 24

def debug
  TRex.configuration.metasaurus_debug
end

#endpointObject



28
29
30
# File 'lib/t_rex/metasaurus.rb', line 28

def endpoint
  @endpoint ||= (self.params.delete('endpoint') || 'geo')
end

#getObject



32
33
34
35
# File 'lib/t_rex/metasaurus.rb', line 32

def get
  options = {query: params, headers: headers}
  self.class.get url, options
end

#scrub_paramsObject



37
38
39
40
41
# File 'lib/t_rex/metasaurus.rb', line 37

def scrub_params
  if params[:zip].to_s =~ /(\d{5})[-\w]?/
    self.params[:zip] = $1
  end
end

#urlObject



43
44
45
# File 'lib/t_rex/metasaurus.rb', line 43

def url
  "#{self.class.metasaurus_url}/#{endpoint}"
end