restclient_api_base 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50ace3660f3c408e006ee031b8710fb48b6648f5
4
+ data.tar.gz: ead68b84e3ac78f26eef168e1e5cabf796e676ef
5
+ SHA512:
6
+ metadata.gz: a8cc9beed56ad8eb1f1a790aae2d9b00b086842aafb38fa82ed8041b5210df6dec119b5ff5c520b99cb95c02765592da4a766e27e9ccc129c7324bf52038026b
7
+ data.tar.gz: d01de8f14e9f0d9535fefebd7c075e7fb8ce900990084644aed3b10937c2f8dc09ded01b0f9d86e38969c76bb239b3a028176f8c3e6a63c3701c5e88e66af416
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restclient_api_base.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Spirit
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,48 @@
1
+ # RestclientApiBase
2
+
3
+ 以 REST 方式调用外部 API 的基础类, 默认 Content-Type 为 JSON
4
+
5
+ ## Dependency
6
+
7
+ [active_support/concern](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/concern.rb)
8
+
9
+ [rest-client](https://github.com/rest-client/rest-client)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'restclient_api_base'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install restclient_api_base
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'restclient_api_base'
29
+
30
+ module A
31
+ include RestclientApiBase
32
+
33
+ self.base_url = 'http://www.example.com' # define app base url
34
+ self.private_params = { key: 'api_key' } # define app secret key
35
+ end
36
+
37
+ A.get('/api/xxx', key_1: value_1, key_2: value_2) # => { response: json_response }
38
+ A.post('/api/xxx', key_1: value_1, key_2: value_2)
39
+ A.patch('/api/xxx', key_1: value_1, key_2: value_2)
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/Naixspirit/restclient_api_base/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module RestclientApiBase
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,76 @@
1
+ require "restclient_api_base/version"
2
+ require "json"
3
+ require "rest-client"
4
+ require "active_support/concern"
5
+
6
+ module RestclientApiBase
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+
11
+ # base_url: the api base url, like: http://api.example.com, default is ''
12
+ # content_type: dafault is { content_type: :json, accept: :json }
13
+ # private_params: your api secret, like: { app_id: 'xxxx' }, default is {}
14
+
15
+ attr_accessor :base_url, :content_type, :private_params
16
+
17
+ def get api_url, params = {}
18
+ res = RestClient.get(
19
+ base_url + api_url,
20
+ params: private_params.merge(params)
21
+ )
22
+ end
23
+
24
+ def post api_url, query = {}, headers = {}
25
+ res = RestClient.post(
26
+ base_url + api_url,
27
+ JSON.generate(private_params.merge(query)),
28
+ content_type.merge(headers)
29
+ )
30
+ end
31
+
32
+ def patch api_url, query = {}
33
+ res = RestClient.patch(
34
+ base_url + api_url,
35
+ JSON.generate(private_params.merge(query)),
36
+ content_type.merge(headers)
37
+ )
38
+ end
39
+
40
+ def put api_url, query = {}
41
+ res = RestClient.put(
42
+ base_url + api_url,
43
+ JSON.generate(private_params.merge(query)),
44
+ content_type.merge(headers)
45
+ )
46
+ end
47
+
48
+ def delete api_url, params = {}
49
+ res = RestClient.delete(
50
+ base_url + api_url,
51
+ params: private_params.merge(params),
52
+ )
53
+ end
54
+
55
+ def base_url
56
+ @base_url ||= ''
57
+ end
58
+
59
+ def content_type
60
+ @content_type ||= { content_type: :json, accept: :json }
61
+ end
62
+
63
+ def content_type= type
64
+ @content_type = { content_type: type, accept: type }
65
+ end
66
+
67
+ def private_params
68
+ @private_params ||= {}
69
+ end
70
+
71
+ def private_params= params
72
+ raise 'the private params must be a hash' unless params.is_a? Hash
73
+ @private_params = params
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restclient_api_base/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "restclient_api_base"
8
+ spec.version = RestclientApiBase::VERSION
9
+ spec.authors = ["Spirit"]
10
+ spec.email = ["neverlandxy.naix@gmail.com"]
11
+ spec.summary = %q{Base restclient api center}
12
+ spec.description = %q{Base restclient api center}
13
+ spec.homepage = "https://github.com/NaixSpirit/restclient_api_base"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client", "~> 1.7"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'minitest/autorun'
2
+ File.dirname(__FILE__) + '/../lib/restclient_api_base'
3
+
4
+ if Minitest.const_defined?('Test')
5
+ # We're on Minitest 5+. Nothing to do here.
6
+ else
7
+ # Minitest 4 doesn't have Minitest::Test yet.
8
+ Minitest::Test = MiniTest::Unit::TestCase
9
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestRestclientApiBase < Minitest::Test
4
+
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ module Client
12
+
13
+ end
14
+
15
+ def test_aa
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restclient_api_base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Spirit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Base restclient api center
56
+ email:
57
+ - neverlandxy.naix@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/restclient_api_base.rb
68
+ - lib/restclient_api_base/version.rb
69
+ - restclinet_api_base.gemspec
70
+ - test/test_helper.rb
71
+ - test/test_restclient_api_base.rb
72
+ homepage: https://github.com/NaixSpirit/restclient_api_base
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Base restclient api center
96
+ test_files:
97
+ - test/test_helper.rb
98
+ - test/test_restclient_api_base.rb
99
+ has_rdoc: