holidapi 0.0.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
+ SHA1:
3
+ metadata.gz: e2f73da6cef2ef3971334b6586327980710312b3
4
+ data.tar.gz: 92190d5e6dc25eb23da1ebd4b9aa29d209e9a402
5
+ SHA512:
6
+ metadata.gz: 9233e616c2046171140b06bc5d7730ec90c7aafc6322836a738abf93321ffef4b54a5cbf263cd358366a00cdd7013037ee4b5b03bf33b5c9fe1fdf86e758f76e
7
+ data.tar.gz: 6cf5d325f9f088822f88018c910c5cb5906cee5610f094fcafab27ddc802ea2e2d859060a1ba12e648b178d4b4a846fde31e431fcf9ee268172566145f79e5b9
data/.gitignore ADDED
@@ -0,0 +1,42 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ # For MacOS:
17
+ #
18
+ .DS_Store
19
+
20
+ # For TextMate
21
+ *.tmproj
22
+ tmtags
23
+
24
+ # For emacs:
25
+ #*~
26
+ #\#*
27
+ #.\#*
28
+
29
+ # For vim:
30
+ #*.swp
31
+
32
+ # For redcar:
33
+ #.redcar
34
+
35
+ # For rubinius:
36
+ #*.rbc
37
+
38
+ # Ruby managers
39
+ .rvmrc
40
+ .ruby-version
41
+
42
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in holidapi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # HolidApi
2
+
3
+ HolidApi is a Ruby gem that provides a wrapper for interacting with the [Holiday APi service](http://holidayapi.com).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'holidapi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install holidapi
20
+
21
+ ## Usage
22
+
23
+ require 'holidapi'
24
+
25
+ holidays = HolidApi.get
26
+
27
+ The following options can also be used
28
+
29
+ holidays = HolidApi.get(country: 'gb', year: 2016, month: 1, day: 15)
30
+
31
+ The gem defaults to `US` for the country, and the current year
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/holidapi/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'documentation']
8
+ end
9
+
10
+ task :default => :spec
data/holidapi.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'holidapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "holidapi"
8
+ spec.version = HolidApi::VERSION
9
+ spec.authors = ["Brian Getting"]
10
+ spec.email = ["brian@tatem.ae"]
11
+ spec.summary = %q{A Ruby wrapper for the Holiday API service.}
12
+ spec.description = %q{A Ruby wrapper for the Holiday API service.}
13
+ spec.homepage = "http://github.com/tatemae-consultancy/holidapi"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'fakeweb', '~> 1.3'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.1'
25
+ spec.add_development_dependency 'timecop', '~> 0.7'
26
+
27
+ spec.add_runtime_dependency 'httparty'
28
+ spec.add_runtime_dependency 'json'
29
+ end
data/lib/holidapi.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+ require 'holidapi/version'
5
+
6
+ module HolidApi
7
+ include HTTParty
8
+
9
+ # Set the base_uri for HTTParty
10
+ base_uri 'http://holidayapi.com/v1'
11
+
12
+ # Set headers for HTTParty
13
+ headers({
14
+ 'User-Agent' => "ruby-holidapi-#{VERSION}",
15
+ 'Content-Type' => 'application/json; charset=utf-8',
16
+ 'Accept-Encoding' => 'gzip, deflate'
17
+ })
18
+
19
+ class << self
20
+
21
+ # Get the holidays from the API.
22
+ # Default country: "US"
23
+ # Default year: Time.now.year
24
+ #
25
+ # @param [Hash] options URI parameters
26
+ # @options [String] country ISO 3166-1 alpha-2 format (BE, GB or US)
27
+ # @options [Integer] year ISO 8601 format (YYYY)
28
+ # @options [Integer] month
29
+ # @options [Integer] day
30
+ #
31
+ # @see http://holidayapi.com
32
+ #
33
+ # @example
34
+ # holidays = HolidApi.get
35
+ # next_year = HolidApi.get(year: 2016)
36
+ # england = HolidApie.get(country: 'gb')
37
+ #
38
+ def get(params = {})
39
+ opts = { country: 'us', year: Time.now.year }
40
+ query = URI.encode_www_form(opts.merge(params))
41
+ handle_response super('/holidays', query: query)
42
+ end
43
+
44
+ # Handle the response from the API
45
+ #
46
+ # @nodoc
47
+ #
48
+ def handle_response(response)
49
+ case response.code.to_i
50
+ when 200
51
+ JSON.parse(response)['holidays']
52
+ when 400
53
+ raise BadRequest.new response.parsed_response
54
+ when 401
55
+ raise Unauthorized.new
56
+ when 404
57
+ raise NotFound.new
58
+ when 400...500
59
+ raise ClientError.new response.parsed_response
60
+ when 500...600
61
+ raise ServerError.new
62
+ else
63
+ response
64
+ end
65
+ end
66
+ end
67
+
68
+ # Error classes
69
+ class ClientError < StandardError; end
70
+ class ServerError < StandardError; end
71
+ class BadRequest < StandardError; end
72
+ class Unauthorized < StandardError; end
73
+ class NotFound < StandardError; end
74
+ class Unavailable < StandardError; end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ module HolidApi
2
+ VERSION = "0.0.1"
3
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'holidapi'
@@ -0,0 +1,27 @@
1
+ {
2
+ "this_year": {
3
+ "status": 200,
4
+ "holidays": {
5
+ "2015-01-01": [
6
+ {
7
+ "name": "New Year\u2019s Day",
8
+ "country": "US",
9
+ "date": "2015-01-01"
10
+ }
11
+ ]
12
+ }
13
+ },
14
+
15
+ "next_year": {
16
+ "status": 200,
17
+ "holidays": {
18
+ "2016-01-01": [
19
+ {
20
+ "name": "New Year\u2019s Day",
21
+ "country": "US",
22
+ "date": "2016-01-01"
23
+ }
24
+ ]
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe HolidApi do
4
+ it 'sets the base_uri for HTTParty' do
5
+ expect(HolidApi.base_uri).to eq('http://holidayapi.com/v1')
6
+ end
7
+
8
+ it 'sets the headers for HTTParty' do
9
+ headers = {
10
+ "User-Agent" => "ruby-holidapi-#{HolidApi::VERSION}",
11
+ "Content-Type" => "application/json; charset=utf-8",
12
+ "Accept-Encoding" => "gzip, deflate"
13
+ }
14
+ expect(HolidApi.headers).to eq(headers)
15
+ end
16
+
17
+ context 'usage' do
18
+ before :each do
19
+ @fixtures = JSON.parse(File.read('spec/fixtures/holidays.json'))
20
+ end
21
+
22
+ it 'returns the holidays' do
23
+ fixture = @fixtures['this_year']
24
+ register_fake_uri(fixture, country: 'us', year: 2015)
25
+ h = HolidApi.get
26
+ expect(h.keys.first).to eq(fixture['holidays'].keys.first)
27
+ end
28
+
29
+ it 'accepts params' do
30
+ fixture = @fixtures['next_year']
31
+ register_fake_uri(fixture, country: 'gb', year: 2016, month: 1, day: 1)
32
+ h = HolidApi.get(country: 'gb', year: 2016, month: 1, day: 1)
33
+ expect(h.keys.first).to eq(fixture['holidays'].keys.first)
34
+ end
35
+ end
36
+
37
+ context 'error handling' do
38
+ before :each do
39
+ @params = { country: 'us', year: 2015 }
40
+ end
41
+ it 'handles 400 errors' do
42
+ register_fake_uri('', @params, ['400', 'Bad Request'])
43
+ expect{ HolidApi.get }.to raise_error(HolidApi::BadRequest)
44
+ end
45
+
46
+ it 'handles 401 errors' do
47
+ register_fake_uri('', @params, ['401', 'Unauthorized'])
48
+ expect{ HolidApi.get }.to raise_error(HolidApi::Unauthorized)
49
+ end
50
+
51
+ it 'handles 404 errors' do
52
+ register_fake_uri('', @params, ['404', 'Not Found'])
53
+ expect{ HolidApi.get }.to raise_error(HolidApi::NotFound)
54
+ end
55
+
56
+ it 'handles generic 400 errors' do
57
+ register_fake_uri('', @params, ['450', 'Who Knows'])
58
+ expect{ HolidApi.get }.to raise_error(HolidApi::ClientError)
59
+ end
60
+
61
+ it 'handles generic 400 errors' do
62
+ register_fake_uri('', @params, ['500', 'Server Error'])
63
+ expect{ HolidApi.get }.to raise_error(HolidApi::ServerError)
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,33 @@
1
+ require 'fakeweb'
2
+ require 'pry'
3
+ require 'timecop'
4
+ require 'holidapi'
5
+
6
+ # Do not allow real HTTP connections during testing
7
+ FakeWeb.allow_net_connect = false
8
+
9
+ # Clean the FakeWeb registry before each test
10
+ RSpec.configure do |config|
11
+ config.before(:each) do
12
+ FakeWeb.clean_registry
13
+ Timecop.freeze(Time.local(2015))
14
+ end
15
+ end
16
+
17
+ # Register a fake URI with Fakeweb, and return fixture data
18
+ #
19
+ # @param [Object] fixture The fixture to return as JSON
20
+ # @param [Hash] params The query params to include in the URI
21
+ #
22
+ # @example
23
+ # fixture = @fixtures['this_year']
24
+ # register_fake_uri(fixture, country: 'us', year: 2015)
25
+ #
26
+ def register_fake_uri(fixture, params = {}, status = ['200', 'OK'])
27
+ FakeWeb.register_uri(
28
+ :get,
29
+ "#{HolidApi.base_uri}/holidays?#{URI.encode_www_form(params)}",
30
+ body: fixture.to_json,
31
+ status: status
32
+ )
33
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: holidapi
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: 2015-01-06 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakeweb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A Ruby wrapper for the Holiday API service.
112
+ email:
113
+ - brian@tatem.ae
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - holidapi.gemspec
124
+ - lib/holidapi.rb
125
+ - lib/holidapi/version.rb
126
+ - rails/init.rb
127
+ - spec/fixtures/holidays.json
128
+ - spec/holidapi/holidapi_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://github.com/tatemae-consultancy/holidapi
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.2.2
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A Ruby wrapper for the Holiday API service.
154
+ test_files:
155
+ - spec/fixtures/holidays.json
156
+ - spec/holidapi/holidapi_spec.rb
157
+ - spec/spec_helper.rb