Class: Location::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/location/finder.rb

Defined Under Namespace

Classes: Address

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postal_code, service) ⇒ Finder

Returns a new instance of Finder.



14
15
16
17
18
# File 'lib/location/finder.rb', line 14

def initialize(postal_code, service)
  @postal_code = postal_code
  @service = service
  @address = Address.new
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



4
5
6
# File 'lib/location/finder.rb', line 4

def address
  @address
end

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'lib/location/finder.rb', line 4

def error
  @error
end

#postal_codeObject

Returns the value of attribute postal_code.



3
4
5
# File 'lib/location/finder.rb', line 3

def postal_code
  @postal_code
end

#serviceObject

Returns the value of attribute service.



3
4
5
# File 'lib/location/finder.rb', line 3

def service
  @service
end

Class Method Details

.build(postal_code) ⇒ Object



6
7
8
# File 'lib/location/finder.rb', line 6

def self.build(postal_code)
  new(postal_code, Location.configuration.default_service.new)
end

.find(postal_code, &block) ⇒ Object



10
11
12
# File 'lib/location/finder.rb', line 10

def self.find(postal_code, &block)
  build(postal_code).find(&block)
end

Instance Method Details

#findObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/location/finder.rb', line 20

def find
  service.fetch(postal_code, address)
  @success = true
rescue Services::Error => error
  @error   = error.message
  @success = false
ensure
  @address.freeze
  yield self if block_given?
end

#successful?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/location/finder.rb', line 31

def successful?
  @success
end