cimis-ruby 0.1.0

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
+ SHA1:
3
+ metadata.gz: d654c3ea5da027d3f1d68a10d149b3bb82297086
4
+ data.tar.gz: 1a3898f53dd9defb65bcd9444490a0cc07746642
5
+ SHA512:
6
+ metadata.gz: 0f8a7abbc7c654149023916b05aecee4b3937f3e7d7f6ac4c817b5b5bf2b700e6c91ccef8eefc41ded9d6e985068ad64807d3c4da8ee5996d3d6d27b41382d59
7
+ data.tar.gz: bb684643a7c775ee91162ffff8e9d95e4eb749763d94f37fd27832abceba6ef10b6d8a2b5eeefcdae50bd318933d940e89131188de5853de5c64d3073ff723ba
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cimis-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 dphaener
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Cimis Ruby
2
+
3
+ This is a slim wrapper around the Department of Water Resources CIMIS REST API.
4
+ It allows you to easily query the API and get easy to handle responses.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'cimis-ruby'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cimis-ruby
21
+
22
+ ## Usage
23
+
24
+ ### Setup
25
+ The first thing you need to do is setup your app key. If you haven't yet
26
+ registered, go to the [CIMIS website](http://wwwcimis.water.ca.gov/) and
27
+ register. You can then go to your account page and retrieve the app key.
28
+
29
+ You need to configure the wrapper to use your app key:
30
+ ```ruby
31
+ Cimis.configure { |c| c.app_key = <your_app_key> }
32
+ ```
33
+
34
+ ### Stations
35
+
36
+ Get a list of CIMIS stations:
37
+ ```ruby
38
+ Cimis::Station.all
39
+ ```
40
+
41
+ This will return an array of station objects. Each station can then be used
42
+ to query for data from that station on the object itself:
43
+ ```ruby
44
+ station = Cimis::Station.all.first
45
+ station.data({start_date: "2015-10-10", end_date: "2015-10-12"})
46
+ ```
47
+
48
+ ### General Querying
49
+
50
+ Aside from getting data for a station, you can also query the api using a
51
+ variety of location targets (lat, lng, zip, address). See the
52
+ [CIMIS API reference](http://et.water.ca.gov/Rest/Index) for more details.
53
+ ```ruby
54
+ Cimis.data({
55
+ targets: "lat=34.99,lng=-119.34",
56
+ start_date: "2015-10-10",
57
+ end_date: "2015-10-12"
58
+ })
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
64
+ run `rake test` to run the tests. You can also run `bin/console` for an
65
+ interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`.
68
+ To release a new version, update the version number in `version.rb`, and then
69
+ run `bundle exec rake release`, which will create a git tag for the version,
70
+ push git commits and tags, and push the `.gem` file to
71
+ [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at
76
+ https://github.com/[USERNAME]/cimis-ruby. This project is intended to be a
77
+ safe, welcoming space for collaboration, and contributors are expected to
78
+ adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the
84
+ [MIT License](http://opensource.org/licenses/MIT).
85
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cimis"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cimis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cimis-ruby"
8
+ spec.version = Cimis::VERSION
9
+ spec.authors = ["dphaener"]
10
+ spec.email = ["dphaener@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby wrapper for the CIMIS data API}
13
+ spec.description = %q{A Ruby wrapper for the CIMIS data API}
14
+ spec.homepage = "https://github.com/EditLLC/cimis-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "shoulda-context", "~> 1.0"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "pry"
27
+
28
+ spec.add_dependency "faraday", "~> 0.9"
29
+ spec.add_dependency "faraday_middleware", "~> 0.9"
30
+ spec.add_dependency "json", "~> 1.8"
31
+ spec.add_dependency "virtus", "~> 1.0"
32
+ end
data/lib/cimis.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ require 'cimis/version'
5
+ require 'cimis/station'
6
+ require 'cimis/station_data'
7
+ require 'cimis/data_point'
8
+
9
+ module Cimis
10
+ class MissingAttributeError < StandardError; end;
11
+
12
+ class << self
13
+ attr_accessor :app_key
14
+
15
+ def configure
16
+ yield self
17
+ raise MissingAttributeError, "You must include the app key" unless app_key
18
+ end
19
+
20
+ def connection
21
+ @connect ||= Faraday.new do |f|
22
+ f.adapter :net_http
23
+ f.url_prefix = "http://et.water.ca.gov/api"
24
+ f.headers["User-Agent"] = "Cimis Ruby v#{Cimis::VERSION}"
25
+ f.headers["Content-Type"] = "application/json"
26
+ f.headers["Accept"] = "*/*"
27
+ f.response :json, content_type: /\bjson$/
28
+ end
29
+ end
30
+
31
+ def data(options = {})
32
+ options.merge!({app_key: app_key})
33
+ response = connection.get("data?#{to_query(options)}")
34
+ response.body["Data"]["Providers"].first["Records"].map do |record|
35
+ StationData.new(record)
36
+ end
37
+ end
38
+
39
+ def to_query(params = {})
40
+ params.collect do |key, value|
41
+ "#{camel_case_lower(key.to_s)}=#{value}"
42
+ end.sort * "&"
43
+ end
44
+
45
+ def camel_case_lower(str)
46
+ str.split('_').inject([]) do |buffer,e|
47
+ buffer.push(buffer.empty? ? e : e.capitalize)
48
+ end.join
49
+ end
50
+
51
+ def underscore(str)
52
+ word = str.dup
53
+ word.gsub!(/::/, '/')
54
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
55
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
56
+ word.tr!("-", "_")
57
+ word.downcase!
58
+ word
59
+ end
60
+
61
+ def symbolize_keys(params)
62
+ {}.tap do |hsh|
63
+ params.keys.each { |k| hsh[underscore(k).to_sym] = params[k] }
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,15 @@
1
+ require 'virtus'
2
+
3
+ module Cimis
4
+ class DataPoint
5
+ include Virtus.model
6
+
7
+ attribute :value, Float
8
+ attribute :qc, String
9
+ attribute :unit, String
10
+
11
+ def initialize(params)
12
+ super(Cimis.symbolize_keys(params))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,61 @@
1
+ require 'virtus'
2
+
3
+ module Cimis
4
+ class Station
5
+ include Virtus.model
6
+
7
+ attribute :station_nbr, Integer
8
+ attribute :name, String
9
+ attribute :city, String
10
+ attribute :regional_office, String
11
+ attribute :county, String
12
+ attribute :connect_date, DateTime
13
+ attribute :disconnect_date, DateTime
14
+ attribute :is_active, Boolean
15
+ attribute :is_eto_station, Boolean
16
+ attribute :elevation, Integer
17
+ attribute :ground_cover, String
18
+ attribute :hms_latitude, Float
19
+ attribute :hms_longitude, Float
20
+ attribute :zip_codes, Array[String]
21
+
22
+ def initialize(params)
23
+ processed_params = process_params(params)
24
+ super(processed_params)
25
+ end
26
+
27
+ def self.all
28
+ response = Cimis.connection.get("station")
29
+ response.body["Stations"].map do |station|
30
+ new(station)
31
+ end
32
+ end
33
+
34
+ def process_params(params)
35
+ symbolized = Cimis.symbolize_keys(params)
36
+ symbolized[:hms_latitude] = extract_coordinate(symbolized[:hms_latitude])
37
+ symbolized[:hms_longitude] = extract_coordinate(symbolized[:hms_longitude])
38
+ symbolized[:connect_date] = parse_date(symbolized[:connect_date])
39
+ symbolized[:disconnect_date] = parse_date(symbolized[:disconnect_date])
40
+ symbolized
41
+ end
42
+
43
+ def extract_coordinate(str)
44
+ word = str.dup
45
+ word = word.split("/").last
46
+ word.strip!
47
+ end
48
+
49
+ def parse_date(date_str)
50
+ DateTime.strptime(date_str, "%m/%d/%Y")
51
+ end
52
+
53
+ def data(options = {})
54
+ options.merge!({app_key: Cimis.app_key, targets: station_nbr})
55
+ response = Cimis.connection.get("data?#{Cimis.to_query(options)}")
56
+ response.body["Data"]["Providers"].first["Records"].map do |record|
57
+ StationData.new(record)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,32 @@
1
+ require 'virtus'
2
+ require 'cimis/data_point'
3
+
4
+ module Cimis
5
+ class StationData
6
+ include Virtus.model
7
+
8
+ attribute :julian, Integer
9
+ attribute :station, Integer
10
+ attribute :standard, String
11
+ attribute :zip_codes, Array[String]
12
+ attribute :scope, String
13
+ attribute :day_air_tmp_avg, Cimis::DataPoint
14
+ attribute :day_air_tmp_max, Cimis::DataPoint
15
+ attribute :day_air_tmp_min, Cimis::DataPoint
16
+ attribute :day_dew_pnt, Cimis::DataPoint
17
+ attribute :day_asce_eto, Cimis::DataPoint
18
+ attribute :day_precip, Cimis::DataPoint
19
+ attribute :day_rel_hum_avg, Cimis::DataPoint
20
+ attribute :day_rel_hum_max, Cimis::DataPoint
21
+ attribute :day_rel_hum_min, Cimis::DataPoint
22
+ attribute :day_soil_tmp_avg, Cimis::DataPoint
23
+ attribute :day_sol_rad_avg, Cimis::DataPoint
24
+ attribute :day_vap_pres_avg, Cimis::DataPoint
25
+ attribute :day_wind_run, Cimis::DataPoint
26
+ attribute :day_wind_spd_avg, Cimis::DataPoint
27
+
28
+ def initialize(params)
29
+ super(Cimis.symbolize_keys(params))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Cimis
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cimis-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dphaener
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-13 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: shoulda-context
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday_middleware
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: json
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.8'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: virtus
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.0'
139
+ description: A Ruby wrapper for the CIMIS data API
140
+ email:
141
+ - dphaener@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - CODE_OF_CONDUCT.md
148
+ - Gemfile
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - bin/console
153
+ - bin/setup
154
+ - cimis-ruby.gemspec
155
+ - lib/cimis.rb
156
+ - lib/cimis/data_point.rb
157
+ - lib/cimis/station.rb
158
+ - lib/cimis/station_data.rb
159
+ - lib/cimis/version.rb
160
+ homepage: https://github.com/EditLLC/cimis-ruby
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.5
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: A Ruby wrapper for the CIMIS data API
184
+ test_files: []