ripcord 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da9d67bc7e6adaaf8718d86b62757b4605a24595
4
+ data.tar.gz: e21fa5867054573200bb32b372d4998e3d4d0020
5
+ SHA512:
6
+ metadata.gz: 40cee178d2a9e784f9e95c37059e19153224d2bf6a4ae499e18d488321b3ed6ed39143217c59dd4e5a80187ebd1bd5899a7bca0ba5b1f89ff542e319c00beba5
7
+ data.tar.gz: 9220717aff0e7b43f7a637ec657268929ceab2f43b3c43f9cf7f60af9fb108f7c6dd83bdd94a8b1be04d626d0c9b15c542bf579ce6d45ba494c63cb0ea7b4743
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.3.1
6
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ripcord.gemspec
4
+ gemspec
@@ -0,0 +1,6 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ watch('spec/spec_helper.rb') { "spec" }
3
+ watch(%r{^spec/.+_spec\.rb})
4
+
5
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Klaus Zanders
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # RiPCord [![Build Status](https://travis-ci.org/klaustopher/ripcord.svg?branch=master)](https://travis-ci.org/klaustopher/ripcord)
2
+
3
+ This is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) implementation. It is heavily based on [JSONRPC::Client](https://github.com/fxposter/jsonrpc-client) but has a lot of changes
4
+ from the initial implementation that are sort of specific to our use case. But it might work for you as well.
5
+
6
+ The name *R*i*PC*ord originates from me searching for a word that contained RPC :)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ripcord'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+
23
+ ### Basic Usage
24
+ ```ruby
25
+ client = Ripcord::Client.new('http://www.some-server.com/rpc-endpoint')
26
+
27
+ client.call('add', [1,2])
28
+ ```
29
+
30
+ ### ... with Authentication
31
+
32
+ ```ruby
33
+ # HTTP Basic Auth (https://tools.ietf.org/html/rfc2617)
34
+ client.authentication = Ripcord::Authentication::HTTPBasicAuth.new('user', 'password')
35
+ ```
36
+
37
+ ```ruby
38
+ # HTTP Token Auth (https://tools.ietf.org/html/draft-hammer-http-token-auth-01)
39
+ client.authentication = Ripcord::Authentication::HTTPBasicAuth.new('some-token')
40
+ ```
41
+
42
+ ```ruby
43
+ # Inline Token
44
+ # This adds a token item to the root of the JSON request.
45
+ client.authentication = Ripcord::Authentication::InlineToken.new('some-token')
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/klaustopher/ripcord.
57
+
58
+ ## Special thanks
59
+
60
+ - [Pavel Forkert](https://github.com/fxposter/) for the initial implementation of `JSONRPC::Client`
61
+ - [Clark Germany](https://www.clark.de) for paying me to work on this ;)
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ripcord"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'ripcord/version'
2
+ require 'ripcord/client'
3
+
4
+ module Ripcord
5
+ JSON_RPC_VERSION = '2.0'.freeze
6
+ end
@@ -0,0 +1,7 @@
1
+ module Ripcord
2
+ module Authentication
3
+ require 'ripcord/authentication/http_basic_auth'
4
+ require 'ripcord/authentication/http_token_auth'
5
+ require 'ripcord/authentication/inline_token'
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Ripcord::Authentication
2
+ class HTTPBasicAuth
3
+ def initialize(username, password)
4
+ @username, @password = username, password
5
+ end
6
+
7
+ def apply_to(request, payload_hash)
8
+ request.basic_auth(@username, @password)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Ripcord::Authentication
2
+ class HTTPTokenAuth
3
+ def initialize(token)
4
+ @token = token
5
+ end
6
+
7
+ def apply_to(request, payload_hash)
8
+ request.add_field 'Authorization', "Token token=#{@token}"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Ripcord::Authentication
2
+ class InlineToken
3
+ def initialize(token)
4
+ @token = token
5
+ end
6
+
7
+ def apply_to(request, payload_hash)
8
+ payload_hash[:token] = @token
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ require 'securerandom'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module Ripcord
7
+ class BaseClient
8
+ def generate_request_id
9
+ SecureRandom.hex(16)
10
+ end
11
+
12
+ attr_accessor :authentication
13
+
14
+ def initialize(endpoint_url)
15
+ @endpoint_url = URI.parse(endpoint_url)
16
+ @http_client = Net::HTTP.new(@endpoint_url.host, @endpoint_url.port)
17
+ end
18
+
19
+ protected
20
+ def execute_request(json_rpc_request)
21
+ request = Net::HTTP::Post.new(@endpoint_url.request_uri)
22
+ request.content_type = 'application/json'
23
+
24
+ payload_hash = json_rpc_request.to_payload
25
+
26
+ if authentication
27
+ authentication.apply_to(request, payload_hash)
28
+ end
29
+
30
+ request.body = JSON.generate(payload_hash)
31
+
32
+ @http_client.request(request)
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,92 @@
1
+ require 'ripcord/authentication'
2
+ require 'ripcord/json_rpc'
3
+
4
+ require 'securerandom'
5
+ require 'uri'
6
+ require 'net/http'
7
+ require 'json'
8
+ require 'logger'
9
+
10
+ module Ripcord
11
+ class Client
12
+ attr_accessor :authentication
13
+ attr_reader :logger, :last_request, :last_response
14
+
15
+ def initialize(endpoint_url)
16
+ @endpoint_url = URI.parse(endpoint_url)
17
+
18
+ set_basic_auth_from_url if @endpoint_url.user && @endpoint_url.password
19
+
20
+ @http_client = Net::HTTP.new(@endpoint_url.host, @endpoint_url.port)
21
+
22
+ if @endpoint_url.kind_of?(URI::HTTPS)
23
+ @http_client.use_ssl=true
24
+ #@http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE
25
+ end
26
+ end
27
+
28
+ def call(method, params)
29
+ request = Ripcord::JsonRPC::Request.new(method, params, generate_request_id)
30
+ @last_request = request
31
+ http_response = execute_request(request)
32
+
33
+ parse_response(http_response)
34
+ end
35
+
36
+ def logger=(logger)
37
+ @logger = logger
38
+ @http_client.set_debug_output(logger)
39
+ end
40
+
41
+ def inspect
42
+ "#<Ripcord::Client endpoint=#{@endpoint_url.to_s}>"
43
+ end
44
+
45
+ private
46
+ def execute_request(json_rpc_request)
47
+ request = Net::HTTP::Post.new(@endpoint_url.request_uri)
48
+ request.content_type = 'application/json'
49
+
50
+ payload_hash = json_rpc_request.to_payload
51
+
52
+ if authentication
53
+ authentication.apply_to(request, payload_hash)
54
+ end
55
+
56
+ request.body = JSON.generate(payload_hash)
57
+
58
+ @http_client.request(request)
59
+ end
60
+
61
+ def parse_response(http_response)
62
+ # Check status code
63
+ status_code = http_response.code.to_i
64
+ if status_code < 200 || status_code > 299
65
+ raise Ripcord::Error::InvalidResponse.new(http_response.body)
66
+ end
67
+
68
+ # try to parse json
69
+ begin
70
+ json_data = JSON.parse(http_response.body, symbolize_names: true)
71
+ rescue JSON::ParserError
72
+ raise Ripcord::Error::InvalidJSON.new(http_response.body)
73
+ end
74
+
75
+ if json_data.kind_of?(Hash) # Handle single response
76
+ Ripcord::JsonRPC::Response.from_data(json_data)
77
+ elsif json_data.kind_of?(Array) # Handle batch response
78
+ json_data.map do |request_json|
79
+ Ripcord::JsonRPC::Response.from_data(request_json)
80
+ end
81
+ end
82
+ end
83
+
84
+ def generate_request_id
85
+ SecureRandom.hex(16)
86
+ end
87
+
88
+ def set_basic_auth_from_url
89
+ self.authentication = Ripcord::Authentication::HTTPBasicAuth.new(@endpoint_url.user, @endpoint_url.password)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,18 @@
1
+ module Ripcord
2
+ module Error
3
+ class InvalidResponse < StandardError
4
+ def initialize(response_body = nil)
5
+ message = 'Invalid or empty response from server.'
6
+ message += "\nResponse: #{response_body}" if response_body
7
+
8
+ super(message)
9
+ end
10
+ end
11
+
12
+ class InvalidJSON < StandardError
13
+ def initialize(response_body)
14
+ super("Couldn't parse JSON string received from server\nResponse: #{response_body}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module Ripcord
2
+ module JsonRPC
3
+ require 'ripcord/json_rpc/error'
4
+ require 'ripcord/json_rpc/request'
5
+ require 'ripcord/json_rpc/response'
6
+ require 'ripcord/json_rpc/notification'
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Ripcord::JsonRPC
2
+ class Error
3
+ attr_reader :code, :message, :data
4
+
5
+ def initialize(code, message, data)
6
+ @code, @message, @data = code, message, data
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'ripcord/json_rpc/request'
2
+
3
+ module Ripcord::JsonRPC
4
+ class Notification < Request
5
+ def initialize(method, params)
6
+ @method, @params = method, params
7
+ @id = nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module Ripcord::JsonRPC
2
+ class Request
3
+ attr_accessor :method, :params
4
+ attr_reader :id
5
+
6
+ def initialize(method, params, id)
7
+ @method, @params, @id = method, params, id
8
+ end
9
+
10
+ def to_payload
11
+ {
12
+ jsonrpc: Ripcord::JSON_RPC_VERSION,
13
+ method: method,
14
+ }.tap do |payload_hash|
15
+ payload_hash[:params] = params if should_include_params?
16
+ payload_hash[:id] = id unless id.nil?
17
+ end
18
+ end
19
+
20
+ private
21
+ def should_include_params?
22
+ (params.kind_of?(Array) || params.kind_of?(Hash)) && !params.empty?
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ require 'ripcord/error'
2
+ require 'ripcord/json_rpc/error'
3
+
4
+ module Ripcord::JsonRPC
5
+ class Response
6
+ attr_reader :result, :error, :id
7
+
8
+ def initialize(result, error, id)
9
+ @result, @id = result, id
10
+
11
+ @error = Ripcord::JsonRPC::Error.new(error[:code], error[:message], error[:data]) if error
12
+ end
13
+
14
+ def successful?
15
+ error.nil? && !result.nil?
16
+ end
17
+
18
+ class<<self
19
+ def from_data(json_data)
20
+ raise Ripcord::Error::InvalidResponse.new(json_data) unless valid_data?(json_data)
21
+
22
+ new(json_data[:result], json_data[:error], json_data[:id])
23
+ end
24
+
25
+ def valid_data?(json_data)
26
+ return false if !json_data.kind_of?(Hash)
27
+ return false if json_data[:jsonrpc] != Ripcord::JSON_RPC_VERSION
28
+ return false if !json_data.has_key?(:id)
29
+ return false if !(json_data.has_key?(:error) ^ json_data.has_key?(:result))
30
+
31
+ if json_data.has_key?(:error)
32
+ return false if !json_data[:error].kind_of?(Hash)
33
+ return false if !json_data[:error].has_key?(:code)
34
+ return false if !json_data[:error][:code].kind_of?(Fixnum)
35
+ return false if !json_data[:error].has_key?(:message)
36
+ return false if !json_data[:error][:message].kind_of?(String)
37
+ end
38
+
39
+ true
40
+ rescue
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Ripcord
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ripcord/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ripcord"
8
+ spec.version = Ripcord::VERSION
9
+ spec.authors = ["Klaus Zanders", "Pavel Forkert"]
10
+ spec.email = ["coding@kgz.me", "fxposter@gmail.com"]
11
+
12
+ spec.summary = %q{This is a JSON-RPC 2.0 client implementation with some specific additions (custom auth schemes, etc)}
13
+ spec.description = %q{JSON-RPC 2.0 client implementation}
14
+ spec.homepage = "https://github.com/klaustopher/ripcord"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_development_dependency "webmock"
24
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripcord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Klaus Zanders
8
+ - Pavel Forkert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-06-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.12'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.12'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: JSON-RPC 2.0 client implementation
71
+ email:
72
+ - coding@kgz.me
73
+ - fxposter@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Guardfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/ripcord.rb
89
+ - lib/ripcord/authentication.rb
90
+ - lib/ripcord/authentication/http_basic_auth.rb
91
+ - lib/ripcord/authentication/http_token_auth.rb
92
+ - lib/ripcord/authentication/inline_token.rb
93
+ - lib/ripcord/base_client.rb
94
+ - lib/ripcord/client.rb
95
+ - lib/ripcord/error.rb
96
+ - lib/ripcord/json_rpc.rb
97
+ - lib/ripcord/json_rpc/error.rb
98
+ - lib/ripcord/json_rpc/notification.rb
99
+ - lib/ripcord/json_rpc/request.rb
100
+ - lib/ripcord/json_rpc/response.rb
101
+ - lib/ripcord/version.rb
102
+ - ripcord.gemspec
103
+ homepage: https://github.com/klaustopher/ripcord
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: This is a JSON-RPC 2.0 client implementation with some specific additions
127
+ (custom auth schemes, etc)
128
+ test_files: []