Class: Vingd
- Inherits:
-
Object
- Object
- Vingd
- Defined in:
- lib/vingd.rb
Constant Summary collapse
- VERSION =
"0.1.5"
- URL_ENDPOINT =
production urls
"https://api.vingd.com/broker/v1"
- URL_FRONTEND =
"https://www.vingd.com"
- URL_ENDPOINT_SANDBOX =
sandbox urls
"https://api.vingd.com/sandbox/broker/v1"
- URL_FRONTEND_SANDBOX =
"https://www.sandbox.vingd.com"
Instance Method Summary collapse
-
#authorized_create_user(identities, primary, permissions = nil) ⇒ Object
authorized_create_user( => “12312312”, “mail” => “[email protected]”, “facebook”, [“get.balance”, “delegated.purchase”] ) Returns Vingd user’s ‘huid` (hashed user id).
-
#authorized_get_account_balance(huid) ⇒ Object
Returns account balance (in vingds) for user defined with ‘huid`.
-
#authorized_purchase_object(oid, price, huid) ⇒ Object
Does delegated (pre-authorized) purchase of ‘oid` in the name of `huid`, at price `price` (vingd transferred from `huid` to consumer’s acc).
-
#create_object(name, url) ⇒ Object
Registers (enrolls) an object into the Vingd Objects Registry.
- #create_order(oid, price, context = nil, expires = nil) ⇒ Object
- #get_object(oid) ⇒ Object
- #get_objects(oid = nil, date_since = nil, date_until = nil, last = nil, first = nil) ⇒ Object
-
#initialize(api_key, api_secret, api_endpoint, usr_frontend) ⇒ Vingd
constructor
Vingd Client init.
- #parse_response(r, name) ⇒ Object
- #request(verb, subpath, data = nil) ⇒ Object
-
#reward_user(huid_to, amount, description = nil) ⇒ Object
User ‘huid` gets `amount` vingds (transferred from consumer’s acc), or on failure, an exception gets thrown.
Constructor Details
#initialize(api_key, api_secret, api_endpoint, usr_frontend) ⇒ Vingd
Vingd Client init. Requires API ‘key`, `secret`. API `endpoint` URL and user `frontend` can be set when testing on sandbox.
23 24 25 26 27 28 29 30 |
# File 'lib/vingd.rb', line 23 def initialize(api_key, api_secret, api_endpoint, usr_frontend) @api_key = api_key @api_secret = api_secret @api_endpoint = api_endpoint @usr_frontend = usr_frontend # ruby 1.9.2 has Base64.strict_encode6 which removes newlines @credentials = Base64.encode64(api_key + ':' + @api_secret).gsub(/\n/, '') end |
Instance Method Details
#authorized_create_user(identities, primary, permissions = nil) ⇒ Object
authorized_create_user(
{"facebook" => "12312312", "mail" => "[email protected]"},
"facebook",
["get.balance", "delegated.purchase"]
) Returns Vingd user’s ‘huid` (hashed user id). Throws exception on failure.
169 170 171 172 173 174 175 176 |
# File 'lib/vingd.rb', line 169 def (identities, primary, = nil) data = JSON.dump({ 'identities' => identities, 'primary' => primary, 'delegate_permissions' => , }) return self.request('post', 'id/users/', data) end |
#authorized_get_account_balance(huid) ⇒ Object
Returns account balance (in vingds) for user defined with ‘huid`.
157 158 159 160 |
# File 'lib/vingd.rb', line 157 def (huid) balance_cents = self.request('get', 'fort/accounts/' + huid)['balance'].to_i return balance_cents / 100.0 end |
#authorized_purchase_object(oid, price, huid) ⇒ Object
Does delegated (pre-authorized) purchase of ‘oid` in the name of `huid`, at price `price` (vingd transferred from `huid` to consumer’s acc). Throws exception on failure.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/vingd.rb', line 181 def (oid, price, huid) data = JSON.dump({ 'price' => (price * 100).round, 'huid' => huid, 'autocommit' => true, }) ret = self.request('post', "objects/#{oid}/purchases", data) ret['cost'] /= 100.0 return ret end |
#create_object(name, url) ⇒ Object
Registers (enrolls) an object into the Vingd Objects Registry. Requires object ‘name` and callback `url`. Returns Object ID (`oid`) as int.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vingd.rb', line 118 def create_object(name, url) data = JSON.dump({ 'description' => { "name" => name, "url" => url, } }) response = self.request('post', 'registry/objects', data) return self.parse_response(response, 'oid') end |
#create_order(oid, price, context = nil, expires = nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/vingd.rb', line 129 def create_order(oid, price, context=nil, expires=nil) expires.is_a?(DateTime) or expires = DateTime.now.new_offset(0) + 30 data = JSON.dump({ 'price' => (price * 100).round, 'order_expires' => expires.iso8601, 'context'=> context, }) response = self.request('post', "objects/#{oid}/orders", data) orderid = self.parse_response(response, 'id') return orderid end |
#get_object(oid) ⇒ Object
111 112 113 |
# File 'lib/vingd.rb', line 111 def get_object(oid) return self.get_objects(oid) end |
#get_objects(oid = nil, date_since = nil, date_until = nil, last = nil, first = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/vingd.rb', line 101 def get_objects(oid=nil, date_since=nil, date_until=nil, last=nil, first=nil) resource = "registry/objects" oid.nil? or resource+= "/#{oid}" date_since.nil? or resource+= "/since=#{date_since}" date_until.nil? or resource+= "/until=#{date_until}" first.nil? or resource+= "/first=#{first}" last.nil? or resource+= "/last=#{last}" return self.request('get', resource) end |
#parse_response(r, name) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/vingd.rb', line 87 def parse_response(r, name) names = name + 's' data = r[names] if data.is_a?(Array) errors = r['errors'] if errors.any? raise VingdError, errors[0]['desc'] end return r[names][0] else return r[name] end end |
#request(verb, subpath, data = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vingd.rb', line 32 def request(verb, subpath, data=nil) uri = URI.parse(@api_endpoint + '/' + subpath) http = Net::HTTP.new(uri.host, 443) http.use_ssl = true method = nil case verb.downcase when 'get' method = Net::HTTP::Get when 'post' method = Net::HTTP::Post else raise VingdError, 'Invalid request method' end request = method.new(uri.request_uri) request['user-agent'] = "vingd-api-ruby/#{Vingd::VERSION}" request['authorization'] = 'Basic ' + @credentials begin response = http.request(request, data) rescue raise InternalError, 'HTTP request failed! (Network error? Installation error?)' end begin code = response.code.to_i content = JSON.parse(response.body) rescue raise VingdError, 'Non-JSON server response' end if (200..299).include?(code) begin return content['data'] rescue raise InvalidData, 'Invalid server DATA response format!' end end begin = content['message'] context = content['context'] rescue raise InvalidData, 'Invalid server ERROR response format!' end case code when Codes::BAD_REQUEST raise InvalidData.new(context), when Codes::FORBIDDEN raise Forbidden.new(context), when Codes::NOT_FOUND raise NotFound.new(context), when Codes::INTERNAL_SERVER_ERROR raise InternalError.new(context), when Codes::CONFLICT raise VingdError.new(context), end raise VingdError.new(context, code), end |
#reward_user(huid_to, amount, description = nil) ⇒ Object
User ‘huid` gets `amount` vingds (transferred from consumer’s acc), or on failure, an exception gets thrown.
143 144 145 146 147 148 149 150 |
# File 'lib/vingd.rb', line 143 def reward_user(huid_to, amount, description = nil) data = JSON.dump({ 'huid_to' => huid_to, 'amount' => (amount * 100).round, 'description' => description, }) return self.request('post', 'rewards', data) end |