strans-client 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/strans-client.rb +93 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1cb35d18c073fc060e0300bf0adf29be725b13cf
4
+ data.tar.gz: 4af402b94efe32c1bffef2acda7b73b23dda31cc
5
+ SHA512:
6
+ metadata.gz: 418cfce27a412da2705cf5436dd4962324f5a5cc3bc29ba2fa4773263d6f5cc1681cacb7f2595d64a9d4e97d460a92f5761ec24ed374dcf09b20ca03bd88472b
7
+ data.tar.gz: 5b9b416865c22285a92915ba3471024ff8c0a6b2391fb4cde382c970198f71f62831e09b16ebc069647b7d12c58aebaf40e03bd80ece1e0a5ed954a112e28641
@@ -0,0 +1,93 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class StransClient
5
+
6
+ VERB_MAP = {
7
+ :get => Net::HTTP::Get,
8
+ :post => Net::HTTP::Post,
9
+ :put => Net::HTTP::Put,
10
+ :delete => Net::HTTP::Delete
11
+ }
12
+
13
+ URL_API = "https://api.inthegra.strans.teresina.pi.gov.br/v1"
14
+
15
+ def initialize( email, pass, key)
16
+ @email = email
17
+ @password = pass
18
+ @key = key
19
+ end
20
+
21
+ def date_in_format()
22
+ today = Time.now
23
+ today.strftime("%a, %d %b %Y %H:%M:%S GMT")
24
+ end
25
+
26
+ def encode_path_params(path, params)
27
+ encoded = URI.encode_www_form(params)
28
+ [path, encoded].join("?")
29
+ end
30
+
31
+ def headers
32
+ head ={
33
+ 'Content-Type' => "application/json",
34
+ 'Accept-Language' => "en",
35
+ 'Date' => date_in_format(),
36
+ 'X-Api-Key' => @key
37
+ }
38
+ head['X-Auth-Token'] = @token if(@token)
39
+ head
40
+ end
41
+
42
+ def create_request ( method, path, params = nil )
43
+ uri = URI.parse(URL_API+path)
44
+ https = Net::HTTP.new(uri.host, uri.port)
45
+ https.use_ssl = true
46
+ if(method == :get)
47
+ uri.query = URI.encode_www_form(params) if (params)
48
+ end
49
+ req = VERB_MAP[method].new(uri, headers())
50
+ if (params && method != :get)
51
+ req.body = params.to_json
52
+ end
53
+ https.request(req)
54
+ end
55
+
56
+ def autentic
57
+ body = { email: @email , password: @password }
58
+ res = create_request( :post, "/signin", body)
59
+ if(res.code == '200')
60
+ body = JSON.parse(res.body)
61
+ @token = body["token"]
62
+ end
63
+ !@token.nil?
64
+ end
65
+
66
+ def linhas( busca = nil)
67
+ params = busca.nil? ? nil : {busca: busca}
68
+ res = create_request( :get, "/linhas", params)
69
+ JSON.parse(res.body)
70
+ end
71
+
72
+ def veiculos()
73
+ res = create_request( :get, "/veiculos")
74
+ JSON.parse(res.body)
75
+ end
76
+
77
+ def veiculos_linha(num_linha)
78
+ res = create_request( :get, "/veiculosLinha", {busca: num_linha})
79
+ JSON.parse(res.body)
80
+ end
81
+
82
+ def paradas (busca = nil)
83
+ params = busca.nil? ? nil : {busca: busca}
84
+ res = create_request( :get, "/paradas", params)
85
+ JSON.parse(res.body)
86
+ end
87
+
88
+ def paradas_linha(num_linha)
89
+ res = create_request( :get, "/paradasLinha", {busca: num_linha})
90
+ JSON.parse(res.body)
91
+ end
92
+
93
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strans-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luan Pontes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple client for Api strans from Teresina.
14
+ email: luapontes2@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/strans-client.rb
20
+ homepage: https://github.com/tOOlmaker-equalsp/ruby-apiStransTHE
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: strans-client
44
+ test_files: []