Class: GoogleMapsJuice::Timezone

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/google_maps_juice/timezone.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

ENDPOINT =
'/timezone'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

#initialize

Constructor Details

This class inherits a constructor from GoogleMapsJuice::Endpoint

Class Method Details

.by_location(params, api_key: GoogleMapsJuice.config.api_key) ⇒ Object



7
8
9
10
# File 'lib/google_maps_juice/timezone.rb', line 7

def by_location(params, api_key: GoogleMapsJuice.config.api_key)
  client = GoogleMapsJuice::Client.new(api_key: api_key)
  self.new(client).by_location(params)
end

Instance Method Details

#build_request_params(params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google_maps_juice/timezone.rb', line 48

def build_request_params(params)
  seconds_since_epoch = (params[:timestamp] || Time.now).to_i
  {
    location: "#{params[:latitude]},#{params[:longitude]}",
    timestamp: seconds_since_epoch
  }.tap do |req_params|
    if params[:language]
      req_params[:language] = params[:language]
    end
  end
end

#by_location(params) ⇒ Object



13
14
15
16
17
18
# File 'lib/google_maps_juice/timezone.rb', line 13

def by_location(params)
  validate_params(params)
  response_text = @client.get("#{ENDPOINT}/json", build_request_params(params))
  response = JSON.parse(response_text, object_class: Response)
  detect_errors(response)
end

#validate_location_params(params) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/google_maps_juice/timezone.rb', line 39

def validate_location_params(params)
  if params[:latitude].abs > 90
    raise ArgumentError, 'Wrong latitude value'
  end
  if params[:longitude].abs > 180
    raise ArgumentError, 'Wrong longitude value'
  end
end

#validate_params(params) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_maps_juice/timezone.rb', line 20

def validate_params(params)
  raise ArgumentError, 'Hash argument expected' unless params.is_a?(Hash)

  supported_keys = %w( latitude longitude timestamp language )
  validate_supported_params(params, supported_keys)

  required_keys = %w( latitude longitude )
  validate_all_required_params(params, required_keys)

  validate_timestamp_param(params)
  validate_location_params(params)
end

#validate_timestamp_param(params) ⇒ Object



33
34
35
36
37
# File 'lib/google_maps_juice/timezone.rb', line 33

def validate_timestamp_param(params)
  if params.has_key?(:timestamp)
    raise ArgumentError, 'Timestamp must be a Time instance' unless params[:timestamp].is_a?(Time)
  end
end