sncf 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/bin/console +11 -0
- data/bin/setup +7 -0
- data/lib/sncf/api_client.rb +53 -0
- data/lib/sncf/api_response.rb +24 -0
- data/lib/sncf/version.rb +3 -0
- data/lib/sncf.rb +6 -0
- data/sncf.gemspec +34 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67268cd1f99364185d187b698ad29e74410b89ec
|
4
|
+
data.tar.gz: 9de2b1a2935e201953c344b0a836e6ea31458b2c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 736b834f00c37a36121512ead4bb49162f553e08b288b02c4cc2f6ba9ab2d8fd6d46242a137bc0011e47305d95f6a601377fd5465cf37a02b58db6ebeee600c9
|
7
|
+
data.tar.gz: 4e8b605ae94267cc83f591d1dd476456dd83e41cebaa51fd42e9256e8163408f6edaf1de311a33b545d14d1aee43d52ed4ca16fb6bb8801a54f1a04933f313b9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.1
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Rémi Delhaye
|
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,54 @@
|
|
1
|
+
# Sncf ruby API
|
2
|
+
|
3
|
+
Ruby gem to use easily the SNCF Open Data API with Ruby.
|
4
|
+
|
5
|
+
API Documentation is available [here](https://data.sncf.com/api)
|
6
|
+
|
7
|
+
You don't already have an API KEY ? [Register here!](https://data.sncf.com/api/register)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'sncf'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install sncf
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'sncf'
|
29
|
+
|
30
|
+
# Create your Sncf::ApiClient with your API_KEY
|
31
|
+
client = Sncf::ApiClient.new('MY_API_KEY')
|
32
|
+
|
33
|
+
client.api_key
|
34
|
+
=> 'MY_API_KEY'
|
35
|
+
|
36
|
+
response = req.fetch('coverage')
|
37
|
+
=> #<Sncf::ApiResponse:0x007f8b0592abe8
|
38
|
+
@body = #<HTTP::Response::Body:3fc582c9818c @streaming=true>,
|
39
|
+
@content = { ... }, # Here is the api response
|
40
|
+
@pagination = nil,
|
41
|
+
@response = #<HTTP::Response/1.1 200 OK ...>,
|
42
|
+
@start_page = 0>
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
Available `fetch` parameters and API actions are here [https://data.sncf.com/api/documentation#title.1.2.3](https://data.sncf.com/api/documentation#title.1.2.3)
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/rdlh/sncf/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
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,53 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'oj'
|
3
|
+
|
4
|
+
module Sncf
|
5
|
+
class ApiClient
|
6
|
+
HOST_URL = 'https://api.sncf.com'
|
7
|
+
BASE_PATH = 'v1'
|
8
|
+
PLAN = {
|
9
|
+
name: 'Free',
|
10
|
+
limits: {
|
11
|
+
per_day: 3_000,
|
12
|
+
per_month: 90_000
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
# => 3aa53f6d-13f1-486b-8a68-a781842cbe73
|
17
|
+
|
18
|
+
attr_reader :api_key
|
19
|
+
|
20
|
+
def initialize(api_key)
|
21
|
+
if !api_key.is_a?(String) || api_key.strip == ''
|
22
|
+
raise ArgumentError, "You must specify your SNCF api_key as a non-empty string."
|
23
|
+
else
|
24
|
+
@api_key = api_key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def quotas
|
29
|
+
@quotas ||= begin
|
30
|
+
{ per_day: PLAN[:limits][:per_day], per_month: PLAN[:limits][:per_month] }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def fetch(path, additional_params = nil)
|
35
|
+
query = construct_formated_url path, additional_params
|
36
|
+
response = Http.basic_auth(user: @api_key, pass: nil).get query
|
37
|
+
if response.code == 200
|
38
|
+
Sncf::ApiResponse.new response, query
|
39
|
+
else
|
40
|
+
raise ArgumentError, "Unauthorized (#{response.code}) #{response.body}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def construct_formated_url(path, additional_params)
|
47
|
+
if additional_params.nil?
|
48
|
+
"#{HOST_URL}/#{BASE_PATH}/#{path}"
|
49
|
+
else
|
50
|
+
"#{HOST_URL}/#{BASE_PATH}/#{path}?#{additional_params.to_query}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'oj'
|
3
|
+
module Sncf
|
4
|
+
class ApiResponse
|
5
|
+
attr_reader :response, :body, :content, :pagination, :start_page
|
6
|
+
|
7
|
+
def initialize(response, query)
|
8
|
+
@response = response
|
9
|
+
@query = query
|
10
|
+
@body = @response.body
|
11
|
+
@content = ''
|
12
|
+
loop do
|
13
|
+
chunk = @body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil
|
14
|
+
break if chunk.nil?
|
15
|
+
@content << chunk
|
16
|
+
end
|
17
|
+
if @response.content_type.mime_type == 'application/json'
|
18
|
+
@content = Oj.load(@content)
|
19
|
+
@pagination = @content['pagination']
|
20
|
+
@start_page = @pagination ? @pagination['start_page'] : 0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/sncf/version.rb
ADDED
data/lib/sncf.rb
ADDED
data/sncf.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/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sncf"
|
8
|
+
spec.version = Sncf::VERSION
|
9
|
+
spec.authors = ["Rémi Delhaye"]
|
10
|
+
spec.email = ["remi@libertrip.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{SNCF open data API ruby gem}
|
13
|
+
spec.description = %q{SNCF open source API. Documentation here: https://data.sncf.com/api/documentation }
|
14
|
+
spec.homepage = "https://github.com/rdlh/sncf"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split("\n").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.0.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,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sncf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rémi Delhaye
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-25 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. Documentation here: https://data.sncf.com/api/documentation '
|
140
|
+
email:
|
141
|
+
- remi@libertrip.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".ruby-version"
|
149
|
+
- Gemfile
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- bin/console
|
154
|
+
- bin/setup
|
155
|
+
- lib/sncf.rb
|
156
|
+
- lib/sncf/api_client.rb
|
157
|
+
- lib/sncf/api_response.rb
|
158
|
+
- lib/sncf/version.rb
|
159
|
+
- sncf.gemspec
|
160
|
+
homepage: https://github.com/rdlh/sncf
|
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: 2.0.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.6
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: SNCF open data API ruby gem
|
184
|
+
test_files: []
|