vingd 0.1.2

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.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2013 Vingd, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,176 @@
1
+ require 'net/http'
2
+ require "base64"
3
+ require "uri"
4
+ require 'json'
5
+ require 'date'
6
+
7
+ require 'vingd/response'
8
+ require 'vingd/exceptions'
9
+
10
+ class Vingd
11
+ VERSION = "0.1.2"
12
+
13
+ # production urls
14
+ URL_ENDPOINT = "https://api.vingd.com/broker/v1"
15
+ URL_FRONTEND = "https://www.vingd.com"
16
+ # sandbox urls
17
+ URL_ENDPOINT_SANDBOX = "https://api.vingd.com/sandbox/broker/v1"
18
+ URL_FRONTEND_SANDBOX = "https://www.sandbox.vingd.com"
19
+
20
+ # Vingd Client init.
21
+ # Requires API `key`, `secret`.
22
+ # API `endpoint` URL and user `frontend` can be set when testing on sandbox.
23
+ def initialize(api_key, api_secret, api_endpoint, usr_frontend)
24
+ @api_key = api_key
25
+ @api_secret = api_secret
26
+ @api_endpoint = api_endpoint
27
+ @usr_frontend = usr_frontend
28
+ @credentials = Base64.strict_encode64(api_key + ':' + @api_secret)
29
+ end
30
+
31
+ def request(verb, subpath, data=nil)
32
+ uri = URI.parse(@api_endpoint + '/' + subpath)
33
+ http = Net::HTTP.new(uri.host, 443)
34
+ http.use_ssl = true
35
+ method = nil
36
+ case verb.downcase
37
+ when 'get'
38
+ method = Net::HTTP::Get
39
+ when 'post'
40
+ method = Net::HTTP::Post
41
+ else
42
+ raise VingdError, 'Invalid request method'
43
+ end
44
+ request = method.new(uri.request_uri)
45
+ request['user-agent'] = "vingd-api-ruby/#{Vingd::VERSION}"
46
+ request['authorization'] = 'Basic ' + @credentials
47
+ begin
48
+ response = http.request(request, data)
49
+ rescue
50
+ raise InternalError, 'HTTP request failed! (Network error? Installation error?)'
51
+ end
52
+ begin
53
+ code = response.code.to_i
54
+ content = JSON.parse(response.body)
55
+ rescue
56
+ raise VingdError, 'Non-JSON server response'
57
+ end
58
+ if (200..299).include?(code)
59
+ begin
60
+ return content['data']
61
+ rescue
62
+ raise InvalidData, 'Invalid server DATA response format!'
63
+ end
64
+ end
65
+ begin
66
+ message = content['message']
67
+ context = content['context']
68
+ rescue
69
+ raise InvalidData, 'Invalid server ERROR response format!'
70
+ end
71
+ case code
72
+ when Codes::BAD_REQUEST
73
+ raise InvalidData.new(context), message
74
+ when Codes::FORBIDDEN
75
+ raise Forbidden.new(context), message
76
+ when Codes::NOT_FOUND
77
+ raise NotFound.new(context), message
78
+ when Codes::INTERNAL_SERVER_ERROR
79
+ raise InternalError.new(context), message
80
+ when Codes::CONFLICT
81
+ raise VingdError.new(context), message
82
+ end
83
+ raise VingdError.new(context, code), message
84
+ end
85
+
86
+ def parse_response(r, name)
87
+ names = name + 's'
88
+ data = r[names]
89
+ if data.is_a?(Array)
90
+ errors = r['errors']
91
+ if errors.any?
92
+ raise VingdError, errors[0]['desc']
93
+ end
94
+ return r[names][0]
95
+ else
96
+ return r[name]
97
+ end
98
+ end
99
+
100
+ def get_objects(oid=nil, date_since=nil, date_until=nil, last=nil, first=nil)
101
+ resource = "registry/objects"
102
+ oid.nil? or resource+= "/#{oid}"
103
+ date_since.nil? or resource+= "/since=#{date_since}"
104
+ date_until.nil? or resource+= "/until=#{date_until}"
105
+ first.nil? or resource+= "/first=#{first}"
106
+ last.nil? or resource+= "/last=#{last}"
107
+ return self.request('get', resource)
108
+ end
109
+
110
+ def get_object(oid)
111
+ return self.get_objects(oid)
112
+ end
113
+
114
+ # Registers (enrolls) an object into the Vingd Objects Registry.
115
+ # Requires object `name` and callback `url`.
116
+ # Returns Object ID (`oid`) as int.
117
+ def create_object(name, url)
118
+ data = JSON.dump({
119
+ 'description' => {
120
+ "name" => name,
121
+ "url" => url,
122
+ }
123
+ })
124
+ response = self.request('post', 'registry/objects', data)
125
+ return self.parse_response(response, 'oid')
126
+ end
127
+
128
+ def create_order(oid, price, context=nil, expires=nil)
129
+ expires.is_a?(DateTime) or expires = DateTime.now.new_offset(0) + 30
130
+ data = JSON.dump({
131
+ 'price' => price,
132
+ 'order_expires' => expires.iso8601,
133
+ 'context'=> context,
134
+ })
135
+ response = self.request('post', "objects/#{oid}/orders", data)
136
+ orderid = self.parse_response(response, 'id')
137
+ return orderid
138
+ end
139
+
140
+ # User `huid` gets `amount` vingd cents (transferred from consumer's acc), or
141
+ # on failure, an exception gets thrown.
142
+ def reward_user(huid_to, amount, description = nil)
143
+ data = JSON.dump({
144
+ 'huid_to' => huid_to,
145
+ 'amount' => amount,
146
+ 'description' => description,
147
+ })
148
+ return self.request('post', 'rewards', data)
149
+ end
150
+
151
+ # Does delegated (pre-authorized) purchase of `oid` in the name of `huid`, at
152
+ # price `price` (vingd transferred from `huid` to consumer's acc).
153
+ # Throws exception on failure.
154
+ def authorized_purchase_object(oid, price, huid)
155
+ # autocommit = true, by default
156
+ end
157
+
158
+ def authorized_purchase_order(orderid, huid)
159
+ end
160
+
161
+ # authorized_create_user(
162
+ # {"facebook" => "12312312", "mail" => "user@example.com"},
163
+ # "facebook",
164
+ # ["get.balance", "delegated.purchase"]
165
+ # )
166
+ # Returns Vingd user's `huid` (hashed user id).
167
+ # Throws exception on failure.
168
+ def authorized_create_user(identities, primary, delegate_permissions = nil)
169
+ end
170
+
171
+ # Returns account balance (in vingd cents) for user defined with `huid`.
172
+ def authorized_get_balance(huid)
173
+ end
174
+ end
175
+
176
+
@@ -0,0 +1,31 @@
1
+ require 'vingd/response'
2
+
3
+ class VingdError < StandardError
4
+ attr_reader :context, :code
5
+ DEFAULT_CONTEXT = "Error"
6
+ DEFAULT_CODE = Codes::CONFLICT
7
+ def initialize(context=nil, code=nil)
8
+ @context = (context or self.class::DEFAULT_CONTEXT)
9
+ @code = (code or self.class::DEFAULT_CODE)
10
+ end
11
+ end
12
+
13
+ class InvalidData < VingdError
14
+ DEFAULT_CONTEXT = "Invalid data"
15
+ DEFAULT_CODE = Codes::BAD_REQUEST
16
+ end
17
+
18
+ class Forbidden < VingdError
19
+ DEFAULT_CONTEXT = "Forbidden"
20
+ DEFAULT_CODE = Codes::FORBIDDEN
21
+ end
22
+
23
+ class NotFound < VingdError
24
+ DEFAULT_CONTEXT = "Not Found"
25
+ DEFAULT_CODE = Codes::NOT_FOUND
26
+ end
27
+
28
+ class InternalError < VingdError
29
+ DEFAULT_CONTEXT = "Internal error"
30
+ DEFAULT_CODE = Codes::INTERNAL_SERVER_ERROR
31
+ end
@@ -0,0 +1,19 @@
1
+ class Codes
2
+ #Standard HTTP response codes used inside Vingd ecosystem.
3
+ # success
4
+ OK = 200
5
+ CREATED = 201
6
+ NO_CONTENT = 204
7
+ PARTIAL_CONTENT = 206
8
+ # error
9
+ MULTIPLE_CHOICES = 300
10
+ MOVED_PERMANENTLY = 301
11
+ BAD_REQUEST = 400
12
+ PAYMENT_REQUIRED = 402
13
+ FORBIDDEN = 403
14
+ NOT_FOUND = 404
15
+ CONFLICT = 409
16
+ GONE = 410
17
+ INTERNAL_SERVER_ERROR = 500
18
+ NOT_IMPLEMENTED = 501
19
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vingd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vingd, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Vingd enables users to pay with money or with time. See https://www.vingd.com
15
+ for more.
16
+ email: developers@vingd.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - lib/vingd.rb
23
+ - lib/vingd/response.rb
24
+ - lib/vingd/exceptions.rb
25
+ homepage: https://git.vingd.com/vingd/vingd-api-ruby
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.23
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Vingd API interface client.
50
+ test_files: []