Class: OpenWeather::NetworkRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/open_weather/network_request.rb

Class Method Summary collapse

Class Method Details

.by_city(name: '', units: 'standard', lang: 'en') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/open_weather/network_request.rb', line 10

def self.by_city(name: '', units: 'standard', lang: 'en')
  url = OpenWeather.configuration.weather_base_uri
  api_key = OpenWeather.configuration.api_key
  uri = @helper.name_to_url(url, name, api_key, units, lang)
  begin
    @response = HTTParty.get(uri)
  rescue StandardError => e
    logger = Logger.new($stdout)
    logger.error('Network is required')
    return []
  end
  if @response.message == 'Unauthorized' || @response.code == 404
    logger = Logger.new($stdout)
    logger.error('Api Key is required')
    return []
  end
  @helper.to_json(@response.body)
end

.by_city_id(id: '', units: 'standard', lang: 'en') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/open_weather/network_request.rb', line 29

def self.by_city_id(id: '', units: 'standard', lang: 'en')
  url = OpenWeather.configuration.weather_base_uri
  api_key = OpenWeather.configuration.api_key
  uri = @helper.id_to_url(url, id, api_key, units, lang)
  begin
    @response = HTTParty.get(uri)
  rescue StandardError => e
    logger = Logger.new($stdout)
    logger.error('Network is required')
    return []
  end
  if @response.message == 'Unauthorized' || @response.code == 404
    logger = Logger.new($stdout)
    logger.error('Api Key is required')
    return []
  end
  @helper.to_json(@response.body)
end

.by_coords(coords: [], units: 'standard', lang: 'en') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/open_weather/network_request.rb', line 48

def self.by_coords(coords: [], units: 'standard', lang: 'en')
  api_key = OpenWeather.configuration.api_key
  url = OpenWeather.configuration.weather_base_uri
  uri = @helper.cords_to_url(url, coords, api_key, units, lang)

  begin
    @response = HTTParty.get(uri)
  rescue StandardError => e
    logger = Logger.new($stdout)
    logger.error('Network is required')
    return []
  end
  if @response.message == 'Unauthorized' || @response.code == 404
    logger = Logger.new($stdout)
    logger.error('Api Key is required')
    return []
  end
  @helper.to_json(@response.body)
end

.by_find(coords: [], count: '', units: 'standard', lang: 'en') ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/open_weather/network_request.rb', line 87

def self.by_find(coords: [], count: '', units: 'standard', lang: 'en')
  api_key = OpenWeather.configuration.api_key
  url = OpenWeather.configuration.weather_base_uri
  uri = @helper.find_to_url(url, coords, count, api_key, units, lang)

  begin
    @response = HTTParty.get(uri)
  rescue StandardError => e
    logger = Logger.new($stdout)
    logger.error('Network is required')
    return []
  end
  if @response.message == 'Unauthorized' || @response.code == 404
    logger = Logger.new($stdout)
    logger.error('Api Key is required')
    return []
  end
  @helper.to_json(@response.body)
end

.by_zip(zipcode: '', country: '', units: 'standard', lang: 'en') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/open_weather/network_request.rb', line 68

def self.by_zip(zipcode: '', country: '', units: 'standard', lang: 'en')
  api_key = OpenWeather.configuration.api_key
  url = OpenWeather.configuration.weather_base_uri
  uri = @helper.zipcode_to_url(url, zipcode, country, api_key, units, lang)
  begin
    @response = HTTParty.get(uri)
  rescue StandardError => e
    logger = Logger.new($stdout)
    logger.error('Network is required')
    return []
  end
  if @response.message == 'Unauthorized' || @response.code == 404
    logger = Logger.new($stdout)
    logger.error('Api Key is required')
    return []
  end
  @helper.to_json(@response.body)
end