strans-client 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2f3edb1e916f3b63d86d2d02818669217b84cc1
4
- data.tar.gz: ef0936f9c63e5f88d95251fe9056b1a1fc4b2543
3
+ metadata.gz: fba9d2d2452214246d583bfe10be1c97efc86c69
4
+ data.tar.gz: 567a4c52548db9e38229d4841d0fca5247515917
5
5
  SHA512:
6
- metadata.gz: 1699064a738e96fc6bbe37672e7bd80058d6f6155a5d2972e9b8686787201c66d3ccd5f8098c40189155726f65f41f7a2abb6198eb3e014f4dd00598864e9815
7
- data.tar.gz: ccf8b1a7ae3942f5109f8600c7580856c94d08fc3172a731d04abdc367c1e8a7e9924fad12c515c69febb2f8e573a0de6561c89cd1455cb13578c53b43b1b63f
6
+ metadata.gz: 11d80eefc6d837a19bfd2b8ac2ed36fa691f5ec512c70b701bfe1c39fd1866f43fc9d56b645530fa93f35c8440846e68621821fb79e9af3f0283788eabd775a0
7
+ data.tar.gz: e75e1c362ecabe084a3a2aa64cea7fbddd16f8255ad9e77041e37f07d814742bda9a5fe304f82fc94d9d9dd8c0b452b5c96f8ddbfc96042ca57cce4f1dc172d1
data/lib/linha.rb CHANGED
@@ -1,33 +1,28 @@
1
1
 
2
2
  class Linha
3
3
 
4
- def initialize(fields)
5
- load_atts(fields)
6
- end
7
-
8
4
  attr_accessor :codigoLinha, :denomicao, :origem,
9
5
  :retorno, :circular, :veiculos, :paradas
10
6
 
11
- def load_atts(fields)
7
+ def initialize(fields)
12
8
  @codigoLinha = fields['CodigoLinha'] || fields[:codigoLinha]
13
9
  @denomicao = fields['Denomicao'] || fields[:denomicao]
14
10
  @origem = fields['Origem'] || fields[:origem]
15
11
  @retorno = fields['Retorno'] || fields[:retorno]
16
12
  @circular = fields['Circular'] || fields[:circular]
17
- @veiculos = load_objs(fields['Veiculos']||fields[:veiculos], Veiculo)
18
- @paradas = load_objs(fields['Paradas']||fields[:paradas], Parada)
13
+ @veiculos = load_objs(fields['Veiculos'], Veiculo)
14
+ @paradas = load_objs(fields['Paradas'], Parada)
19
15
  end
20
16
 
21
-
22
- def load_objs(objs, klass)
17
+ def load_objs(objs, k)
23
18
  if objs.is_a?(Array)
24
- objs = objs.map do |o|
25
- o = klass.new(o)
26
- o.linha = self
27
- o
19
+ map = objs.map do |o|
20
+ o = k.new(o)
21
+ o.linha = self
22
+ o
28
23
  end
29
24
  end
30
- return objs
25
+ return map
31
26
  end
32
27
 
33
28
  end
data/lib/request.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'json'
2
+
3
+ class Request
4
+
5
+ URL_API = "https://api.inthegra.strans.teresina.pi.gov.br/v1"
6
+
7
+ VERB_MAP = {
8
+ :get => Net::HTTP::Get,
9
+ :post => Net::HTTP::Post,
10
+ :put => Net::HTTP::Put,
11
+ :delete => Net::HTTP::Delete
12
+ }
13
+ def initialize(token)
14
+ @token = token
15
+ end
16
+
17
+ def encode_path_params(path, params)
18
+ encoded = URI.encode_www_form(params)
19
+ [path, encoded].join("?")
20
+ end
21
+
22
+ def send( method, path, params = nil )
23
+ uri = URI.parse(URL_API+path)
24
+ https = Net::HTTP.new(uri.host, uri.port)
25
+ https.use_ssl = true
26
+ if(method == :get)
27
+ uri.query = URI.encode_www_form(params) if (params)
28
+ end
29
+ req = VERB_MAP[method].new(uri, @token.header)
30
+ if (params && method != :get)
31
+ req.body = params.to_json
32
+ end
33
+ Response.new(https.request(req))
34
+ end
35
+
36
+ end
data/lib/response.rb ADDED
@@ -0,0 +1,64 @@
1
+
2
+
3
+ class Response
4
+
5
+ def initialize(resp)
6
+ @resp = resp
7
+ end
8
+
9
+ def model(path)
10
+ #
11
+ @resp = JSON.parse(@resp.body)
12
+
13
+ if(@resp.respond_to?('has_key') && @resp.has_key?('code'))
14
+ raise @resp['message']
15
+ end
16
+
17
+ case path
18
+ when :signin
19
+ token()
20
+ when :linhas
21
+ linhas()
22
+ when :veiculos
23
+ veiculos()
24
+ when :veiculos_linha
25
+ veiculos_linha()
26
+ when :paradas
27
+ paradas()
28
+ when :paradas_linha
29
+ paradas_linha()
30
+ end
31
+ end
32
+
33
+ def token()
34
+ @resp
35
+ end
36
+
37
+ def linhas()
38
+ @resp.map{|l| Linha.new(l) }
39
+ end
40
+
41
+ def veiculos()
42
+ v = @resp.map do |node|
43
+ linha = Linha.new(node['Linha'])
44
+ linha.veiculos
45
+ end
46
+ v.reduce(:concat)
47
+ end
48
+
49
+ def veiculos_linha()
50
+ puts @resp
51
+ linha = Linha.new(@resp['Linha'])
52
+ linha.veiculos
53
+ end
54
+
55
+ def paradas ()
56
+ @resp.map{|p| Parada.new(p) }
57
+ end
58
+
59
+ def paradas_linha()
60
+ @resp = @resp['Linha'].merge!('Paradas' => @resp['Paradas'])
61
+ Linha.new(@resp).paradas
62
+ end
63
+
64
+ end
data/lib/strans-client.rb CHANGED
@@ -1,100 +1,46 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
3
  require 'linha'
4
+ require 'token'
4
5
 
5
6
  class StransClient
6
7
 
7
- VERB_MAP = {
8
- :get => Net::HTTP::Get,
9
- :post => Net::HTTP::Post,
10
- :put => Net::HTTP::Put,
11
- :delete => Net::HTTP::Delete
8
+ PATHS = {
9
+ :signin => '/signin',
10
+ :linhas => '/linhas',
11
+ :veiculos => '/veiculos',
12
+ :veiculos_linha => '/veiculosLinha',
13
+ :paradas => '/paradas',
14
+ :paradas_linha => '/paradasLinha',
12
15
  }
13
16
 
14
- URL_API = "https://api.inthegra.strans.teresina.pi.gov.br/v1"
15
-
16
17
  def initialize( email, pass, key)
17
18
  @email = email
18
19
  @password = pass
19
20
  @key = key
20
- end
21
-
22
- def date_in_format()
23
- today = Time.now
24
- today.strftime("%a, %d %b %Y %H:%M:%S GMT")
25
- end
26
-
27
- def encode_path_params(path, params)
28
- encoded = URI.encode_www_form(params)
29
- [path, encoded].join("?")
30
- end
31
-
32
- def headers
33
- head ={
34
- 'Content-Type' => "application/json",
35
- 'Accept-Language' => "en",
36
- 'Date' => date_in_format(),
37
- 'X-Api-Key' => @key
38
- }
39
- head['X-Auth-Token'] = @token if(@token)
40
- head
41
- end
42
-
43
- def create_request ( method, path, params = nil )
44
- uri = URI.parse(URL_API+path)
45
- https = Net::HTTP.new(uri.host, uri.port)
46
- https.use_ssl = true
47
- if(method == :get)
48
- uri.query = URI.encode_www_form(params) if (params)
21
+ @token = Token.new(@key)
22
+ end
23
+
24
+ def get(path, busca = nil )
25
+ @request = Request.new(@token)
26
+ if(@token.valid?)
27
+ puts '#2'
28
+ busca = busca.nil? ? {} : {busca: busca}
29
+ @resp = @request.send(:get, PATHS[path], busca)
30
+ @resp.model(path)
31
+ else
32
+ puts '#1'
33
+ autentic()
34
+ get(path, busca)
49
35
  end
50
- req = VERB_MAP[method].new(uri, headers())
51
- if (params && method != :get)
52
- req.body = params.to_json
53
- end
54
- https.request(req)
55
36
  end
56
37
 
57
- def autentic
58
- body = { email: @email , password: @password }
59
- res = create_request( :post, "/signin", body)
60
- if(res.code == '200')
61
- body = JSON.parse(res.body)
62
- @token = body["token"]
63
- end
64
- !@token.nil?
65
- end
66
-
67
- def linhas( busca = nil)
68
- params = busca.nil? ? nil : {busca: busca}
69
- res = create_request( :get, "/linhas", params)
70
- JSON.parse(res.body).map{|l| Linha.new(l) }
71
- end
72
-
73
- def veiculos()
74
- res = create_request( :get, "/veiculos")
75
- veiculos = JSON.parse(res.body).map do |node|
76
- linha = Linha.new(node['Linha'])
77
- linha.veiculos
78
- end
79
- veiculos.reduce(:concat)
80
- end
38
+ private
81
39
 
82
- def veiculos_linha(num_linha)
83
- res = create_request( :get, "/veiculosLinha", {busca: num_linha})
84
- JSON.parse(res.body)
85
- end
86
-
87
- def paradas (busca = nil)
88
- params = busca.nil? ? nil : {busca: busca}
89
- res = create_request( :get, "/paradas", params)
90
- JSON.parse(res.body).map{|p| Parada.new(p) }
91
- end
92
-
93
- def paradas_linha(num_linha)
94
- res = create_request( :get, "/paradasLinha", {busca: num_linha})
95
- result = JSON.parse(res.body)
96
- result = result['Linha'].merge!( 'Paradas' => result['Paradas'])
97
- Linha.new(result).paradas
40
+ def autentic
41
+ credencials = { email:@email, password:@password }
42
+ @resp = @request.send(:post, PATHS[:signin], credencials)
43
+ @token = Token.new(@key, @resp.model(:signin))
98
44
  end
99
45
 
100
46
  end
data/lib/token.rb ADDED
@@ -0,0 +1,32 @@
1
+
2
+ class Token
3
+
4
+ def initialize (key, fields={})
5
+
6
+ @key = key
7
+ @create_at = Time.now
8
+ @minute_validate = fields[:minutes]
9
+ @token = fields['token']
10
+ end
11
+
12
+ def date_in_format()
13
+ today = Time.now
14
+ today.strftime("%a, %d %b %Y %H:%M:%S GMT")
15
+ end
16
+
17
+ def header
18
+ head ={}
19
+ head['Accept-Language'] = "en"
20
+ head['Content-Type'] = "application/json"
21
+ head['X-Auth-Token'] = @token if @token
22
+ head['X-Api-Key'] = @key
23
+ head['Date'] = date_in_format()
24
+ head
25
+ end
26
+
27
+ def valid?
28
+ puts @token
29
+ !@token.nil? && (Time.now - 10 * 60) <= @create_at
30
+ end
31
+
32
+ end
data/lib/veiculo.rb CHANGED
@@ -1,16 +1,12 @@
1
1
  class Veiculo
2
2
 
3
3
  def initialize(fields)
4
- load_atts(fields)
5
- end
6
-
7
- attr_accessor :codigoVeiculo, :hora, :lat, :long, :linha
8
-
9
- def load_atts(fields)
10
4
  @codigoVeiculo = fields['CodigoVeiculo'] || fields[:codigoVeiculo]
11
5
  @hora = fields['Hora'] || fields[:hora]
12
6
  @lat = fields['Lat'] || fields[:lat]
13
7
  @long = fields['Long'] || fields[:long]
14
8
  end
15
9
 
10
+ attr_accessor :codigoVeiculo, :hora, :lat, :long, :linha
11
+
16
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strans-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luan Pontes
@@ -18,7 +18,10 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/linha.rb
20
20
  - lib/parada.rb
21
+ - lib/request.rb
22
+ - lib/response.rb
21
23
  - lib/strans-client.rb
24
+ - lib/token.rb
22
25
  - lib/veiculo.rb
23
26
  homepage: https://github.com/tOOlmaker-equalsp/ruby-apiStransTHE
24
27
  licenses:
@@ -40,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
43
  version: '0'
41
44
  requirements: []
42
45
  rubyforge_project:
43
- rubygems_version: 2.2.2
46
+ rubygems_version: 2.4.8
44
47
  signing_key:
45
48
  specification_version: 4
46
49
  summary: strans-client