api_recipes 0.7.0 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +2 -2
- data/lib/api_recipes.rb +61 -60
- data/lib/api_recipes/api.rb +70 -0
- data/lib/api_recipes/configuration.rb +9 -9
- data/lib/api_recipes/endpoint.rb +109 -30
- data/lib/api_recipes/exceptions.rb +31 -23
- data/lib/api_recipes/response.rb +22 -0
- data/lib/api_recipes/route.rb +156 -0
- data/lib/api_recipes/settings.rb +9 -6
- data/lib/api_recipes/version.rb +1 -1
- metadata +15 -48
- data/.gitignore +0 -16
- data/.rspec +0 -2
- data/.travis.yml +0 -10
- data/CHANGELOG.md +0 -15
- data/CODE_OF_CONDUCT.md +0 -13
- data/Gemfile +0 -12
- data/Guardfile +0 -14
- data/Rakefile +0 -6
- data/api_recipes.gemspec +0 -23
- data/api_recipes.png +0 -0
- data/bin/console +0 -14
- data/bin/setup +0 -7
- data/examples/authorization.rb +0 -42
- data/examples/authorization_with_default_headers.rb +0 -41
- data/examples/basic_auth.rb +0 -32
- data/examples/config/apis.yml +0 -43
- data/examples/custom_configs.rb +0 -51
- data/examples/delete_me.rb +0 -21
- data/examples/multiple_endpoints.rb +0 -54
- data/examples/simple_usage.rb +0 -38
- data/lib/api_recipes/resource.rb +0 -157
data/CHANGELOG.md
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
## Version 0.6.0 - Default to HTTPS
|
2
|
-
|
3
|
-
**POSSIBLE BREAKING CHANGES**
|
4
|
-
|
5
|
-
Requests now default to HTTPS. If you still want to use (unsecure) HTTP you have to specify it in your configs through the dedicated `protocol` option e.g.
|
6
|
-
|
7
|
-
```yaml
|
8
|
-
github:
|
9
|
-
protocol: http
|
10
|
-
host: api.github.com
|
11
|
-
```
|
12
|
-
|
13
|
-
## Version 0.5.0 - Better Exceptions
|
14
|
-
|
15
|
-
Exceptions now include attributes that can be accessed during the `rescue` phase in order to execute specific actions
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,13 +0,0 @@
|
|
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
DELETED
data/Guardfile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
guard :rspec, cmd: 'bundle exec rspec' do
|
2
|
-
require 'guard/rspec/dsl'
|
3
|
-
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
-
|
5
|
-
# RSpec files
|
6
|
-
rspec = dsl.rspec
|
7
|
-
watch(rspec.spec_helper) { rspec.spec_dir }
|
8
|
-
watch(rspec.spec_support) { rspec.spec_dir }
|
9
|
-
watch(rspec.spec_files)
|
10
|
-
|
11
|
-
# Ruby files
|
12
|
-
ruby = dsl.ruby
|
13
|
-
dsl.watch_spec_files_for(ruby.lib_files)
|
14
|
-
end
|
data/Rakefile
DELETED
data/api_recipes.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'api_recipes/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = 'api_recipes'
|
8
|
-
spec.version = ApiRecipes::VERSION
|
9
|
-
spec.authors = ['Alessandro Verlato']
|
10
|
-
spec.email = ['averlato@gmail.com']
|
11
|
-
|
12
|
-
spec.summary = %q{Consume HTTP APIs with style}
|
13
|
-
spec.homepage = 'https://github.com/madAle/api_recipes'
|
14
|
-
spec.license = 'MIT'
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
-
spec.require_paths = ['lib']
|
19
|
-
spec.required_ruby_version = '> 2.2.0'
|
20
|
-
|
21
|
-
spec.add_dependency 'oj', '~> 3.7.0'
|
22
|
-
spec.add_dependency 'http', '~> 4.1.1'
|
23
|
-
end
|
data/api_recipes.png
DELETED
Binary file
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "api_recipes"
|
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
DELETED
data/examples/authorization.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Configure RemoteApi through a yaml file.
|
5
|
-
# Take a look at examples/config/apis.yml for details
|
6
|
-
ApiRecipes.configure do |config|
|
7
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
8
|
-
end
|
9
|
-
|
10
|
-
# Let's create a simple class that uses ApiRecipes
|
11
|
-
class MyFancyClass
|
12
|
-
include ApiRecipes
|
13
|
-
|
14
|
-
# Declare the endpoints that we're going to use
|
15
|
-
endpoint :github
|
16
|
-
end
|
17
|
-
|
18
|
-
# Setup Auth. This will set/replace the HTTP 'Authorization' header with the
|
19
|
-
# provided value
|
20
|
-
|
21
|
-
# Get your OAUTH token on Github.
|
22
|
-
# Navigate Profile => Settings => Personal Access Tokens => Generate new token
|
23
|
-
# Copy the generated string and substitute it to YOUR_GITHUB_OAUTH_TOKEN below
|
24
|
-
MyFancyClass.github.authorization = 'token YOUR_GITHUB_OAUTH_TOKEN'
|
25
|
-
|
26
|
-
MyFancyClass.github.users.list do |users|
|
27
|
-
puts users.collect{ |user| user['login'] }
|
28
|
-
end
|
29
|
-
|
30
|
-
# From now on every MyFancyClass (and its instances) github's api request will
|
31
|
-
# be authenticated through the provided auth
|
32
|
-
|
33
|
-
# Get user's usernames from Github's Apis (https://github.com)
|
34
|
-
usernames = nil
|
35
|
-
MyFancyClass.github.users.list do |users|
|
36
|
-
usernames = users.collect{ |user| user['login'] }
|
37
|
-
end
|
38
|
-
|
39
|
-
# Get user's repos
|
40
|
-
MyFancyClass.github.users.repos(user_id: usernames.first) do |repos|
|
41
|
-
puts repos
|
42
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Configure RemoteApi through a yaml file.
|
5
|
-
# Take a look at examples/config/apis.yml for details
|
6
|
-
ApiRecipes.configure do |config|
|
7
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
8
|
-
end
|
9
|
-
|
10
|
-
# Let's create a simple class that uses ApiRecipes
|
11
|
-
class MyFancyClass
|
12
|
-
include ApiRecipes
|
13
|
-
|
14
|
-
# Get your OAUTH token on Github.
|
15
|
-
# Navigate Profile => Settings => Personal Access Tokens => Generate new token
|
16
|
-
# Copy the generated string and substitute it to YOUR_GITHUB_OAUTH_TOKEN below
|
17
|
-
|
18
|
-
MY_CUSTOM_CONFIG = {
|
19
|
-
default_headers: {
|
20
|
-
'Authorization' => 'token YOUR_GITHUB_OAUTH_TOKEN'
|
21
|
-
}
|
22
|
-
}
|
23
|
-
|
24
|
-
# Declare the endpoints that we're going to use
|
25
|
-
# Add some custom configs
|
26
|
-
endpoint :github, MY_CUSTOM_CONFIG
|
27
|
-
end
|
28
|
-
|
29
|
-
# From now on every MyFancyClass (and its instances) github's api request will
|
30
|
-
# be authenticated through the provided Authorization header
|
31
|
-
|
32
|
-
# Get user's usernames from Github's Apis (https://github.com)
|
33
|
-
usernames = nil
|
34
|
-
MyFancyClass.github.users.list do |users|
|
35
|
-
usernames = users.collect{ |user| user['login'] }
|
36
|
-
end
|
37
|
-
|
38
|
-
# Get user's repos
|
39
|
-
MyFancyClass.github.users.repos(user_id: usernames.first) do |repos|
|
40
|
-
puts repos
|
41
|
-
end
|
data/examples/basic_auth.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Configure RemoteApi through a yaml file.
|
5
|
-
# Take a look at examples/config/apis.yml for details
|
6
|
-
ApiRecipes.configure do |config|
|
7
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
8
|
-
end
|
9
|
-
|
10
|
-
# Let's create a simple class that uses ApiRecipes
|
11
|
-
class MyFancyClass
|
12
|
-
include ApiRecipes
|
13
|
-
|
14
|
-
# Declare the endpoints that we're going to use
|
15
|
-
endpoint :github
|
16
|
-
end
|
17
|
-
|
18
|
-
# Setup Basic Auth
|
19
|
-
MyFancyClass.github.basic_auth = { user: 'github_username', pass: 'github_password' }
|
20
|
-
|
21
|
-
# From now on every github's api request will be authenticated with basic auth
|
22
|
-
|
23
|
-
# Get user's usernames from Github's Apis (https://github.com)
|
24
|
-
usernames = nil
|
25
|
-
MyFancyClass.github.users.list do |users|
|
26
|
-
usernames = users.collect{ |user| user['login'] }
|
27
|
-
end
|
28
|
-
|
29
|
-
# Get user's repos
|
30
|
-
MyFancyClass.github.users.repos(user_id: usernames.first) do |repos|
|
31
|
-
puts repos
|
32
|
-
end
|
data/examples/config/apis.yml
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
github:
|
2
|
-
protocol: https # Optional, defaults to: 'https'
|
3
|
-
host: api.github.com # Optional, defaults to: 'localhost'
|
4
|
-
port: 443 # Optional, defaults to: 80 for http and 443 for https
|
5
|
-
base_path: # Optional, defaults to: ''
|
6
|
-
api_version: # Optional, defaults to: ''
|
7
|
-
timeout: 3 # Optional, defaults to: 3 (time expressed in seconds)
|
8
|
-
on_nok_code: :raise # Optional, defaults to :raise. Possible values: [:do_nothing, :raise, :return_false]
|
9
|
-
default_headers:
|
10
|
-
accept: 'application/vnd.github.v3+json'
|
11
|
-
content_type: 'application/json'
|
12
|
-
|
13
|
-
routes:
|
14
|
-
users:
|
15
|
-
list:
|
16
|
-
path: # Optional, defaults to: ''
|
17
|
-
method: get # Optional, defaults to: get
|
18
|
-
ok_code: 200 # Optional, defaults to: 200
|
19
|
-
|
20
|
-
show:
|
21
|
-
path: /:user_id # You can use :some_param notation and provide the param when calling the route (see examples/simple.rb)
|
22
|
-
|
23
|
-
repos:
|
24
|
-
path: /:user_id/repos
|
25
|
-
|
26
|
-
|
27
|
-
# Example of test endpoint with minimal configuration and multiple routes
|
28
|
-
jsonplaceholder:
|
29
|
-
host: jsonplaceholder.typicode.com
|
30
|
-
|
31
|
-
routes:
|
32
|
-
users:
|
33
|
-
list:
|
34
|
-
show:
|
35
|
-
path: /:user_id # You can use :some_param notation and provide params when calling the route (see examples/simple.rb)
|
36
|
-
|
37
|
-
posts:
|
38
|
-
list:
|
39
|
-
show:
|
40
|
-
path: /:post_id
|
41
|
-
|
42
|
-
comments:
|
43
|
-
path: /:post_id/comments
|
data/examples/custom_configs.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# This example aims to demonstrate how to provide custom configs to endpoints
|
2
|
-
|
3
|
-
require 'api_recipes'
|
4
|
-
|
5
|
-
# Configure RemoteApi through custom settings
|
6
|
-
FAKE_API_SETTINGS = {
|
7
|
-
host: 'jsonplaceholder.typicode.com',
|
8
|
-
timeout: 10,
|
9
|
-
routes: {
|
10
|
-
users: {
|
11
|
-
list: nil,
|
12
|
-
show: {
|
13
|
-
path: '/:user_id'
|
14
|
-
}
|
15
|
-
},
|
16
|
-
posts: {
|
17
|
-
list: nil,
|
18
|
-
comments: {
|
19
|
-
path: '/:post_id/comments'
|
20
|
-
}
|
21
|
-
}
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
# Let's create a simple class that uses ApiRecipes
|
26
|
-
class MyFancyClass
|
27
|
-
include ApiRecipes
|
28
|
-
|
29
|
-
# Declare the endpoints that we're going to use
|
30
|
-
endpoint :fake_api, FAKE_API_SETTINGS
|
31
|
-
end
|
32
|
-
|
33
|
-
# Let's demonstrate that even a class instance can use enpoints
|
34
|
-
fancy_instance = MyFancyClass.new
|
35
|
-
|
36
|
-
# Get User's names
|
37
|
-
fancy_instance.fake_api.users.list do |users|
|
38
|
-
names = users.collect{ |user| user['name'] }
|
39
|
-
puts "Names: #{names}"
|
40
|
-
end
|
41
|
-
|
42
|
-
# Collect post ids
|
43
|
-
post_ids = nil
|
44
|
-
MyFancyClass.fake_api.posts.list do |posts|
|
45
|
-
post_ids = posts.map{ |post| post['id'] }
|
46
|
-
end
|
47
|
-
|
48
|
-
# Fetch post's comments
|
49
|
-
fancy_instance.fake_api.posts.comments(post_id: post_ids.last) do |comments|
|
50
|
-
puts "Comments: #{comments}"
|
51
|
-
end
|
data/examples/delete_me.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
ApiRecipes.configure do |config|
|
5
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
6
|
-
end
|
7
|
-
|
8
|
-
class Foo
|
9
|
-
include ApiRecipes
|
10
|
-
|
11
|
-
endpoint :jsonplaceholder
|
12
|
-
end
|
13
|
-
|
14
|
-
puts 'Use from class:'
|
15
|
-
puts Foo.jsonplaceholder.users.list
|
16
|
-
|
17
|
-
f1 = Foo.new
|
18
|
-
|
19
|
-
puts "\n\nUse from instance:"
|
20
|
-
sleep 1
|
21
|
-
puts f1.jsonplaceholder.users.list
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Configure RemoteApi through a yaml file.
|
5
|
-
# Take a look at examples/config/apis.yml for details
|
6
|
-
ApiRecipes.configure do |config|
|
7
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
8
|
-
end
|
9
|
-
|
10
|
-
# Let's create a simple class that uses ApiRecipes
|
11
|
-
class MyFancyClass
|
12
|
-
include ApiRecipes
|
13
|
-
|
14
|
-
# Declare the endpoints that we're going to use
|
15
|
-
endpoint :jsonplaceholder
|
16
|
-
endpoint :github
|
17
|
-
end
|
18
|
-
|
19
|
-
# Get users from JSONPlaceholder (http://jsonplaceholder.typicode.com/users)
|
20
|
-
MyFancyClass.jsonplaceholder.users.list do |users|
|
21
|
-
puts 'Usernames:'
|
22
|
-
puts users.collect{ |user| user['name'] }
|
23
|
-
end
|
24
|
-
|
25
|
-
# Get posts from JSONPlaceholder (http://jsonplaceholder.typicode.com/posts)
|
26
|
-
post_ids = nil
|
27
|
-
MyFancyClass.jsonplaceholder.posts.list do |posts|
|
28
|
-
post_ids = posts.map{ |post| post['id'] }
|
29
|
-
end
|
30
|
-
|
31
|
-
puts "\n\n\n\n"
|
32
|
-
# Get first post's comments from JSONPlaceholder
|
33
|
-
# http://jsonplaceholder.typicode.com/posts/1/comments
|
34
|
-
MyFancyClass.jsonplaceholder.posts.comments(post_id: post_ids.first) do |comments|
|
35
|
-
puts "Comments:"
|
36
|
-
puts comments
|
37
|
-
end
|
38
|
-
|
39
|
-
puts "\n\n\n\n"
|
40
|
-
# Get also Github users
|
41
|
-
# Warning: Github has a low rate limit for unauthorized api requests.
|
42
|
-
MyFancyClass.github.users.list do |body, status_code, status_message|
|
43
|
-
puts "Response: #{status_code}: #{status_message}"
|
44
|
-
puts "Data: #{body}"
|
45
|
-
end
|
46
|
-
|
47
|
-
# If you need a 'raw' response (e.g. the response must not automatically be
|
48
|
-
# parsed as JSON) just don't provide a block and the result will be an
|
49
|
-
# HTTP::Response object. See https://github.com/httprb/http for further details
|
50
|
-
|
51
|
-
response = MyFancyClass.github.users.list
|
52
|
-
puts response.code # e.g. 200
|
53
|
-
puts response.reason # e.g. OK
|
54
|
-
puts response.body # e.g Some response body (not necessarely JSON)
|
data/examples/simple_usage.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'api_recipes'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Configure RemoteApi through a yaml file.
|
5
|
-
# Take a look at examples/config/apis.yml for details
|
6
|
-
ApiRecipes.configure do |config|
|
7
|
-
config.endpoints_configs = YAML.load_file(File.expand_path('examples/config/apis.yml'))
|
8
|
-
end
|
9
|
-
|
10
|
-
# Let's create a simple class that uses ApiRecipes
|
11
|
-
class MyFancyClass
|
12
|
-
include ApiRecipes
|
13
|
-
|
14
|
-
endpoint :github
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
# Warning: Github has a low rate limit for unauthorized api requests.
|
19
|
-
|
20
|
-
|
21
|
-
# Get user's usernames from Github's Apis (https://github.com)
|
22
|
-
usernames = nil
|
23
|
-
|
24
|
-
MyFancyClass.github.users.list do |users|
|
25
|
-
usernames = users.collect{ |user| user['login'] }
|
26
|
-
end
|
27
|
-
|
28
|
-
# Get user's repos
|
29
|
-
MyFancyClass.github.users.repos(user_id: usernames.first) do |repos|
|
30
|
-
puts repos
|
31
|
-
end
|
32
|
-
|
33
|
-
# The endpoints are available on instances too
|
34
|
-
fancy = MyFancyClass.new
|
35
|
-
|
36
|
-
fancy.github.users.list do |users|
|
37
|
-
puts users.collect{ |user| user['login'] }
|
38
|
-
end
|