crossbar-http 0.1.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19a2298c3a79adeae874b6b4dcff452bbdf536a0
4
- data.tar.gz: 2dc0cf90b84e592ae2b570665c17e6dcfa96c8ee
3
+ metadata.gz: 5bb9f8a404140d6b70bd23f5d8b89f09313b2a19
4
+ data.tar.gz: 6b78516ba76055fa4dd962ba85fa0a74247e3b49
5
5
  SHA512:
6
- metadata.gz: ba1b8ddb833e5cf23c4f5dba920848d5795a54d6568d8ca92747339369f62ac146870dc9e5a2418a652f07b5febb65e06f51326c32e39c2ba71769a706fbc389
7
- data.tar.gz: 8a0a5e9d2bae3b7958c8479600a2a12d91eb797b88eeed8acdc9788a7614fd9f505af947c33bbd2b0315735f564d9e4bece81ccd75cc46c04a9a18d43254b3a0
6
+ metadata.gz: ea38d47f7104caaf5008a680a9026c12cd46b4d355094badbf4a2a8790b345beb92516f856e2f069c9af2b249e88ba223670f90490b36b0541ef026eb5b8e9f6
7
+ data.tar.gz: b3b137f07ba6f11a94fb81fed455540e220195d0628b2233280ae77cfdbe4f12134621ce6e5a4d82186c67fbf7f4db6f30e84ecd47e260c6fdef291defe0ea2f
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  .idea/
11
11
  /dist/
12
+ .DS_Store
data/README.md CHANGED
@@ -1,16 +1,20 @@
1
1
  # Crossbar HTTP
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/crossbar-http.svg)](https://badge.fury.io/rb/crossbar-http)
4
+ [![Circle CI](https://circleci.com/gh/ericchapman/ruby-crossbar-http/tree/master.svg?&style=shield&circle-token=d1d127456bc9210c10f34db5b6fd573fb228bed7)](https://circleci.com/gh/ericchapman/ruby-crossbar-http/tree/master)
5
+ [![Codecov](https://img.shields.io/codecov/c/github/ericchapman/ruby-crossbar-http/master.svg)](https://codecov.io/github/ericchapman/ruby-crossbar-http)
4
6
 
5
7
  Module that provides methods for accessing Crossbar.io HTTP Bridge Services
6
8
 
7
9
  ## Revision History
8
10
 
9
- - v0.1.0:
10
- - Initial version
11
+ - v0.1.2:
12
+ - Added the ability to override the date serialization
11
13
  - v0.1.1:
12
14
  - Fixed typos in README
13
- - Added support for https (I didn't know the library I was using didn't)
15
+ - Added support for https
16
+ - v0.1.0:
17
+ - Initial version
14
18
 
15
19
  ## Installation
16
20
 
@@ -60,18 +64,37 @@ def onJoin(self, details):
60
64
  ```
61
65
 
62
66
  ### Key/Secret
63
- For bridge services that have a key and secret defined, simply include the key and secret in the instantiation of the
64
- client.
67
+ For bridge services that have a key and secret defined, simply include the key and secret
68
+ in the instantiation of the client.
65
69
 
66
70
  ``` ruby
67
71
  client = Crossbar::HTTP::Client.new("http://127.0.0.1/publish", key: "key", secret: "secret")
68
72
  ```
69
73
 
74
+ ### Pre-Serialize Callback
75
+ The library provides a "pre-serialize" callback which will allow the user to
76
+ modify certain object types before the payload is serialized. This can be used
77
+ for things like changing the date/time to a custom value. See an example of
78
+ doing this below
79
+
80
+ ``` ruby
81
+ pre_serialize = lambda { |value|
82
+ if value.is_a? Date
83
+ return value.strftime("%Y/%m/%d %H:%M:%S.%L %Z")
84
+ end
85
+ }
86
+ client = Crossbar::HTTP::Client.new("http://127.0.0.1/publish", pre_serialize: pre_serialize)
87
+ ```
88
+
70
89
  ## Contributing
71
90
  To contribute, fork the repo and submit a pull request.
72
91
 
73
92
  ## Testing
74
- TODO
93
+ To run the unit tests, execute the following
94
+
95
+ ```
96
+ %> rake spec
97
+ ```
75
98
 
76
- ##License
99
+ ## License
77
100
  MIT
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'bundler', '~> 1.13'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'simplecov', '~> 0.12.0'
28
+ spec.add_development_dependency 'codecov'
27
29
  end
@@ -33,14 +33,15 @@ require 'json'
33
33
  module Crossbar
34
34
  module HTTP
35
35
  class Client
36
- attr_accessor :url, :key, :secret, :verbose, :sequence
36
+ attr_accessor :url, :key, :secret, :verbose, :sequence, :pre_serialize
37
37
 
38
38
  # Initializes the client
39
39
  # @param url [String] - The url of the router's HTTP bridge
40
40
  # @param key [String] - The key (optional)
41
41
  # @param secret [String] - The secret (optional)
42
42
  # @param verbose [Bool] - 'True' if you want debug messages printed
43
- def initialize(url, key: nil, secret: nil, verbose: false)
43
+ # @param pre_serialize [lambda] - Lambda to format the data
44
+ def initialize(url, key: nil, secret: nil, verbose: false, pre_serialize: nil)
44
45
 
45
46
  raise 'The url can not be nil' unless url != nil
46
47
 
@@ -48,6 +49,7 @@ module Crossbar
48
49
  self.key = key
49
50
  self.secret = secret
50
51
  self.verbose = verbose
52
+ self.pre_serialize = pre_serialize
51
53
  self.sequence = 1
52
54
  end
53
55
 
@@ -83,15 +85,34 @@ module Crossbar
83
85
  self._make_api_call(params)
84
86
  end
85
87
 
88
+ # Parses the params to pre-serialize the overrides
89
+ def _parse_params(params)
90
+
91
+ if params.is_a? Hash
92
+ return_value = {}
93
+ params.each do |key, value|
94
+ return_value[key] = self._parse_params(value)
95
+ end
96
+ elsif params.is_a? Array
97
+ return_value = []
98
+ params.each_index {|i|
99
+ return_value.push(self._parse_params (params[i]))
100
+ }
101
+ else
102
+ return_value = self.pre_serialize.call(params) || params
103
+ end
104
+
105
+ return_value
106
+ end
107
+
86
108
  # Computes the signature.
87
109
  # Described at: http://crossbar.io/docs/HTTP-Bridge-Services-Caller/
88
110
  # Reference code is at: https://github.com/crossbario/crossbar/blob/master/crossbar/adapter/rest/common.py
89
111
  # @param body [Hash]
90
112
  # @return (signature, nonce, timestamp)
91
- def _compute_signature(body, nonce: nil, timestamp: nil)
92
-
93
- timestamp ||= Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
94
- nonce ||= rand(0..2**53)
113
+ def _compute_signature(body)
114
+ timestamp = self.class._get_timestamp
115
+ nonce = self.class._get_nonce
95
116
 
96
117
  # Compute signature: HMAC[SHA256]_{secret} (key | timestamp | seq | nonce | body) => signature
97
118
  hm = OpenSSL::HMAC.new(self.secret, OpenSSL::Digest::SHA256.new)
@@ -105,11 +126,28 @@ module Crossbar
105
126
  return signature, nonce, timestamp
106
127
  end
107
128
 
129
+ def self._get_timestamp
130
+ Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
131
+ end
132
+
133
+ def self._get_nonce
134
+ rand(0..2**53)
135
+ end
136
+
137
+ # Performs the API call
138
+ # @param json_params [Hash,nil] The parameters that will make up the body of the request
139
+ # @return [Hash] The response from the server
108
140
  def _make_api_call(json_params=nil)
109
141
 
110
142
  puts "Crossbar::HTTP - Request: POST #{url}" if self.verbose
111
143
 
112
- encoded_params = (json_params != nil) ? JSON.generate(json_params) : nil
144
+ encoded_params = nil
145
+ if json_params != nil
146
+ if self.pre_serialize&.is_a? Proc
147
+ json_params = self._parse_params json_params
148
+ end
149
+ encoded_params = JSON.generate(json_params)
150
+ end
113
151
 
114
152
  puts "Crossbar::HTTP - Params: #{encoded_params}" if encoded_params != nil and self.verbose
115
153
 
@@ -132,11 +170,20 @@ module Crossbar
132
170
  # TODO: Not sure what this is supposed to be but this works
133
171
  self.sequence += 1
134
172
 
173
+ self._api_call uri, encoded_params
174
+ end
175
+
176
+ # This method makes tha API call. It is a class method so it can be more easily stubbed
177
+ # for testing
178
+ # @param uri [URI] The uri of the request
179
+ # @param body [String] The body of the request
180
+ # @return [Hash] The response from the server
181
+ def _api_call(uri, body=nil)
135
182
  # Create the request
136
183
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
137
184
  req = Net::HTTP::Post.new(uri)
138
185
  req['Content-Type'] = 'application/json'
139
- req.body = encoded_params
186
+ req.body = body
140
187
 
141
188
  http.request(req)
142
189
  end
@@ -148,7 +195,6 @@ module Crossbar
148
195
  else
149
196
  raise "Crossbar::HTTP - Code: #{res.code}, Error: #{res.message}"
150
197
  end
151
-
152
198
  end
153
199
  end
154
200
  end
@@ -1,5 +1,5 @@
1
1
  module Crossbar
2
2
  module HTTP
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crossbar-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-12 00:00:00.000000000 Z
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.12.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: codecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: ''
56
84
  email:
57
85
  - eric.chappy@gmail.com