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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b923412ac426e50bfe86de63178c89d2a90cfd9
4
- data.tar.gz: 0456c4718fc589184e5871627040c0ec21a59dbc
3
+ metadata.gz: 32decfea52cb0ea7fe5bbc03dd9fe5ccf47c9c04
4
+ data.tar.gz: 4027556ee32fcb6b16c7046f10250ca4b0e490c2
5
5
  SHA512:
6
- metadata.gz: 9571221d5b4e1ac0f146f6f673fef87bc09bf6091793b736dac403703a47dee79d654e6de8c6443b6a8d35b6100cd1502d814d1d3a610acbfd9e7495b26cfeb3
7
- data.tar.gz: 49915d7698a0ffd4b8fb26ac2ec50e6ed24864cad0a53714a660cfb8b288ffd6a11b31336ea4382fa1c65ea762a04b88a29b91b21fa7ad6556c5035582b5b89d
6
+ metadata.gz: f2f053ac647f14b06d9eebb8fcacab9e36fc856c01b30e0660d5568feaff0c3b3c150d9e2e0d1760054f4de9fb3608fa6b4ff7d7ba107bdb917dffbbb45bb78c
7
+ data.tar.gz: e520c1667665ef6712543f1ebcd0b4fa22913393972aadbf57c609c865534cd6fa95b845a5d8ea36e6082660cc6677c5b2966f1ddfe4f9ce2a2c6a967dd289da
@@ -2,4 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1.5
4
4
  - 2.2.1
5
+ addons:
6
+ code_climate:
7
+ repo_token: 95f6584c5d7e081bbcde9b4de1c9564112a0f65cbdc3dff0054d571735af131f
5
8
  script: bundle exec rspec spec
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:
@@ -1,5 +1,6 @@
1
1
  require 'sncf_api/version'
2
2
  require 'sncf_api/request'
3
+ require 'sncf_api/response'
3
4
 
4
5
  module SncfApi
5
6
  # Your code goes here...
@@ -4,7 +4,7 @@ require 'oj'
4
4
  module SncfApi
5
5
  class Request
6
6
  IMPLEMENTATION = '20150615'
7
- BASE_URL = 'https://api.sncf.com/v1'
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
- 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
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
@@ -1,3 +1,3 @@
1
1
  module SncfApi
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "pry", "~> 0.10"
32
32
  spec.add_development_dependency "byebug", "~> 4.0"
33
33
  spec.add_development_dependency "pry-byebug", "~> 3.1"
34
+ spec.add_development_dependency "codeclimate-test-reporter"
34
35
  end
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: 0.1.0
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-19 00:00:00.000000000 Z
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