airvisual_api 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/airvisual_api/city.rb +6 -0
- data/lib/airvisual_api/client.rb +9 -4
- data/lib/airvisual_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e074bae36cd7cde2a2c4452d8d8f17f43d24a5df32db0014356e33527fcbc728
|
4
|
+
data.tar.gz: 2797272c6cdff75d079f39d9a752f4f1d35a80f21178fd4fcdbd6134c800fd27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d771ef49fcabd86e3a6a51018966c7ff308b1f43126c2aec8770484fe17c5f8c766b0609a7201c345be65b8b1a777cf36af2e33582fbeeca7d9998f9a7f91b37
|
7
|
+
data.tar.gz: bee3e204bc9ad8a3c11fcfe2034521ee11531512c69ee2ef55b042f919ed10fcb9130f0bce26afe022529830b5bf627562374f1db79bcc86174c4cc1b1fb98ab
|
data/lib/airvisual_api/city.rb
CHANGED
@@ -6,6 +6,12 @@ module AirVisualApi
|
|
6
6
|
# some of the key names. I can't stand abbreviation
|
7
7
|
# and jargon.
|
8
8
|
|
9
|
+
# Have to actually set the IP or we'll get the
|
10
|
+
# server's IP as a result.
|
11
|
+
def nearest_city_by_ip(ip:)
|
12
|
+
client.get(url: '/nearest_city', headers: { 'x-forwarded-for' => ip })
|
13
|
+
end
|
14
|
+
|
9
15
|
def nearest_city_by_gps(latitude:, longitude:)
|
10
16
|
# coercing to float to ensure proper formatting
|
11
17
|
# this should probably be a little smarter with some
|
data/lib/airvisual_api/client.rb
CHANGED
@@ -3,22 +3,27 @@ require 'httparty'
|
|
3
3
|
module AirVisualApi
|
4
4
|
class Client
|
5
5
|
BASE_URI = 'http://api.airvisual.com/v2'
|
6
|
+
DEFAULT_HEADERS = {
|
7
|
+
'Accept' => 'application/json',
|
8
|
+
'Content-Type' => 'application/json'
|
9
|
+
}.freeze
|
6
10
|
|
7
11
|
def initialize(api_key: ENV['AIRVISUAL_API_KEY'])
|
8
12
|
# defaults to env variable but can be specified
|
9
13
|
@api_key = api_key
|
10
14
|
end
|
11
15
|
|
12
|
-
def get(url:)
|
13
|
-
HTTParty.get(BASE_URI + url + key_param)
|
16
|
+
def get(url:, headers: {})
|
17
|
+
HTTParty.get(BASE_URI + url + key_param(url), headers: headers.merge(DEFAULT_HEADERS))
|
14
18
|
end
|
15
19
|
|
16
20
|
private
|
17
21
|
|
18
22
|
attr_reader :api_key
|
19
23
|
|
20
|
-
def key_param
|
21
|
-
"&key=#{api_key}"
|
24
|
+
def key_param(url)
|
25
|
+
return "&key=#{api_key}" if url.include?('?')
|
26
|
+
"?key=#{api_key}"
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|