sncf_api 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: 5b923412ac426e50bfe86de63178c89d2a90cfd9
4
+ data.tar.gz: 0456c4718fc589184e5871627040c0ec21a59dbc
5
+ SHA512:
6
+ metadata.gz: 9571221d5b4e1ac0f146f6f673fef87bc09bf6091793b736dac403703a47dee79d654e6de8c6443b6a8d35b6100cd1502d814d1d3a610acbfd9e7495b26cfeb3
7
+ data.tar.gz: 49915d7698a0ffd4b8fb26ac2ec50e6ed24864cad0a53714a660cfb8b288ffd6a11b31336ea4382fa1c65ea762a04b88a29b91b21fa7ad6556c5035582b5b89d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.2.1
5
+ script: bundle exec rspec spec
@@ -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, 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,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Lock bundler here to help travis to work correctly
4
+ gem 'bundler', '>= 1.7'
5
+ # Specify your gem's dependencies in sncf_api.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,52 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(%r{^spec/.+_spec\.rb$})
49
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
50
+ watch(%r{^lib/sncf_api/(.+)\.rb$}) { |m| "spec/lib/sncf_api/#{m[1]}_spec.rb" }
51
+ watch('spec/spec_helper.rb') { "spec" }
52
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Thomas Lecavelier
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,59 @@
1
+ # SncfApi
2
+
3
+ Ruby gem to consume SNCF API (available from https://data.sncf.com/api)
4
+
5
+ This API let you access SNCF routes, schedules, stop points, etc.
6
+
7
+ Note: you have to request a user token from SNCF to use this API, so this gem (https://data.sncf.com/api/register)
8
+
9
+ [![Build Status](https://travis-ci.org/ook/sncf_api.svg)](https://travis-ci.org/ook/sncf_api)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ Note: I haven't published the gem yet. For now point to this github account:
16
+
17
+ ```ruby
18
+ gem 'sncf_api', git: 'https://github.com/ook/sncf_api', branch: 'master'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install sncf_api
28
+
29
+ ## Usage
30
+
31
+ You have to pass your token either via environment variable `ENV['SNCF_API_TOKEN']` or via constructor.
32
+
33
+ ```ruby
34
+ require 'sncf_api' # eventually
35
+
36
+ # Take an instance linked to your API TOKEN. You can avoid passing api_token if ENV['SNCF_API_TOKEN'] is defined
37
+ req = SncfApi::Request.instance(api_token: 'YOURTOKEN')
38
+ => #<SncfApi::Request:0x007f95ab1162a8 @api_token="YOURTOKEN", @countdown={:per_day=>2997, :per_month=>89997, :per_month_started_at=>2015-06-19 12:01:03 UTC, :per_day_started_at=>2015-06-19 12:01:03 UTC}, @plan={:per_day=>3000, :per_month=>90000}>
39
+
40
+ # At this level, you can build raw requests like this:
41
+ req.fetch('/coverage/sncf/stop_areas')
42
+ => { … } # giant Hash instance
43
+ # Note that if the mime type is json, it's automatically loaded into Hash. Raw String will be returned if it's not the case.
44
+
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/ook/sncf_api/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sncf_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
11
+
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,80 @@
1
+ require 'http'
2
+ require 'oj'
3
+
4
+ module SncfApi
5
+ class Request
6
+ IMPLEMENTATION = '20150615'
7
+ BASE_URL = 'https://api.sncf.com/v1'
8
+
9
+ class << self
10
+ def instance(api_token: ENV['SNCF_API_TOKEN'], plan: {name: 'Free', limits: { per_day: 3_000, per_month: 90_000 }})
11
+ api_token = nil if !api_token.is_a?(String) || api_token.strip == ''
12
+ raise ArgumentError, "You MUST specify either api_token argument nor SNCF_API_TOKEN env variable" unless api_token
13
+ @instances ||= {}
14
+ @instances[api_token] ||= new(plan: plan[:limits], api_token: api_token)
15
+ end
16
+
17
+ def drop_instance(api_token: ENV['SNCF_API_TOKEN'])
18
+ @instances.delete(api_token)
19
+ @instances.count
20
+ end
21
+ end
22
+
23
+ # My understanding of SNCF quota is that the countdown start at the very first call of a dau and the plan start on that instant
24
+ def countdown
25
+ @countdown ||= default_countdown
26
+ end
27
+
28
+ KNOWN_HTTP_ERROR_CODES = [400, 401, 404, 500]
29
+ def fetch(path)
30
+ response = Http.basic_auth(:user => @api_token, :pass => nil).get(BASE_URL + path)
31
+ body = response.body
32
+ decrease_quotas
33
+ if response.code == 200
34
+ content = ''
35
+ loop do
36
+ chunk = body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil
37
+ break if chunk.nil?
38
+ content << chunk
39
+ end
40
+ content = Oj.load(content) if response.content_type.mime_type == 'application/json'
41
+ return content
42
+ end
43
+ if KNOWN_HTTP_ERROR_CODES.include?(response.code)
44
+ raise ArgumentError, "Unauthorized (#{response.code}) #{response.body}"
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def initialize(plan:, api_token:)
51
+ @plan = plan
52
+ @api_token = api_token
53
+ end
54
+
55
+ def default_countdown
56
+ { per_day: @plan[:per_day], per_month: @plan[:per_month], per_month_started_at: now, per_day_started_at: now }
57
+ end
58
+
59
+ def now
60
+ Time.now.utc
61
+ end
62
+
63
+ QUOTA_PERIODS = {
64
+ per_day: 24 * 60 * 60,
65
+ per_month: 30 * 24 * 60 * 60
66
+ }
67
+ def decrease_quotas
68
+ @countdown ||= default_countdown
69
+ right_now = now
70
+ QUOTA_PERIODS.each do |key, period|
71
+ if @countdown["#{key}_started_at".to_sym] <= (right_now - period)
72
+ @countdown[key] = @plan[key]
73
+ @countdown["#{key}_started_at".to_sym] = right_now
74
+ end
75
+ end
76
+ QUOTA_PERIODS.keys.each { |k| @countdown[k] -= 1 }
77
+ @countdown
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module SncfApi
2
+ VERSION = "0.1.0"
3
+ end
data/lib/sncf_api.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'sncf_api/version'
2
+ require 'sncf_api/request'
3
+
4
+ module SncfApi
5
+ # Your code goes here...
6
+ end
data/sncf_api.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sncf_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sncf_api"
8
+ spec.version = SncfApi::VERSION
9
+ spec.authors = ["Thomas Lecavelier"]
10
+ spec.email = ["thomas@lecavelier.name"]
11
+
12
+ spec.summary = %q{Implements general SNCF open data API}
13
+ spec.description = %q{SNCF open source API exposes plenty of services. This gem implements its access for ruby. API available at https://data.sncf.com/api/documentation }
14
+ spec.homepage = "https://github.com/ook/sncf_api"
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.required_ruby_version = '>= 2.1.0'
23
+
24
+ spec.add_runtime_dependency "http", "~> 0.8.12"
25
+ spec.add_runtime_dependency "oj", "~> 2.12"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.9"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.3"
30
+ spec.add_development_dependency "guard-rspec", "~> 4.5"
31
+ spec.add_development_dependency "pry", "~> 0.10"
32
+ spec.add_development_dependency "byebug", "~> 4.0"
33
+ spec.add_development_dependency "pry-byebug", "~> 3.1"
34
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sncf_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Lecavelier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.12
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.12
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.12'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.1'
139
+ description: 'SNCF open source API exposes plenty of services. This gem implements
140
+ its access for ruby. API available at https://data.sncf.com/api/documentation '
141
+ email:
142
+ - thomas@lecavelier.name
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".ruby-version"
150
+ - ".travis.yml"
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Guardfile
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - lib/sncf_api.rb
160
+ - lib/sncf_api/request.rb
161
+ - lib/sncf_api/version.rb
162
+ - sncf_api.gemspec
163
+ homepage: https://github.com/ook/sncf_api
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.1.0
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.5
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Implements general SNCF open data API
187
+ test_files: []