twm-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82eedfd49645226dc1b0660ccab0d298216e8b8b
4
+ data.tar.gz: 5feb1f95a11d91a0fe4369983e73a7fc34d4df10
5
+ SHA512:
6
+ metadata.gz: 3ba82700f5b3d05c9c2a6cf698147ddb2a769b72a9be001c491a5da34dc979df3d831b69bad78b402f869263cddd37af76c7491566c4ea1c101d0ea5ba0b17ed
7
+ data.tar.gz: e3b3f15925f17c2a057820c140aab8f2a0ec46638d4c6eb35793032aece065d97c467c86c11778175e0b768a7c6fb2d14aef8c25f5644e7b766707d72b5004f5
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .rbenv-gemsets
20
+ .rbenv-vars
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twm-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Getting
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # The Whale Museum API's
2
+
3
+ A Ruby gem for interacting with [The Whale Museum](http://whalemuseum.org) API's.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'twm-ruby'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Currently only the [Whale Hotline API](http://hotline.whalemuseum.org/api) is supported. There is no authentication required, however usage of the API requires that The Whale Museum receive full acknowledgment as the data source through citation where the data is used.
16
+
17
+ ```ruby
18
+ require 'twm-ruby'
19
+
20
+ twm = TWM::API.new
21
+
22
+ total_count = twm.sightings.count
23
+ => 17829
24
+
25
+ sightings = twm.sightings.search(limit: 1000)
26
+ => [...]
27
+
28
+ orca_count = twm.sightings.count(species: 'orca')
29
+ => 17179
30
+
31
+ pages = (orca_count.to_f/1000.to_f).ceil
32
+ orca_sightings = []
33
+ 1.upto(pages) do |page|
34
+ orca_sightings.concat twm.sightings.search(limit: 1000, page: page)
35
+ end
36
+
37
+ orca_sightings.first['id']
38
+ => "52c4a6da686f741c25000000"
39
+
40
+ sighting = twm.sightings.find("52c4a6da686f741c25000000")
41
+ sighting['species']
42
+ => 'orca'
43
+
44
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,83 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ require 'twm-ruby/version'
5
+ require 'twm-ruby/errors'
6
+ require 'twm-ruby/api'
7
+
8
+ module TWM
9
+ class API
10
+ attr_accessor :debug, :session
11
+
12
+ def initialize(debug=false)
13
+ @debug = debug
14
+ end
15
+
16
+ def create_session(url) # :nodoc:
17
+ @session = Faraday.new(url: url, headers: set_headers)
18
+ end
19
+
20
+ # Make a GET request with the session
21
+ #
22
+ def get(path, params={}) # :nodoc:
23
+ handle_response(@session.get(path, params))
24
+ end
25
+
26
+ # Make a POST request with the session
27
+ #
28
+ def post(path, params={}) # :nodoc:
29
+ handle_response(@session.post(path, MultiJson.dump(params)))
30
+ end
31
+
32
+ # Make a PUT request with the session
33
+ #
34
+ def put(path, params={}) # :nodoc:
35
+ handle_response(@session.put(path, MultiJson.dump(params)))
36
+ end
37
+
38
+ # Make a DELETE request with the session
39
+ #
40
+ def delete(path, params={}) # :nodoc:
41
+ handle_response(@session.delete(path, MultiJson.dump(params)))
42
+ end
43
+
44
+ def handle_response(response)
45
+ case response.status # :nodoc:
46
+ when 400
47
+ raise BadRequest.new response.body
48
+ when 401
49
+ raise Unauthorized.new
50
+ when 404
51
+ raise NotFound.new
52
+ when 400...500
53
+ raise ClientError.new response.body
54
+ when 500...600
55
+ raise ServerError.new
56
+ else
57
+ case response.body
58
+ when ''
59
+ true
60
+ when is_a?(Integer)
61
+ response.body
62
+ else
63
+ MultiJson.load(response.body)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Set the request headers
69
+ def set_headers # :nodoc:
70
+ {
71
+ 'User-Agent' => "twm-ruby-#{VERSION}",
72
+ 'Content-Type' => 'application/json; charset=utf-8',
73
+ 'Accept' => 'application/json'
74
+ }
75
+ end
76
+
77
+ # API nodes
78
+ def sightings # :nodoc:
79
+ Sightings.new self
80
+ end
81
+
82
+ end
83
+ end
Binary file
@@ -0,0 +1,87 @@
1
+ module TWM
2
+ #
3
+ # The Whale Hotline API
4
+ # http://hotline.whalemuseum.org/api
5
+ #
6
+ class Sightings
7
+ attr_accessor :api
8
+
9
+ def initialize(api)
10
+ @api = api
11
+ @api.create_session('http://hotline.whalemuseum.org')
12
+ end
13
+
14
+ # Retrieve Hotline sighting reports
15
+ #
16
+ # @param [Hash] params Optional params or filters:
17
+ # @option params [String] species Get sighting reports for a species.
18
+ # @option params [String] orca_type Get sighting reports for a type of orca.
19
+ # @option params [String] orca_pod Get sighting reports for a pod of orca.
20
+ # @option params [String] since Get sighting reports after this date.
21
+ # @option params [String] until Get sighting reports before this date.
22
+ # @option params [String] near Get sighting reports near a Lat/Lng point.
23
+ # @option params [String] radius Get sighting reports within this distance of center point.
24
+ #
25
+ # @return [Integer] The count of sighting reports.
26
+ #
27
+ # @see http://hotline.whalemuseum.org/api
28
+ #
29
+ # @example
30
+ #
31
+ # count = twm.sightings.count
32
+ # => 17000
33
+ #
34
+ # count = twm.sightings.count(species: 'orca')
35
+ #
36
+ def count(params = {})
37
+ @api.get("api/count.json", params)
38
+ end
39
+
40
+ # Retrieve a specific sighting report
41
+ #
42
+ # @param [String] id The ID of the sighting report to retrieve.
43
+ #
44
+ # @return [JSON] A sighting report object.
45
+ #
46
+ # @see http://hotline.whalemuseum.org/api
47
+ #
48
+ # @example
49
+ #
50
+ # sighting = twm.sightings.find('52b158c0686f7438d8ac0600')
51
+ # sighting['species']
52
+ # => "orca"
53
+ #
54
+ def find(id)
55
+ @api.get("api/#{id.to_s}.json")
56
+ end
57
+
58
+ # Retrieve Hotline sighting reports
59
+ #
60
+ # @param [Hash] params Optional params or filters:
61
+ # @option params [String] species Get sighting reports for a species.
62
+ # @option params [String] orca_type Get sighting reports for a type of orca.
63
+ # @option params [String] orca_pod Get sighting reports for a pod of orca.
64
+ # @option params [Integer] limit The number of sighting reports to return.
65
+ # @option params [Integer] page The page number to return.
66
+ # @option params [String] since Get sighting reports after this date.
67
+ # @option params [String] until Get sighting reports before this date.
68
+ # @option params [String] near Get sighting reports near a Lat/Lng point.
69
+ # @option params [String] radius Get sighting reports within this distance of center point.
70
+ #
71
+ # @return [JSON] An array of matching sighting reports.
72
+ #
73
+ # @see http://hotline.whalemuseum.org/api
74
+ #
75
+ # @example
76
+ #
77
+ # sightings = twm.sightings.search
78
+ # sightings.size
79
+ # => 17000
80
+ #
81
+ # sightings = twm.sightings.search(species: 'orca', limit: 250)
82
+ #
83
+ def search(params = {})
84
+ @api.get("api.json", params)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,18 @@
1
+ module TWM
2
+ class Error < StandardError
3
+ end
4
+ class BadRequest < Error
5
+ end
6
+ class ClientError < Error
7
+ end
8
+ class NotFound < Error
9
+ end
10
+ class ServerError < Error
11
+ end
12
+ class TooManyRequests < Error
13
+ end
14
+ class Unauthorized < Error
15
+ end
16
+ class Unavailable < Error
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module TWM
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'twm-ruby'
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twm-ruby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "twm-ruby"
8
+ gem.version = TWM::VERSION
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.authors = ["Brian Getting"]
11
+ gem.email = ["brian@tatem.ae"]
12
+ gem.homepage = "https://github.com/tatemae-consultancy/twm-ruby"
13
+ gem.summary = "Ruby wrapper for The Whale Museum API's"
14
+ gem.description = "A Ruby gem for interacting with The Whale Museum API's."
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'faraday', '>= 0.8.7'
22
+ gem.add_dependency 'multi_json', '~> 1.7.3'
23
+
24
+ gem.add_development_dependency "rspec"
25
+ gem.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twm-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Getting
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: A Ruby gem for interacting with The Whale Museum API's.
70
+ email:
71
+ - brian@tatem.ae
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/twm-ruby.rb
82
+ - lib/twm-ruby/.DS_Store
83
+ - lib/twm-ruby/api.rb
84
+ - lib/twm-ruby/errors.rb
85
+ - lib/twm-ruby/version.rb
86
+ - rails/init.rb
87
+ - twm-ruby.gemspec
88
+ homepage: https://github.com/tatemae-consultancy/twm-ruby
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruby wrapper for The Whale Museum API's
111
+ test_files: []