api_recipes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzBiNzgwZTQyYjI2MmU3NDk2Mzg3ZTA2ZmYzMWViM2E1YmRlN2RlNw==
5
+ data.tar.gz: !binary |-
6
+ N2Y1YTc4OTcxZGI3NWYxYWJmMDAxZWY4N2ViZTA3N2EyYzdhY2JmMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ M2VhNDJlZjYxMzQ3YjkxNDkyOGFlYTI0OGI5MTJjMjM1ZWJmMWU5MTg4M2Rl
10
+ NjRlOWViOWVjNTk0NmRiZWYyMTA2MjAzNGFkYWU0YTc0YTgzNmUyNDE1NjA5
11
+ N2ZkZTEzZTNjMDUxNTY1YTE3N2Y1YTUxYzdiNTc1NGRlMDU1ZWQ=
12
+ data.tar.gz: !binary |-
13
+ NzQxMWY0NGI3NzQzMzBlZDFjNWM0MjE0M2ZkNzQ1NTdjMGU5YmI2NTAzYWZi
14
+ ZTUzNjk5NDBiYzQ4ZjQ4MGY0ZWEzYzAxZjFkZGVjMmM1ZTVlZWM1NjljNjJj
15
+ NTQ0Nzg3MWU0OWU5MDE4M2UyMzAxNzY5NDk0MjY4ZjY3MGFhZDc=
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.orig
11
+ .idea
12
+ .rvmrc
13
+ .ruby-version
14
+ .ruby-gemset
15
+ .DS_Store
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ - 2.2.3
8
+ notifications:
9
+ email: false
10
+ bundler_args: --without development
@@ -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,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api_recipes.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 10.0'
7
+
8
+ group :development do
9
+ gem 'guard', '~> 2.13.0'
10
+ gem 'guard-rspec', '~> 4.6.4'
11
+ gem 'guard-bundler', '~> 2.1.0'
12
+ end
13
+
14
+ group :test do
15
+ gem 'rspec', '~> 3.3'
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+
6
+ # RSpec files
7
+ rspec = dsl.rspec
8
+ watch(rspec.spec_helper) { rspec.spec_dir }
9
+ watch(rspec.spec_support) { rspec.spec_dir }
10
+ watch(rspec.spec_files)
11
+
12
+ # Ruby files
13
+ ruby = dsl.ruby
14
+ dsl.watch_spec_files_for(ruby.lib_files)
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Alessandro Verlato
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.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ [![Code Climate](https://codeclimate.com/github/madAle/api_recipes/badges/gpa.svg)](https://codeclimate.com/github/madAle/api_recipes)
2
+
3
+ # ApiRecipes
4
+
5
+ Consume HTTP APIs with style
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'api_recipes'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install api_recipes
22
+
23
+ ## Compatibility
24
+
25
+ Ruby 1.9.3 or higher.
26
+
27
+ ## Usage
28
+
29
+ ... in progress.
30
+
31
+ In the meantime take a look at the ``` examples ``` folder. Thank you.
32
+
33
+ ## TODO
34
+
35
+ * Write specs
36
+ * Write a good Usage
37
+ * Add more examples
38
+ * Delete this TODO section
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
43
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update
46
+ the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for
47
+ the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/madAle/api_recipes.
52
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
53
+ the [Contributor Covenant](contributor-covenant.org) code of conduct.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
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
@@ -0,0 +1,25 @@
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 = '>= 1.9.3'
20
+
21
+ spec.add_dependency 'oj', '~> 2.13.0'
22
+ spec.add_dependency 'oj_mimic_json', '~> 1.0.1'
23
+ spec.add_dependency 'http', '~> 0.9.0'
24
+ spec.add_dependency 'activesupport', '>= 4.0.0'
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
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 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
+ 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
@@ -0,0 +1,41 @@
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
@@ -0,0 +1,32 @@
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
@@ -0,0 +1,43 @@
1
+ github:
2
+ protocol: https # Optional, defaults to: 'http'
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: 1 (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 params 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
@@ -0,0 +1,50 @@
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
+ routes: {
9
+ users: {
10
+ list: nil,
11
+ show: {
12
+ path: '/:user_id'
13
+ }
14
+ },
15
+ posts: {
16
+ list: nil,
17
+ comments: {
18
+ path: '/:post_id/comments'
19
+ }
20
+ }
21
+ }
22
+ }
23
+
24
+ # Let's create a simple class that uses ApiRecipes
25
+ class MyFancyClass
26
+ include ApiRecipes
27
+
28
+ # Declare the endpoints that we're going to use
29
+ endpoint :fake_api, FAKE_API_SETTINGS
30
+ end
31
+
32
+ # Let's demonstrate that even a class instance can use enpoints
33
+ fancy_instance = MyFancyClass.new
34
+
35
+ # Get User's names
36
+ fancy_instance.fake_api.users.list do |users|
37
+ names = users.collect{ |user| user['name'] }
38
+ puts "Names: #{names}"
39
+ end
40
+
41
+ # Collect post ids
42
+ post_ids = nil
43
+ MyFancyClass.fake_api.posts.list do |posts|
44
+ post_ids = posts.map{ |post| post['id'] }
45
+ end
46
+
47
+ # Fetch post's comments
48
+ fancy_instance.fake_api.posts.comments(post_id: post_ids.last) do |comments|
49
+ puts "Comments: #{comments}"
50
+ end
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,54 @@
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)
@@ -0,0 +1,32 @@
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
+ # Add some custom configs
16
+ endpoint :github
17
+ end
18
+
19
+
20
+ # Warning: Github has a low rate limit for unauthorized api requests.
21
+
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
@@ -0,0 +1,16 @@
1
+ module ApiRecipes
2
+ class Configuration
3
+
4
+ def endpoints_configs=(configs = {})
5
+ raise ArgumentError, 'endpoints_configs must be an Hash' unless configs.is_a? Hash
6
+ @endpoints_configs = configs.deep_symbolize_keys
7
+ end
8
+
9
+ def endpoints_configs
10
+ unless @endpoints_configs
11
+ @endpoints_configs = {}
12
+ end
13
+ @endpoints_configs
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module ApiRecipes
2
+ class Endpoint
3
+
4
+ attr_accessor :name, :settings, :authorization, :basic_auth
5
+ attr_reader :resources
6
+
7
+ def initialize(name, config)
8
+ @name = name
9
+ @config = config
10
+ @settings = ApiRecipes::Settings::DEFAULT.merge config.endpoints_configs[name]
11
+
12
+ # Generate some_endpoint.some_resource methods
13
+ # e.g. github.users
14
+ @resources = [] unless @resources
15
+ @settings[:routes].each do |resource, routes|
16
+ @resources << resource
17
+ res = Resource.new resource, self, routes
18
+ define_singleton_method resource do
19
+ res
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,65 @@
1
+ module ApiRecipes
2
+ class RouteNameClashError < Exception
3
+ def initialize(message = nil, route = nil, resource = nil)
4
+ if message
5
+ # Nothing to do
6
+ elsif route
7
+ message = "route name (#{route}) can't be equal to resource name (#{resource}). Please change route or resource name."
8
+ else
9
+ message = "route name can't be equal to resource name. Please change route or resource names."
10
+ end
11
+ super(message)
12
+ end
13
+ end
14
+
15
+ class MissingRouteAttribute < Exception
16
+ def initialize(message = nil, resource = nil, route = nil, attribute = nil)
17
+ if message
18
+ # Nothing to do
19
+ elsif route && attribute
20
+ message = "route '#{resource}.#{route}' requires '#{attribute}' attribute but this was not given"
21
+ end
22
+ super(message)
23
+ end
24
+ end
25
+
26
+ class ResponseCodeNotAsExpected < Exception
27
+ def initialize(message = nil, resource = nil, route = nil, expected_code = nil, actual_code = nil, reason = nil)
28
+ if message
29
+ # Nothing to do
30
+ else
31
+ message = "response code for request on route '#{resource}.#{route}' has returned #{actual_code}, but #{expected_code} was expected. Reason: #{reason}"
32
+ end
33
+ super(message)
34
+ end
35
+ end
36
+
37
+ class EndpointConfigIsNotAnHash < Exception
38
+ def initialize(message = nil, endpoint = nil)
39
+ if message
40
+ # Nothing to do
41
+ else
42
+ message = "provided config for endpoint '#{endpoint}' must be an Hash"
43
+ end
44
+ super(message)
45
+ end
46
+ end
47
+
48
+ class NoConfigurationGivenForEndpoint < Exception
49
+ def initialize(message = nil, endpoint = nil)
50
+ if message
51
+ # Nothing to do
52
+ else
53
+ message = "no configuration provided for endpoint '#{endpoint}'"
54
+ end
55
+ super(message)
56
+ end
57
+ end
58
+
59
+ class RouteNameClashWithExistentMethod < Exception
60
+ def initialize(resource, route)
61
+ message = "can't define route '#{route}' method in resource '#{resource}' because method '#{route}' already exists"
62
+ super(message)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,151 @@
1
+ module ApiRecipes
2
+ class Resource
3
+
4
+ attr_accessor :authorization, :basic_auth
5
+
6
+ def initialize(name, endpoint, routes = {})
7
+ @name = name
8
+ @routes = routes
9
+ @endpoint = endpoint
10
+
11
+ generate_routes
12
+ end
13
+
14
+ private
15
+
16
+ def build_path(route_name, route_attributes, provided_params)
17
+ path = route_attributes[:path] || ''
18
+
19
+ required_params_for_path(path).each do |rp|
20
+ unless p = provided_params.delete(rp)
21
+ raise MissingRouteAttribute.new(@name, route_name, rp)
22
+ end
23
+ path.gsub! ":#{rp}", p.to_s
24
+ end
25
+ path = "#{settings[:base_path]}#{settings[:api_version]}/#{@name}#{path}"
26
+ return path, provided_params
27
+ end
28
+
29
+ def build_request
30
+ request_with_auth
31
+ .headers(extract_headers)
32
+ .timeout :global,
33
+ write: per_kind_timeout,
34
+ connect: per_kind_timeout,
35
+ read: per_kind_timeout
36
+ end
37
+
38
+ def build_uri_from(path)
39
+ attrs = {
40
+ scheme: settings[:protocol],
41
+ host: settings[:host],
42
+ port: port,
43
+ path: path
44
+ }
45
+ URI::Generic.build attrs
46
+ end
47
+
48
+ def check_response_code(route, route_attributes, response)
49
+ # Check if :ok_code is present, check the response
50
+ if ok_code = route_attributes[:ok_code]
51
+ code = response.code
52
+ # If the code does not match, apply the requested strategy (see FAIL_OPTIONS)
53
+ unless code == ok_code
54
+ case settings[:on_nok_code]
55
+ when :do_nothing
56
+ when :raise
57
+ raise ResponseCodeNotAsExpected.new(nil, @name, route, ok_code, code, response.body)
58
+ when :return_false
59
+ return false
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def extract_headers
66
+ settings[:default_headers] || {}
67
+ end
68
+
69
+ # Generate routes some_endpoint.some_resource.some_route methods
70
+ # e.g. webapp.alarms.index
71
+ def generate_routes
72
+ @routes.each do |route, attrs|
73
+ if route.eql? @name
74
+ raise RouteNameClashError.new(route, @name)
75
+ end
76
+ unless respond_to? route.to_sym
77
+ define_singleton_method route.to_sym do |*params, &block|
78
+ start_request route, attrs, *params, &block
79
+ end
80
+ else
81
+ raise RouteNameClashWithExistentMethod.new(@name, route)
82
+ end
83
+ end
84
+ self
85
+ end
86
+
87
+ def per_kind_timeout
88
+ settings.fetch(:timeout, ApiRecipes::Settings::GLOBAL_TIMEOUT)/3.0
89
+ end
90
+
91
+ def port
92
+ settings[:port] || case settings[:protocol]
93
+ when 'http'
94
+ 80
95
+ when 'https'
96
+ 443
97
+ end
98
+ end
99
+
100
+ def request
101
+ HTTP
102
+ end
103
+
104
+ def request_with_auth
105
+ req = request
106
+ if basic_auth
107
+ req = req.basic_auth basic_auth
108
+ elsif ba = @endpoint.basic_auth
109
+ req = req.basic_auth ba
110
+ end
111
+ if authorization
112
+ req = req.auth authorization
113
+ elsif auth = @endpoint.authorization
114
+ req = req.auth auth
115
+ end
116
+ req
117
+ end
118
+
119
+ def required_params_for_path(path)
120
+ path.scan(/:(\w+)/).flatten.map &:to_sym
121
+ end
122
+
123
+ def settings
124
+ @endpoint.settings
125
+ end
126
+
127
+ def start_request(route, route_attributes, *pars)
128
+ unless route_attributes
129
+ route_attributes = {}
130
+ end
131
+ unless method = route_attributes[:method]
132
+ method = Settings::DEFAULT_HTTP_VERB
133
+ end
134
+
135
+ params = pars.extract_options!
136
+ path, residual_params = build_path(route, route_attributes, params)
137
+ residual_params = nil unless residual_params.any?
138
+ uri = build_uri_from path
139
+
140
+ response = build_request.send(method.downcase, uri, json: residual_params)
141
+ check_response_code route, route_attributes, response
142
+
143
+ if block_given?
144
+ body = JSON.load response.body.to_s
145
+ yield body, response.code, response.reason
146
+ else
147
+ response
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,19 @@
1
+ module ApiRecipes
2
+ module Settings
3
+ GLOBAL_TIMEOUT = 1
4
+ FAIL_OPTIONS = [:return, :raise, :return_false]
5
+
6
+ DEFAULT = {
7
+ protocol: 'http',
8
+ host: 'localhost',
9
+ port: nil,
10
+ base_path: '',
11
+ api_version: '',
12
+ timeout: 3,
13
+ on_nok_code: :raise,
14
+ routes: {}
15
+ }
16
+
17
+ DEFAULT_HTTP_VERB = :get
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ApiRecipes
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,90 @@
1
+ require 'http'
2
+ require 'oj'
3
+ require 'oj_mimic_json'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require 'active_support/core_ext/hash/keys'
6
+ require 'active_support/core_ext/array/extract_options'
7
+
8
+ require 'api_recipes/exceptions'
9
+ require 'api_recipes/configuration'
10
+ require 'api_recipes/resource'
11
+ require 'api_recipes/endpoint'
12
+ require 'api_recipes/settings'
13
+
14
+ module ApiRecipes
15
+
16
+ def self.included(base)
17
+ base.send :include, InstanceAndClassMethods
18
+ base.extend ClassMethods
19
+ base.extend InstanceAndClassMethods
20
+ end
21
+
22
+ class << self
23
+ attr_accessor :configuration
24
+ end
25
+
26
+ def self.configuration
27
+ unless @configuration
28
+ @configuration = Configuration.new
29
+ end
30
+ @configuration
31
+ end
32
+
33
+ def self.configure
34
+ if block_given?
35
+ yield(configuration)
36
+ else
37
+ configuration
38
+ end
39
+ end
40
+
41
+ module ClassMethods
42
+ def endpoint(name, configs = {})
43
+ configs = configs.deep_symbolize_keys
44
+ name = name.to_sym
45
+
46
+ ep = Endpoint.new(name, merge_endpoints_configs(name, configs))
47
+ # Define 'name' method for the class
48
+ define_class_endpoint ep
49
+ # Define 'name' method for the class' instances
50
+ define_instance_endpoint ep
51
+ end
52
+
53
+ private
54
+
55
+ def define_class_endpoint(ep)
56
+ name = ep.name
57
+ unless method_defined? name
58
+ Thread.current[name] = ep
59
+
60
+ define_singleton_method name do
61
+ Thread.current[name]
62
+ end
63
+ end
64
+ end
65
+
66
+ def define_instance_endpoint(ep)
67
+ name = ep.name
68
+ send :define_method, name do
69
+ Thread.current[name].clone
70
+ end
71
+ end
72
+ end
73
+
74
+ module InstanceAndClassMethods
75
+
76
+ private
77
+
78
+ def merge_endpoints_configs(endpoint, configs)
79
+ if configs && !configs.is_a?(Hash)
80
+ raise EndpointConfigIsNotAnHash.new(endpoint)
81
+ end
82
+ unless ApiRecipes.configuration.endpoints_configs[endpoint]
83
+ ApiRecipes.configuration.endpoints_configs[endpoint] = {}
84
+ end
85
+ ApiRecipes.configuration.endpoints_configs[endpoint].merge! configs
86
+ ApiRecipes.configuration
87
+ end
88
+ end
89
+ end
90
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_recipes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Verlato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.13.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.13.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj_mimic_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.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.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ description:
70
+ email:
71
+ - averlato@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Guardfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - api_recipes.gemspec
86
+ - bin/console
87
+ - bin/setup
88
+ - examples/authorization.rb
89
+ - examples/authorization_with_default_headers.rb
90
+ - examples/basic_auth.rb
91
+ - examples/config/apis.yml
92
+ - examples/custom_configs.rb
93
+ - examples/delete_me.rb
94
+ - examples/multiple_endpoints.rb
95
+ - examples/simple_usage.rb
96
+ - lib/api_recipes.rb
97
+ - lib/api_recipes/configuration.rb
98
+ - lib/api_recipes/endpoint.rb
99
+ - lib/api_recipes/exceptions.rb
100
+ - lib/api_recipes/resource.rb
101
+ - lib/api_recipes/settings.rb
102
+ - lib/api_recipes/version.rb
103
+ homepage: https://github.com/madAle/api_recipes
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: 1.9.3
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.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Consume HTTP APIs with style
127
+ test_files: []