api_recipes 1.0.0 → 2.5.0
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 +60 -59
- data/lib/api_recipes/api.rb +69 -0
- data/lib/api_recipes/configuration.rb +8 -8
- data/lib/api_recipes/endpoint.rb +105 -30
- data/lib/api_recipes/exceptions.rb +24 -23
- data/lib/api_recipes/response.rb +18 -1
- data/lib/api_recipes/route.rb +158 -0
- data/lib/api_recipes/settings.rb +9 -6
- data/lib/api_recipes/version.rb +1 -1
- metadata +15 -34
- 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 -22
- 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 -192
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,22 +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 'http', '~> 4.1.1'
|
22
|
-
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
|
data/lib/api_recipes/resource.rb
DELETED
@@ -1,192 +0,0 @@
|
|
1
|
-
module ApiRecipes
|
2
|
-
class Resource
|
3
|
-
|
4
|
-
attr_reader :request, :response
|
5
|
-
|
6
|
-
def initialize(name, endpoint, routes = {})
|
7
|
-
@name = name
|
8
|
-
@routes = routes
|
9
|
-
@endpoint = endpoint
|
10
|
-
|
11
|
-
generate_routes
|
12
|
-
end
|
13
|
-
|
14
|
-
def fill(object)
|
15
|
-
data = @response.parse
|
16
|
-
if block_given?
|
17
|
-
tap do
|
18
|
-
try_to_fill object, data
|
19
|
-
yield object, data, @response.status
|
20
|
-
end
|
21
|
-
else
|
22
|
-
try_to_fill object, data
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def build_path(route_name, route_attributes, provided_params)
|
29
|
-
path = route_attributes[:path] || ''
|
30
|
-
|
31
|
-
required_params_for_path(path).each do |rp|
|
32
|
-
unless p = provided_params.delete(rp)
|
33
|
-
raise MissingRouteAttribute.new(@name, route_name, rp)
|
34
|
-
end
|
35
|
-
path.gsub! ":#{rp}", p.to_s
|
36
|
-
end
|
37
|
-
path = "#{settings[:base_path]}#{settings[:api_version]}/#{@name}#{path}"
|
38
|
-
return path, provided_params
|
39
|
-
end
|
40
|
-
|
41
|
-
def build_request(route, route_attributes, *pars)
|
42
|
-
unless route_attributes
|
43
|
-
route_attributes = {}
|
44
|
-
end
|
45
|
-
# Merge route attributes with defaults and deep clone route attributes
|
46
|
-
route_attributes = Marshal.load(Marshal.dump(Settings::DEFAULT_ROUTE_ATTRIBUTES.merge(route_attributes).deep_symbolize_keys))
|
47
|
-
|
48
|
-
params = pars.extract_options!
|
49
|
-
path, residual_params = build_path(route, route_attributes, params)
|
50
|
-
residual_params = nil unless residual_params.any?
|
51
|
-
uri = build_uri_from path
|
52
|
-
|
53
|
-
@request = request_with_auth
|
54
|
-
@response = @request.send(route_attributes[:method], uri, encode_residual_params(route_attributes, residual_params))
|
55
|
-
check_response_code route, route_attributes, @response
|
56
|
-
|
57
|
-
if block_given?
|
58
|
-
data = @response.parse
|
59
|
-
tap { yield data, @response.status }
|
60
|
-
else
|
61
|
-
self
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def build_uri_from(path)
|
66
|
-
attrs = {
|
67
|
-
scheme: settings[:protocol],
|
68
|
-
host: settings[:host],
|
69
|
-
port: port,
|
70
|
-
path: path
|
71
|
-
}
|
72
|
-
URI::Generic.build attrs
|
73
|
-
end
|
74
|
-
|
75
|
-
def check_response_code(route, route_attributes, response)
|
76
|
-
# Check if :ok_code is present, check the response
|
77
|
-
if ok_code = route_attributes[:ok_code]
|
78
|
-
code = response.status.code
|
79
|
-
# If the code does not match, apply the requested strategy (see FAIL_OPTIONS)
|
80
|
-
unless code == ok_code
|
81
|
-
case settings[:on_nok_code]
|
82
|
-
when :do_nothing
|
83
|
-
when :raise
|
84
|
-
raise ResponseCodeNotAsExpected.new(nil, @name, route, ok_code, code, response.body)
|
85
|
-
when :return_false
|
86
|
-
return false
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def encode_residual_params(route_attributes, residual_params)
|
93
|
-
# If :encode_params_as is specified and avalable use it
|
94
|
-
if Settings::AVAILABLE_PARAMS_ENCODINGS.include? route_attributes[:encode_params_as].to_s
|
95
|
-
{ route_attributes[:encode_params_as].to_sym => residual_params }
|
96
|
-
else
|
97
|
-
# Default to query string params (get) or json (other methods)
|
98
|
-
case route_attributes[:method].to_sym
|
99
|
-
when :get
|
100
|
-
{ params: residual_params }
|
101
|
-
when :post, :put, :patch, :delete
|
102
|
-
{ json: residual_params }
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def extract_headers
|
108
|
-
settings[:default_headers] || {}
|
109
|
-
end
|
110
|
-
|
111
|
-
# Generate routes some_endpoint.some_resource.some_route methods
|
112
|
-
# e.g. webapp.alarms.index
|
113
|
-
def generate_routes
|
114
|
-
@routes.each do |route, attrs|
|
115
|
-
# Check if route name clashes with resource name
|
116
|
-
if route.eql? @name
|
117
|
-
raise RouteAndResourceNamesClashError.new(route, @name)
|
118
|
-
end
|
119
|
-
unless respond_to? route.to_sym
|
120
|
-
define_singleton_method route.to_sym do |*params, &block|
|
121
|
-
build_request route, attrs, *params, &block
|
122
|
-
end
|
123
|
-
else
|
124
|
-
raise RouteNameClashWithExistentMethod.new(@name, route)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
self
|
128
|
-
end
|
129
|
-
|
130
|
-
def timeout
|
131
|
-
settings.fetch(:timeout, ApiRecipes::Settings::GLOBAL_TIMEOUT)
|
132
|
-
end
|
133
|
-
|
134
|
-
def port
|
135
|
-
settings[:port] || case settings[:protocol]
|
136
|
-
when 'http'
|
137
|
-
80
|
138
|
-
when 'https'
|
139
|
-
443
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
def request_with_auth
|
144
|
-
req = HTTP
|
145
|
-
req = req.headers(extract_headers)
|
146
|
-
.timeout(timeout)
|
147
|
-
|
148
|
-
basic_auth = @endpoint.basic_auth
|
149
|
-
if basic_auth
|
150
|
-
req = req.basic_auth basic_auth
|
151
|
-
end
|
152
|
-
authorization = @endpoint.authorization
|
153
|
-
if authorization
|
154
|
-
req = req.auth authorization
|
155
|
-
end
|
156
|
-
req
|
157
|
-
end
|
158
|
-
|
159
|
-
def required_params_for_path(path)
|
160
|
-
path.scan(/:(\w+)/).flatten.map { |p| p.to_sym }
|
161
|
-
end
|
162
|
-
|
163
|
-
def settings
|
164
|
-
@endpoint.configs
|
165
|
-
end
|
166
|
-
|
167
|
-
def try_to_fill(object, data)
|
168
|
-
case data
|
169
|
-
when Hash
|
170
|
-
res = fill_object_with object, data
|
171
|
-
when Array
|
172
|
-
res = []
|
173
|
-
data.each do |element|
|
174
|
-
res << fill_object_with(object.new, element)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
res
|
179
|
-
end
|
180
|
-
|
181
|
-
def fill_object_with(object, data)
|
182
|
-
data.each do |key, value|
|
183
|
-
begin
|
184
|
-
object.send "#{key}=", value
|
185
|
-
rescue
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
object
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|