Class: PQSDK::City

Inherits:
RemoteObject show all
Defined in:
lib/pqsdk/city.rb

Overview

The City class provides an interface for crawlers to the v1/cities api endpoint.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RemoteObject

all, #create, #create!, from_hash, get, #persisted?, #save, #save!, #update, #update!

Instance Attribute Details

#countryObject

Returns the value of attribute country.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def country
  @country
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def id
  @id
end

#inhabitantsObject

Returns the value of attribute inhabitants.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def inhabitants
  @inhabitants
end

#latitudeObject

Returns the value of attribute latitude.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def name
  @name
end

#stateObject

Returns the value of attribute state.



9
10
11
# File 'lib/pqsdk/city.rb', line 9

def state
  @state
end

Class Method Details

.find(name) ⇒ Object



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

def self.find(name)
  res = RestLayer.get(@endpoint, q: name)
  if res[0] == 200
    from_hash res[1]
  elsif res[0] == 404
    nil
  else
    raise "Unexpected HTTP status code #{res[0]}, #{res[1]}"
  end
end

.find_or_create(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/pqsdk/city.rb', line 31

def self.find_or_create(name)
  city = find(name)
  return city if city

  city = City.new
  city.name = name
  city.create!

  city
end

Instance Method Details

#attributesObject



13
14
15
16
17
18
# File 'lib/pqsdk/city.rb', line 13

def attributes
  {
    'name' => nil, 'inhabitants' => nil, 'latitude' => nil,
    'longitude' => nil, 'state' => nil, 'country' => nil
  }
end