sncf_api 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +2 -1
- data/lib/sncf_api.rb +1 -0
- data/lib/sncf_api/request.rb +2 -18
- data/lib/sncf_api/response.rb +77 -0
- data/lib/sncf_api/version.rb +1 -1
- data/sncf_api.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32decfea52cb0ea7fe5bbc03dd9fe5ccf47c9c04
|
4
|
+
data.tar.gz: 4027556ee32fcb6b16c7046f10250ca4b0e490c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2f053ac647f14b06d9eebb8fcacab9e36fc856c01b30e0660d5568feaff0c3b3c150d9e2e0d1760054f4de9fb3608fa6b4ff7d7ba107bdb917dffbbb45bb78c
|
7
|
+
data.tar.gz: e520c1667665ef6712543f1ebcd0b4fa22913393972aadbf57c609c865534cd6fa95b845a5d8ea36e6082660cc6677c5b2966f1ddfe4f9ce2a2c6a967dd289da
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,7 @@ This API let you access SNCF routes, schedules, stop points, etc.
|
|
7
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
8
|
|
9
9
|
[![Build Status](https://travis-ci.org/ook/sncf_api.svg)](https://travis-ci.org/ook/sncf_api)
|
10
|
+
[![Code Climate](https://codeclimate.com/github/ook/sncf_api/badges/gpa.svg)](https://codeclimate.com/github/ook/sncf_api)
|
10
11
|
|
11
12
|
## Installation
|
12
13
|
|
@@ -15,7 +16,7 @@ Add this line to your application's Gemfile:
|
|
15
16
|
Note: I haven't published the gem yet. For now point to this github account:
|
16
17
|
|
17
18
|
```ruby
|
18
|
-
gem 'sncf_api', git: 'https://github.com/ook/sncf_api', branch: 'master'
|
19
|
+
gem 'sncf_api', git: 'https://github.com/ook/sncf_api.git', branch: 'master'
|
19
20
|
```
|
20
21
|
|
21
22
|
And then execute:
|
data/lib/sncf_api.rb
CHANGED
data/lib/sncf_api/request.rb
CHANGED
@@ -4,7 +4,7 @@ require 'oj'
|
|
4
4
|
module SncfApi
|
5
5
|
class Request
|
6
6
|
IMPLEMENTATION = '20150615'
|
7
|
-
|
7
|
+
attr_reader :api_token
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def instance(api_token: ENV['SNCF_API_TOKEN'], plan: {name: 'Free', limits: { per_day: 3_000, per_month: 90_000 }})
|
@@ -25,24 +25,8 @@ module SncfApi
|
|
25
25
|
@countdown ||= default_countdown
|
26
26
|
end
|
27
27
|
|
28
|
-
KNOWN_HTTP_ERROR_CODES = [400, 401, 404, 500]
|
29
28
|
def fetch(path)
|
30
|
-
|
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
|
29
|
+
SncfApi::Response.new(path: path, request: self)
|
46
30
|
end
|
47
31
|
|
48
32
|
private
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'oj'
|
3
|
+
module SncfApi
|
4
|
+
# A reponse represent a request response from API. Note that means only ONE PAGE AT A TIME
|
5
|
+
# There's helper methods like #each_page and #page(index) to help you to crawl easily in the
|
6
|
+
# result
|
7
|
+
class Response
|
8
|
+
BASE_URL = 'https://api.sncf.com/v1'
|
9
|
+
|
10
|
+
attr_reader :response, :request, :body, :content, :http_params
|
11
|
+
attr_reader :pagination, :start_page
|
12
|
+
|
13
|
+
KNOWN_HTTP_ERROR_CODES = [400, 401, 404, 500]
|
14
|
+
def initialize(path:, request:, http_params: nil)
|
15
|
+
@path = path
|
16
|
+
@http_params = http_params
|
17
|
+
@response = Http.basic_auth(:user => request.api_token, :pass => nil).get(BASE_URL + @path, params: @http_params)
|
18
|
+
@body = response.body
|
19
|
+
@request = request
|
20
|
+
@request.send :decrease_quotas
|
21
|
+
|
22
|
+
if @response.code == 200
|
23
|
+
@content = ''
|
24
|
+
loop do
|
25
|
+
chunk = @body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil
|
26
|
+
break if chunk.nil?
|
27
|
+
@content << chunk
|
28
|
+
end
|
29
|
+
if @response.content_type.mime_type == 'application/json'
|
30
|
+
@content = Oj.load(@content)
|
31
|
+
@pagination = @content['pagination']
|
32
|
+
@start_page = @pagination ? @pagination['start_page'] : 0
|
33
|
+
end
|
34
|
+
end
|
35
|
+
if KNOWN_HTTP_ERROR_CODES.include?(@response.code)
|
36
|
+
raise ArgumentError, "Unauthorized (#{@response.code}) #{@response.body}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# return Hash
|
41
|
+
def links
|
42
|
+
@content['links']
|
43
|
+
end
|
44
|
+
|
45
|
+
def page(index=0)
|
46
|
+
new_http_params = @http_params.dup
|
47
|
+
new_http_params['start_page'] = index
|
48
|
+
SncfApi::Response.new(path: @path, request: @request, http_params: new_http_params)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Execute given block from the current page until last rel=next
|
52
|
+
# * block param is the block that will be yield with the page content
|
53
|
+
# * return self
|
54
|
+
def each_page(&block)
|
55
|
+
yield @content
|
56
|
+
content = @content
|
57
|
+
loop do
|
58
|
+
next_page = content && content['links'] && content['links'].find { |e| e['type'] == 'next' }
|
59
|
+
break unless next_page
|
60
|
+
resp = Http.basic_auth(user: request.api_token, pass: nil).get(next_page['href'])
|
61
|
+
@request.send :decrease_quotas
|
62
|
+
break unless resp.code == 200
|
63
|
+
break unless resp.content_type.mime_type == 'application/json'
|
64
|
+
content = ''
|
65
|
+
loop do
|
66
|
+
chunk = resp.body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil
|
67
|
+
break if chunk.nil?
|
68
|
+
content << chunk
|
69
|
+
end
|
70
|
+
content = Oj.load(content)
|
71
|
+
yield content
|
72
|
+
end
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
data/lib/sncf_api/version.rb
CHANGED
data/sncf_api.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sncf_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Lecavelier
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '3.1'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: codeclimate-test-reporter
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: 'SNCF open source API exposes plenty of services. This gem implements
|
140
154
|
its access for ruby. API available at https://data.sncf.com/api/documentation '
|
141
155
|
email:
|
@@ -158,6 +172,7 @@ files:
|
|
158
172
|
- bin/setup
|
159
173
|
- lib/sncf_api.rb
|
160
174
|
- lib/sncf_api/request.rb
|
175
|
+
- lib/sncf_api/response.rb
|
161
176
|
- lib/sncf_api/version.rb
|
162
177
|
- sncf_api.gemspec
|
163
178
|
homepage: https://github.com/ook/sncf_api
|