fretala 1.0.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.
Files changed (2) hide show
  1. data/lib/fretala.rb +105 -0
  2. metadata +46 -0
@@ -0,0 +1,105 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'base64'
4
+
5
+ class Fretala
6
+ FRETALA_SANDBOX_URL = 'sandbox.freta.la'
7
+ FRETALA_PRODUCTION_URL = 'api.freta.la'
8
+
9
+ def initialize(environment, settings)
10
+ throw 'environment must be sandbox or production' if not ['production', 'sandbox'].include? environment
11
+ @environment = environment
12
+ if @environment == 'production'
13
+ @url = FRETALA_PRODUCTION_URL
14
+ else
15
+ @url = FRETALA_SANDBOX_URL
16
+ end
17
+
18
+ @clientId = settings['clientId']
19
+ @clientSecret = settings['clientSecret']
20
+ @username = settings['username']
21
+ @password = settings['password']
22
+ @token = ''
23
+ end
24
+
25
+ def authenticate()
26
+ data = {
27
+ 'grant_type' => 'password',
28
+ 'username' => @username,
29
+ 'password' => @password
30
+ };
31
+ res = performRequest('POST', '/authenticate', data.to_json, true)
32
+ @token = res['access_token']
33
+ return @token
34
+ end
35
+
36
+ def getCards()
37
+ authenticate()
38
+ performRequest('GET', '/cards')
39
+ end
40
+
41
+ def insertCard(card)
42
+ authenticate()
43
+ performRequest('POST', '/cards', card.to_json)
44
+ end
45
+
46
+ def deleteCard(cardToken)
47
+ authenticate()
48
+ performRequest('DELETE', '/cards/'+cardToken)
49
+ end
50
+
51
+ def insertFrete(frete)
52
+ authenticate()
53
+ performRequest('POST', '/fretes', frete.to_json)
54
+ end
55
+
56
+ def cost(route)
57
+ performRequest('POST', '/fretes/cost', route.to_json)
58
+ end
59
+
60
+ def buildHeaders(auth=false)
61
+ headers = {
62
+ 'Content-Type' => 'application/json'
63
+ }
64
+ if(auth)
65
+ headers['Authorization'] = 'Basic ' + Base64.encode64(@clientId + ':' + @clientSecret)
66
+ elsif(@token != '')
67
+ headers['Authorization'] = 'Bearer ' + @token
68
+ end
69
+ return headers;
70
+ end
71
+
72
+ def performRequest(type, path, data='', auth=false)
73
+ http = Net::HTTP.new(@url, 443)
74
+ http.use_ssl = true
75
+ headers = buildHeaders(auth)
76
+ if type == 'POST'
77
+ request = Net::HTTP::Post.new(path, headers)
78
+ request.body = data
79
+ elsif type == 'GET'
80
+ request = Net::HTTP::Get.new(path, headers)
81
+ elsif type == 'PUT'
82
+ request = Net::HTTP::Put.new(path, headers)
83
+ request.body = data
84
+ elsif type == 'DELETE'
85
+ request = Net::HTTP::Delete.new(path, headers)
86
+ else
87
+ throw 'Request type ' + type + ' is not valid'
88
+ end
89
+
90
+ response = http.request(request)
91
+ json = JSON.parse(response.body)
92
+ if response.code != '200' && response.code != '204'
93
+ if(auth)
94
+ throw json['error_description']
95
+ else
96
+ throw json['message']
97
+ end
98
+ end
99
+ @token = ''
100
+ return json
101
+ end
102
+
103
+ private :buildHeaders, :performRequest
104
+
105
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fretala
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Lobosque
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A wrapper for Freta lá API
15
+ email: lucas@freta.la
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/fretala.rb
21
+ homepage: http://freta.la
22
+ licenses:
23
+ - MIT
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: A wrapper for Freta lá API
46
+ test_files: []