jsonrpc-faraday 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b709a056cafb2ebac6abc68d4748d171149dfe1b
4
+ data.tar.gz: 27ca8dff4437bd9c9a341a5166c701d4f548d2a2
5
+ SHA512:
6
+ metadata.gz: a5d0f731cb4ad2f09494d6f8dd72cd0353090b197b290a65745ed8d15c8450cfb8b696d12e66102311c322314eb79e6bdea53389761cebd1601a0c6e177ec755
7
+ data.tar.gz: f500efc3c1bd435f49dac62cb4bfaeda4104c76aa005ac0da1f1233d3553f3503d3c44ddad39d2aa5cbc89183981f06c4ac50261ee730f086cb7a92cf7f35320
data/.gitignore ADDED
@@ -0,0 +1,28 @@
1
+ /.bundle/
2
+
3
+ /pkg/
4
+ /tmp/
5
+
6
+ ## Gems
7
+ # Comment the below out if this is not a Gem:
8
+ /Gemfile.lock
9
+ *.gem
10
+ *.rbc
11
+
12
+ ## Test reports
13
+ /coverage/
14
+ /spec/reports/
15
+ /test/tmp/
16
+ /test/version_tmp/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ # for a library or gem, you might want to ignore these files since the code is
25
+ # intended to run in multiple environments; otherwise, check them in:
26
+ # Gemfile.lock
27
+ .ruby-version
28
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ # Most of these are disabling existing cops, primarily
2
+ # due to a smattering of different styles and loose
3
+ # guidlines for contributions.
4
+ #
5
+ # Any of these may be changed.
6
+
7
+ # I personally believe double-quotes is more-idiomatic Ruby
8
+ StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ # Enabled: false
11
+
12
+ # Anytime you're likely to need % string-prefix, string-interpolation is less-likely:
13
+ Style/PercentLiteralDelimiters:
14
+ PreferredDelimiters:
15
+ '%': '{}'
16
+ '%i': '{}'
17
+ '%q': '{}'
18
+ '%Q': '{}'
19
+ '%r': '{}'
20
+ '%s': '{}'
21
+ '%w': '{}'
22
+ '%W': '{}'
23
+ '%x': '{}'
24
+
25
+ SpaceInsideBlockBraces:
26
+ SpaceBeforeBlockParameters: false
27
+
28
+ LineLength:
29
+ Max: 120
30
+
31
+ Style/ClassAndModuleChildren:
32
+ EnforcedStyle: compact
33
+
34
+ Style/FileName:
35
+ Enabled: false
36
+
37
+ # TODO: Disable before release
38
+ Documentation:
39
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jsonrpc-faraday.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # JsonRpc
2
+
3
+ This is a simple JSON-RPC client, using Faraday & Typhoeus (by default).
4
+
5
+ The
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jsonrpc-faraday'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jsonrpc-faraday
22
+
23
+ ## Usage
24
+
25
+
26
+ ### Example
27
+
28
+ Create a new RPC connection, connect to https://rpc.domain.somewhere, with a call-back that will prefix the auth token
29
+ to the head of the arg-list for every RPC call to the remote server:
30
+
31
+ ```ruby
32
+ require "jsonrpc"
33
+
34
+ rpc = JsonRpc::Client.connect("https://rpc.domain.somewhere") {|args| ["authorisation token"] + args }
35
+ rpc.call("method", :a, :b, :c)
36
+ ```
37
+
38
+ ## Development
39
+
40
+ 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.
41
+
42
+ 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).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mexisme/jsonrpc-faraday. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonrpc/faraday"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jsonrpc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonrpc-faraday"
8
+ spec.version = JsonRpc::VERSION
9
+ spec.authors = ["mexisme"]
10
+ spec.email = ["wildjim+dev@kiwinet.org"]
11
+
12
+ spec.summary = "A simple JSON-RPC library, which defaults to using Faraday for the http-client wrapper."
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/mexisme/json-rpc-client"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting "allowed_push_host", or
17
+ # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ # else
22
+ # fail "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "logging"
31
+ spec.add_runtime_dependency "typhoeus"
32
+ spec.add_runtime_dependency "faraday"
33
+ spec.add_runtime_dependency "faraday_middleware"
34
+ spec.add_runtime_dependency "multi_json"
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.10"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.3"
39
+ spec.add_development_dependency "simplecov", "~> 0.10"
40
+ spec.add_development_dependency "rubocop"
41
+ spec.add_development_dependency "pry-byebug"
42
+ end
data/lib/jsonrpc.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "logging"
2
+ require "securerandom"
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+ require "typhoeus"
6
+ require "typhoeus/adapters/faraday"
7
+ require "multi_json"
8
+
9
+ require "jsonrpc/version"
10
+ require "jsonrpc/errors"
11
+ require "jsonrpc/client"
12
+ require "jsonrpc/client/faraday"
@@ -0,0 +1,56 @@
1
+ module JsonRpc
2
+ class Client
3
+ include Logger
4
+
5
+ attr_reader :connection, :rpc_id, :update_params
6
+
7
+ def self.connect(uri, &p)
8
+ self.new(Faraday.new(uri), &p)
9
+ end
10
+
11
+ # @param connection
12
+ # @param update_params [Proc]
13
+ # A call-back that will update the arg-list before it's passed via HTTP, e.g. to add authentication params
14
+ def initialize(connection, &update_params)
15
+ @connection = connection
16
+ @update_params = update_params || ->(args) { args }
17
+
18
+ next_rpc_id
19
+ end
20
+
21
+ def call(method, *args)
22
+ next_rpc_id
23
+ logger.debug "RPC #{method.inspect} #{args.inspect}"
24
+
25
+ begin
26
+ response = connection.call(json_rpc_request(method, args))
27
+ rescue ::JSON::ParserError
28
+ raise BadJsonReceivedError
29
+ end
30
+
31
+ logger.debug "Response = #{response.inspect}"
32
+
33
+ err = response["error"]
34
+ raise Error.new(err["code"], err["message"], err["data"]) if err
35
+
36
+ raise UnexpectedMessageIdError.new([response["id"], rpc_id]) unless response["id"].to_s == rpc_id.to_s
37
+ raise NoResultOrError unless response.key? "result"
38
+
39
+ response["result"]
40
+ end
41
+
42
+ private
43
+
44
+ def json_rpc_request(method, args)
45
+ { jsonrpc: "2.0",
46
+ id: rpc_id,
47
+ method: method,
48
+ params: update_params.call(args)
49
+ }
50
+ end
51
+
52
+ def next_rpc_id
53
+ @rpc_id = SecureRandom.uuid
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,43 @@
1
+ module JsonRpc
2
+ class Client
3
+ class Faraday
4
+ include Logger
5
+
6
+ attr_reader :uri
7
+
8
+ def initialize(uri)
9
+ @uri = uri
10
+ end
11
+
12
+ def connection
13
+ @connection ||= ::Faraday.new(url: uri) do |c|
14
+ logger.debug "Connecting to #{uri.inspect}..."
15
+
16
+ c.request :json
17
+
18
+ c.response :logger, logger.class[c]
19
+ c.response :json, content_type: /\bjson$/
20
+
21
+ # c.use :instrumentation
22
+
23
+ c.use ::FaradayMiddleware::FollowRedirects, limit: 3
24
+ c.use ::Faraday::Response::RaiseError # raise exceptions on 40x, 50x responses
25
+
26
+ c.adapter :typhoeus
27
+ end
28
+ end
29
+
30
+ # Send a JSON Request as a POST body
31
+ # @param json_request [Hash|JSON]
32
+ # @return [Hash|JSON] JSON response
33
+ def call(json_request)
34
+ response = connection.post do |r|
35
+ r.body = json_request
36
+ logger.debug "Request = #{r.inspect}"
37
+ end
38
+
39
+ response.body
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
1
+ module JsonRpc
2
+ module Logger
3
+ attr_writer :logger
4
+ def logger
5
+ @logger ||= Logging.logger[self]
6
+ end
7
+ end
8
+
9
+ class Error < StandardError
10
+ attr_reader :code, :message, :data
11
+
12
+ def initialize(code, message, data = nil)
13
+ @code = code
14
+ @message = message
15
+ @data = data
16
+ super(message)
17
+ end
18
+
19
+ def to_s
20
+ "Code #{code}: #{super}" + (@data ? " (#{@data.inspect})" : "")
21
+ end
22
+ end
23
+
24
+ class BadJsonReceivedError < Error
25
+ def initialize(data = nil)
26
+ super(-32603, "Bad JSON received", data)
27
+ end
28
+ end
29
+
30
+ class UnexpectedMessageIdError < Error
31
+ def initialize(data = nil)
32
+ super(-32603, "Unexpected Message ID", data)
33
+ end
34
+ end
35
+
36
+ class NoResultOrError < Error
37
+ def initialize(data = nil)
38
+ super(-32603, "Neither Result nor Error received", data)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module JsonRpc
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonrpc-faraday
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mexisme
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: typhoeus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.10'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.10'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: A simple JSON-RPC library, which defaults to using Faraday for the http-client
168
+ wrapper.
169
+ email:
170
+ - wildjim+dev@kiwinet.org
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".rubocop.yml"
178
+ - ".travis.yml"
179
+ - CODE_OF_CONDUCT.md
180
+ - Gemfile
181
+ - README.md
182
+ - Rakefile
183
+ - bin/console
184
+ - bin/setup
185
+ - jsonrpc-faraday.gemspec
186
+ - lib/jsonrpc.rb
187
+ - lib/jsonrpc/client.rb
188
+ - lib/jsonrpc/client/faraday.rb
189
+ - lib/jsonrpc/errors.rb
190
+ - lib/jsonrpc/version.rb
191
+ homepage: https://github.com/mexisme/json-rpc-client
192
+ licenses: []
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.4.5
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: A simple JSON-RPC library, which defaults to using Faraday for the http-client
214
+ wrapper.
215
+ test_files: []
216
+ has_rdoc: