jsoneur 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +92 -0
- data/Rakefile +5 -0
- data/examples/github.rb +29 -0
- data/jsoneur.gemspec +30 -0
- data/lib/Jsoneur/version.rb +3 -0
- data/lib/jsoneur.rb +23 -0
- data/lib/jsoneur/service.rb +64 -0
- data/spec/spec_helper.rb +15 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a18331361578a5fbfd4614f06f21142313087ba1
|
4
|
+
data.tar.gz: 2d2497bae8d8cece0dd3c07242cebb2bc3629f6c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7887ff54a7a7033fd0a997bc6240a6c7c5ba2e682c2c83e8951fced6d37ae76f4542f41588c291d66e7bcbecd5b04a019481170e82fa103b49cdda7267bc0d03
|
7
|
+
data.tar.gz: 5c4172f8a7a8a80212f2ea3b4a287f469e1860dc82412caee4cc8afd88b2b5be1beab3fabb81efbfb636f3038bec38a3aa07542b49c0736311fd39375935a865
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jsoneur
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p247
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Georg Kunz, http://georgkunz.coms
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Jsoneur
|
2
|
+
|
3
|
+
A very simple, general purpose JSON API client to quickly consume (read) any sane JSON API.
|
4
|
+
It is based on Faraday, provides a service registry for configuring multiple
|
5
|
+
API endpoints and allows quick usage of these services.
|
6
|
+
|
7
|
+
Features:
|
8
|
+
|
9
|
+
* Path and query string parameters.
|
10
|
+
* No intermediate classes need to be setup.
|
11
|
+
* Simple endpoint setup and access on a configures service.
|
12
|
+
* Flexible processing of requests and responses through Faraday.
|
13
|
+
* Returns JSON as a Mash which means it can be accessed in a pseudo-object way and is nice for using it in templates.
|
14
|
+
|
15
|
+
|
16
|
+
Limitations:
|
17
|
+
|
18
|
+
* Only get requests for now
|
19
|
+
* No splitting between query string and path parameters
|
20
|
+
* Probably many more ...
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
gem 'jsoneur'
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install jsoneur
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
A simple example
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'jsoneur'
|
43
|
+
|
44
|
+
# Configure Github API
|
45
|
+
Jsoneur.add('github_user_repos', 'https://api.github.com') do |service|
|
46
|
+
service.path = '/users/%{user}/repos'
|
47
|
+
|
48
|
+
# Define default parameters (default: {})
|
49
|
+
# service.default_params = {search: 'search-term'}
|
50
|
+
|
51
|
+
# Define the empty reponse in case nothing is found (default: [])
|
52
|
+
# service.empty_response = {}
|
53
|
+
|
54
|
+
# Setup your own Faraday Middleware stack
|
55
|
+
# service.connection do |faraday|
|
56
|
+
# faraday.response :mashify
|
57
|
+
# faraday.response :xml, :content_type => /\bxml$/
|
58
|
+
# # or set the Jsoneur defaults by calling
|
59
|
+
# set_faraday_defaults(faraday)
|
60
|
+
# edd
|
61
|
+
end
|
62
|
+
|
63
|
+
# Configure another API, e.g. Twitter
|
64
|
+
Jsoneur.add('tweets', 'https://api.twitter.com') do |service|
|
65
|
+
service.path = '/1.1/statuses/user_timeline.json'
|
66
|
+
# ...
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
# Consume an API
|
71
|
+
repos = Jsoneur['github_user_repos'].get(user: 'geku')
|
72
|
+
repos.each do |r|
|
73
|
+
puts "#{r.name} (#{r.owner.login})"
|
74
|
+
end
|
75
|
+
|
76
|
+
```
|
77
|
+
|
78
|
+
For a running example, see `examples/github.rb`.
|
79
|
+
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create new Pull Request
|
88
|
+
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
MIT License. Copyright 2013 Georg Kunz, http://georgkunz.com
|
data/Rakefile
ADDED
data/examples/github.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'jsoneur'
|
4
|
+
|
5
|
+
#
|
6
|
+
# Jsoneur example to list all public
|
7
|
+
# Github repos of a certain user
|
8
|
+
#
|
9
|
+
|
10
|
+
if ARGV.size < 1
|
11
|
+
puts "\n!!! Wrong number of parameters\n\n"
|
12
|
+
puts "ruby github.rb <username>"
|
13
|
+
exit
|
14
|
+
end
|
15
|
+
|
16
|
+
Jsoneur.add('github_user_repos', 'https://api.github.com') do |service|
|
17
|
+
service.path = '/users/%{user}/repos'
|
18
|
+
end
|
19
|
+
|
20
|
+
repos = Jsoneur['github_user_repos'].get(user: ARGV.first)
|
21
|
+
repos.each do |r|
|
22
|
+
puts " Name: #{r.name}"
|
23
|
+
puts " Owner: #{r.owner.login}"
|
24
|
+
puts " Desc: #{r.description}"
|
25
|
+
puts " Lang: #{r.language}"
|
26
|
+
puts ""
|
27
|
+
puts "=============================================="
|
28
|
+
puts ""
|
29
|
+
end
|
data/jsoneur.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jsoneur/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jsoneur"
|
8
|
+
spec.version = Jsoneur::VERSION
|
9
|
+
spec.authors = ["Georg Kunz"]
|
10
|
+
spec.email = ["kwd@gmx.ch"]
|
11
|
+
spec.description = 'General purpose JSON API client'
|
12
|
+
spec.summary = 'Very simple and generic JSON API client based on Faraday and providing a service registry for configuration and usage of multiple services.'
|
13
|
+
spec.homepage = "http://georgkunz.com"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 1.9"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency 'hashie'
|
23
|
+
spec.add_runtime_dependency 'multi_json'
|
24
|
+
spec.add_runtime_dependency 'faraday'
|
25
|
+
spec.add_runtime_dependency 'faraday_middleware'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
28
|
+
spec.add_development_dependency 'rake'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
30
|
+
end
|
data/lib/jsoneur.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
require 'jsoneur/version'
|
5
|
+
require 'jsoneur/service'
|
6
|
+
|
7
|
+
module Jsoneur
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def services
|
11
|
+
@services ||= {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(name, base_url, &block)
|
15
|
+
services[name] = Service.new(name, base_url, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](service_name)
|
19
|
+
services[service_name]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module Jsoneur
|
4
|
+
class Service
|
5
|
+
|
6
|
+
# Config methods
|
7
|
+
attr_accessor :path, :default_params, :empty_response
|
8
|
+
|
9
|
+
def initialize(name, base_url, &block)
|
10
|
+
@name = name
|
11
|
+
@base_url = base_url
|
12
|
+
|
13
|
+
# Set default config
|
14
|
+
@default_params = {}
|
15
|
+
@empty_response = []
|
16
|
+
|
17
|
+
block.call(self)
|
18
|
+
|
19
|
+
if @faraday.nil?
|
20
|
+
@faraday = Faraday.new(@base_url) do |f|
|
21
|
+
set_faraday_defaults(f)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def connection(&block)
|
27
|
+
@faraday = Faraday.new(@base_url, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_faraday_defaults(faraday)
|
31
|
+
faraday.request :json
|
32
|
+
|
33
|
+
faraday.response :mashify
|
34
|
+
faraday.response :json, :content_type => /\bjson$/
|
35
|
+
|
36
|
+
faraday.adapter Faraday.default_adapter
|
37
|
+
end
|
38
|
+
|
39
|
+
# Raises (depending on adapter that is used, by default NetHTTP)
|
40
|
+
# * Faraday::Error::ConnectionFailed on connection problems
|
41
|
+
# * Faraday::Error::TimeoutError on timeouts
|
42
|
+
# e.g. Typhoeus raises Faraday::Error::ClientError as well
|
43
|
+
def get(params = {})
|
44
|
+
final_path = path % urlencoded_params(params)
|
45
|
+
result = @faraday.get(final_path, default_params.merge(params))
|
46
|
+
|
47
|
+
if result && result.success? && result.body
|
48
|
+
result.body
|
49
|
+
else
|
50
|
+
empty_response
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
private
|
56
|
+
def urlencoded_params(params)
|
57
|
+
safe_params = params.dup
|
58
|
+
safe_params.each do |key, value|
|
59
|
+
safe_params[key] = URI::encode(value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'jsoneur'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
7
|
+
config.run_all_when_everything_filtered = true
|
8
|
+
config.filter_run :focus
|
9
|
+
|
10
|
+
# Run specs in random order to surface order dependencies. If you find an
|
11
|
+
# order dependency and want to debug it, you can fix the order by providing
|
12
|
+
# the seed, which is printed after each run.
|
13
|
+
# --seed 1234
|
14
|
+
config.order = 'random'
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsoneur
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Georg Kunz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
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: multi_json
|
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: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.14'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.14'
|
111
|
+
description: General purpose JSON API client
|
112
|
+
email:
|
113
|
+
- kwd@gmx.ch
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- .ruby-gemset
|
121
|
+
- .ruby-version
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- examples/github.rb
|
127
|
+
- jsoneur.gemspec
|
128
|
+
- lib/Jsoneur/version.rb
|
129
|
+
- lib/jsoneur.rb
|
130
|
+
- lib/jsoneur/service.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
homepage: http://georgkunz.com
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '1.9'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.0.3
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Very simple and generic JSON API client based on Faraday and providing a
|
156
|
+
service registry for configuration and usage of multiple services.
|
157
|
+
test_files:
|
158
|
+
- spec/spec_helper.rb
|