Permalink
Browse files

Merge pull request #3 from senico/master

3scale_ws_api_for_nodejs v0.3.1
  • Loading branch information...
joahking committed Aug 3, 2012
2 parents 5bab0d8 + d5b13ad commit a11fb29f8049d90f1395e5697169095051254af5
Showing with 46 additions and 41 deletions.
  1. +0 −3 .npmignore
  2. +5 −4 package.json
  3. +41 −34 src/client.coffee
View
@@ -1,3 +0,0 @@
-.*
-src/
-test/
View
@@ -1,7 +1,7 @@
{
"name": "3scale",
"description": "Client for 3Scale Networks API",
- "version": "0.2.0",
+ "version": "0.3.1",
"homepage": "http://www.3scale.net",
"repository": {
"type": "git",
@@ -10,7 +10,8 @@
"author": "3scale <support@3scale.net>",
"contributors": [
"Carlos Villuendas <carlosvillu@3scale.net>",
- "Michal Cichra <michal@3scale.net>"
+ "Michal Cichra <michal@3scale.net>",
+ "Daniel Stevens <daniel.stevens@senicolabs.com>"
],
"main": "lib/index.js",
"bugs": {
@@ -30,14 +31,14 @@
"lib": "./lib"
},
"dependencies": {
- "libxmljs-easy": "*"
+ "libxmljs": "*"
},
"devDependencies": {
"coffee-script": "1.x",
"vows": "0.5.x"
},
"engines": {
- "node": "0.6.x"
+ "node": "0.8.x"
},
"optionalDependencies": {}
}
View
@@ -1,6 +1,6 @@
-http = require 'http'
+https = require 'https'
querystring = require 'querystring'
-easy = require('libxmljs-easy')
+libxml = require 'libxmljs'
Response = require './response'
AuthorizeResponse = require './authorize_response'
@@ -54,22 +54,25 @@ module.exports = class Client
query = querystring.stringify options
query += '&' + querystring.stringify {provider_key: @provider_key}
- threescale = http.createClient 80, @host
- request = threescale.request "GET", "#{url}#{query}", {host: @host}
- request.end()
- request.on 'response', (response) ->
+ req_opts =
+ host: @host
+ port: 443
+ path: url + query
+ method: 'GET'
+ request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
xml = ""
response.on 'data', (chunk) ->
xml += chunk
- response.on 'end', () ->
+ response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
else
throw "[Client::authorize] Server Error Code: #{response.statusCode}"
+ request.end()
@@ -108,12 +111,13 @@ module.exports = class Client
url = "/transactions.xml"
query = querystring.stringify {transactions: trans, provider_key: @provider_key}
- threescale = http.createClient 80, @host
- request = threescale.request "POST", "#{url}",
- {"host": @host, "Content-Type": "application/x-www-form-urlencoded", "Content-Length": query.length}
- request.write query
- request.end()
- request.on 'response', (response) ->
+ req_opts =
+ host: @host
+ port: 443
+ path: url
+ method: 'POST'
+ headers: {"host": @host, "Content-Type": "application/x-www-form-urlencoded", "Content-Length": query.length}
+ request = https.request req_opts, (response) ->
xml = ""
response.on "data", (data) ->
xml += data
@@ -125,41 +129,44 @@ module.exports = class Client
callback response
else if response.statusCode == 403
callback _self._build_error_response xml
+ request.write query
+ request.end()
# privates methods
_build_success_authorize_response: (xml) ->
response = new AuthorizeResponse()
- doc = easy.parse(xml)
- authorize = doc.authorized[0].$.text()
- plan = doc.plan[0].$.text()
-
+ doc = libxml.parseXml xml
+ authorize = doc.get('//authorized').text()
+ plan = doc.get('//plan').text()
+
if authorize is 'true'
response.success()
else
- reason = doc.reason[0]
+ reason = doc.get('//reason').text()
response.error(reason)
- usage_reports = doc.usage_reports[0].usage_report
-
- for index, usage_report of usage_reports
- do (usage_report) ->
- report =
- period: usage_report.$period
- metric: usage_report.$metric
- period_start: usage_report.period_start[0].$.text()
- period_end: usage_report.period_end[0].$.text()
- current_value: usage_report.current_value[0].$.text()
- max_value: usage_report.max_value[0].$.text()
- response.add_usage_reports report
-
+ usage_reports = doc.get '//usage_reports'
+
+ if usage_reports
+ for index, usage_report of usage_reports.childNodes()
+ do (usage_report) ->
+ report =
+ period: usage_report.attr('period').value()
+ metric: usage_report.attr('metric').value()
+ period_start: usage_report.get('period_start').text()
+ period_end: usage_report.get('period_end').text()
+ current_value: usage_report.get('current_value').text()
+ max_value: usage_report.get('max_value').text()
+ response.add_usage_reports report
+
response
_build_error_response: (xml) ->
response = new AuthorizeResponse()
- doc = easy.parse xml
- error = doc[0]
+ doc = libxml.parseXml xml
+ error = doc.get '/error'
response = new Response()
- response.error error.$.text(), error.$code
+ response.error error.text(), error.attr('code').value()
response

0 comments on commit a11fb29

Please sign in to comment.