openaq-client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 53cc2eb6ba11d375be248fcb0390094518d4277615bea4b3c39e10fff0193afd
4
+ data.tar.gz: dd1a59b861c8f44a8b2ece8fc36c7925be0712ccc02441f716618ca6c5e04755
5
+ SHA512:
6
+ metadata.gz: c9655a124435f37b4be0a34971c43b950b4f92b9b40975dba4ff29cb85be153c4ae9098645ff1fbe46d4e87738288693c6409bcaa34b4602563b9e25bfafc1bb
7
+ data.tar.gz: a43f13591566e50856860775050bffbaceaa9e96e288c4181b0b25b0faa089990acbe94dde3c562378b6a55b1776a4cee8247748102e14933f180ecca5976466
data/lib/openaq.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ require "openaq/client"
5
+ require "openaq/version"
6
+
7
+ module Openaq
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ def client
12
+ @client ||= Client.new
13
+ end
14
+
15
+ def url
16
+ 'https://api.openaq.org/v1'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require "openaq/networking"
2
+
3
+ module Openaq
4
+ class Client
5
+ include Networking
6
+
7
+ [
8
+ :cities,
9
+ :countries,
10
+ :fetches,
11
+ :latest,
12
+ :locations,
13
+ :measurements,
14
+ :parameters,
15
+ :sources,
16
+ ].each do |name|
17
+ define_method(name) do |params={}|
18
+ get("/#{name}", params)
19
+ end
20
+
21
+ define_method(:"all_#{name}") do |params={}|
22
+ paginated_get("/#{name}", params)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module Openaq
2
+ module Networking
3
+
4
+ def get(path, params={})
5
+ uri = URI(Openaq.url + path)
6
+ uri.query = URI.encode_www_form(params)
7
+
8
+ begin
9
+ response = Net::HTTP.get_response(uri)
10
+ rescue Timeout::Error, Errno::ECONNREFUSED => e
11
+ raise Openaq::Error, e.message
12
+ end
13
+
14
+ parsed_response = JSON.parse(response.body) rescue { "results" => [] }
15
+ if !response.is_a?(Net::HTTPSuccess)
16
+ raise Openaq::Error, parsed_response["error"] || "Openaq responded with #{response.code}."
17
+ end
18
+
19
+ parsed_response["results"]
20
+ end
21
+
22
+ def paginated_get(path, params={})
23
+ Enumerator.new do |yielder|
24
+ page = 1
25
+ params = { page: page }.merge(params)
26
+
27
+ loop do
28
+ response = get(path, params)
29
+ if !response.empty?
30
+ response.map { |item| yielder << item }
31
+ params[:page] += 1
32
+ else
33
+ raise StopIteration
34
+ end
35
+ end
36
+ end.lazy
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Openaq
2
+ VERSION = "0.1.1".freeze
3
+ end
@@ -0,0 +1,14 @@
1
+ RSpec.describe Openaq do
2
+
3
+ it "has a version number" do
4
+ expect(Openaq::VERSION).not_to be nil
5
+ expect(Openaq::VERSION).to eql "0.1.1"
6
+ end
7
+
8
+ describe ".client" do
9
+ it "should create and return a new client" do
10
+ expect(Openaq.client).to be_a(Openaq::Client)
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,22 @@
1
+ require "bundler/setup"
2
+ require 'webmock/rspec'
3
+ require "vcr"
4
+
5
+ require File.expand_path('../../lib/openaq', __FILE__)
6
+
7
+ VCR.configure do |config|
8
+ config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
9
+ config.hook_into :webmock
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ # Enable flags like --only-failures and --next-failure
14
+ config.example_status_persistence_file_path = ".rspec_status"
15
+
16
+ # Disable RSpec exposing methods globally on `Module` and `main`
17
+ config.disable_monkey_patching!
18
+
19
+ config.expect_with :rspec do |c|
20
+ c.syntax = :expect
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openaq-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Yiorgos Michokostas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ description: 'openaq-client is a Gem for the openaq.org JSON API
84
+
85
+ '
86
+ email:
87
+ - michokostas@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - lib/openaq.rb
93
+ - lib/openaq/client.rb
94
+ - lib/openaq/networking.rb
95
+ - lib/openaq/version.rb
96
+ - spec/openaq_spec.rb
97
+ - spec/spec_helper.rb
98
+ homepage: https://github.com/gmichokostas/openaq-client
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.7.7
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Ruby client for the OpenAQ API
122
+ test_files:
123
+ - spec/openaq_spec.rb
124
+ - spec/spec_helper.rb