basic_client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a54f7d7aa446060fc229685947bf8a91a828f131
4
+ data.tar.gz: 952589a865d90d6f7fdbf4992a85ad05b4190cd0
5
+ SHA512:
6
+ metadata.gz: bf64642aaff39bfa8b01065f8ea01b18fedb77f94f09d011352a15c144647c3265194f3e7c907c3ffc57f236a03d43c516550635757aed2ead31d3d569196540
7
+ data.tar.gz: 6bcbe7e3dea742dcfbcc45b125c77879b0a9d44783e49f88730207ea3594b5f438e9698cc0f398b29754d6749c64cd0a4b2b7fb7fb03b054b00c616cfae1606a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in basic_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jason Kriss
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,54 @@
1
+ # BasicClient
2
+
3
+ Helper library for writing faraday-based API wrappers.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'basic_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install basic_client
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class StripeClient < BasicClient::Base
25
+ with_base 'https://api.stripe.com'
26
+
27
+ def initialize(api_key)
28
+ @api_key = api_key
29
+ end
30
+
31
+ def charges
32
+ get('charges')
33
+ end
34
+
35
+ private
36
+ attr_reader :api_key
37
+
38
+ def path_prefix
39
+ 'v1'
40
+ end
41
+
42
+ def set_request_middleware(connection)
43
+ connection.request :basic_auth, api_key, ''
44
+ end
45
+ end
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/jasonkriss/basic_client/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'basic_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "basic_client"
8
+ spec.version = BasicClient::VERSION
9
+ spec.authors = ["Jason Kriss"]
10
+ spec.email = ["jasonkriss@gmail.com"]
11
+ spec.summary = %q{Helper library for writing faraday-based API wrappers.}
12
+ spec.homepage = "https://github.com/jasonkriss/basic_client"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'faraday', '~> 0.8.9'
21
+ spec.add_dependency 'faraday_middleware', '~> 0.9.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'basic_client/version'
2
+ require 'basic_client/base'
3
+ require 'basic_client/errors'
4
+ require 'basic_client/middleware/symbolized_json'
5
+ require 'basic_client/middleware/raise_error'
6
+
7
+ module BasicClient
8
+
9
+ end
@@ -0,0 +1,60 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module BasicClient
5
+ class Base
6
+ class << self
7
+ attr_accessor :base_url
8
+
9
+ def with_base(url)
10
+ @base_url = url
11
+ end
12
+ end
13
+
14
+ def get(path, params = {}, headers = {}, &block)
15
+ request(:get, path, params, headers, &block)
16
+ end
17
+
18
+ def post(path, params = {}, headers = {}, &block)
19
+ request(:post, path, params, headers, &block)
20
+ end
21
+
22
+ def put(path, params = {}, headers = {}, &block)
23
+ request(:put, path, params, headers, &block)
24
+ end
25
+
26
+ def delete(path, params = {}, headers = {}, &block)
27
+ request(:delete, path, params, headers, &block)
28
+ end
29
+
30
+ private
31
+ def request(method, path, params, headers, &block)
32
+ response = connection.send(method, path, params, headers, &block)
33
+ response.body
34
+ end
35
+
36
+ def connection
37
+ @connection ||= build_connection
38
+ end
39
+
40
+ def build_connection
41
+ Faraday.new(self.class.base_url) do |connection|
42
+ connection.path_prefix = path_prefix if path_prefix
43
+ set_request_middleware(connection)
44
+ set_response_middleware(connection)
45
+ connection.adapter :net_http
46
+ end
47
+ end
48
+
49
+ def path_prefix; end
50
+
51
+ def set_request_middleware(connection)
52
+ connection.request :json
53
+ end
54
+
55
+ def set_response_middleware(connection)
56
+ connection.response :symbolized_json
57
+ connection.response :raise_error
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ module BasicClient
2
+ class Error < StandardError
3
+ attr_reader :status
4
+
5
+ def initialize(status, message)
6
+ @status = status
7
+ super(message)
8
+ end
9
+ end
10
+
11
+ ServerError = Class.new(Error)
12
+ ClientError = Class.new(Error)
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+
3
+ module BasicClient
4
+ module Middleware
5
+ class RaiseError < Faraday::Response::Middleware
6
+ def on_complete(env)
7
+ status = env[:status].to_i
8
+
9
+ error_class = if status.between?(400, 499)
10
+ BasicClient::ClientError
11
+ elsif status.between?(500, 599)
12
+ BasicClient::ServerError
13
+ end
14
+
15
+ raise error_class.new(status, env[:body]) if error_class
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Faraday::Response.register_middleware :raise_error => BasicClient::Middleware::RaiseError
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+
3
+ module BasicClient
4
+ module Middleware
5
+ class SymbolizedJson < Faraday::Response::Middleware
6
+ UNPARSABLE = [204, 301, 302, 304]
7
+
8
+ def parse(body)
9
+ case body
10
+ when /\A^\s*$\z/, nil
11
+ nil
12
+ else
13
+ try_parse(body)
14
+ end
15
+ end
16
+
17
+ def on_complete(env)
18
+ env[:body] = parse(env[:body]) unless UNPARSABLE.include?(env[:status])
19
+ end
20
+
21
+ def try_parse(body)
22
+ JSON.parse(body, symbolize_names: true)
23
+ rescue JSON::ParserError
24
+ body
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Faraday::Response.register_middleware :symbolized_json => BasicClient::Middleware::SymbolizedJson
@@ -0,0 +1,3 @@
1
+ module BasicClient
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kriss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - jasonkriss@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - basic_client.gemspec
82
+ - lib/basic_client.rb
83
+ - lib/basic_client/base.rb
84
+ - lib/basic_client/errors.rb
85
+ - lib/basic_client/middleware/raise_error.rb
86
+ - lib/basic_client/middleware/symbolized_json.rb
87
+ - lib/basic_client/version.rb
88
+ homepage: https://github.com/jasonkriss/basic_client
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Helper library for writing faraday-based API wrappers.
112
+ test_files: []