simple_api_client 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/README.md +23 -2
- data/Rakefile +1 -3
- data/config.ru +4 -59
- data/lib/simple_api_client/http_caller/{application_types.rb → constants.rb} +0 -2
- data/lib/simple_api_client/http_caller/net_http.rb +35 -0
- data/lib/simple_api_client/testing_server.rb +58 -0
- data/lib/simple_api_client/version.rb +1 -1
- data/lib/simple_api_client.rb +2 -4
- data/simple_api_client.gemspec +1 -2
- data/spec/http_caller/{curb_spec.rb → net_http_spec.rb} +3 -3
- data/spec/spec_helper.rb +1 -2
- metadata +8 -21
- data/lib/simple_api_client/http_caller/curb.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae96344d08b465b56f54620eb75714d3e61a81e8
|
4
|
+
data.tar.gz: 3a0153a20f2d6ef263f159aa2a1e07e0ce0ed5b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24f3de8ea72890c464e226bcf1135ee44c713d3ca634cca91da41b0a8938c7c56f98db599cc5e5f8978843fbc4e897a55375217ec3f39f200d5c1e05fb0b8c4c
|
7
|
+
data.tar.gz: a2873988e18cbd74008d26b9d85a86210072847996d82919e45f5ddbe066e12683645eb2265a95eb616ed119c71583930c75d5881dd835b27797361f723b99e5
|
data/Gemfile
CHANGED
@@ -3,9 +3,9 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in simple_api_client.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
group :
|
7
|
-
gem 'guard-minitest'
|
8
|
-
gem 'nokogiri'
|
9
|
-
gem 'shotgun'
|
6
|
+
group :test, :development do
|
10
7
|
gem 'sinatra'
|
8
|
+
gem 'shotgun'
|
9
|
+
gem 'nokogiri'
|
10
|
+
gem 'guard-minitest'
|
11
11
|
end
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SimpleApiClient
|
2
2
|
|
3
|
-
|
3
|
+
A module to help create api clients quickly. The idea is you shou ld only have to define your endpoints and not worry about setup and the http client.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,28 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'simple_api_client'
|
25
|
+
|
26
|
+
class YourClientClass
|
27
|
+
|
28
|
+
include SimpleApiClient
|
29
|
+
|
30
|
+
#including SimpleApiClient gives you a base initialize method that accepts two parameters.
|
31
|
+
#param1: excepts a URI object (which is used to set the Scheme, Host, Port etc, of your api.
|
32
|
+
#param2: is an object that responses to a call method which needs to accept a hash of options.
|
33
|
+
|
34
|
+
#Define your own endpoints
|
35
|
+
|
36
|
+
def client_info(client_id, payload)
|
37
|
+
call(method: :get, uri: uri('/client_info')
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
42
|
+
```
|
22
43
|
|
23
44
|
## Contributing
|
24
45
|
|
data/Rakefile
CHANGED
data/config.ru
CHANGED
@@ -1,61 +1,6 @@
|
|
1
|
-
lib = File.expand_path('../lib', __FILE__)
|
2
|
-
|
3
|
-
require 'json'
|
4
|
-
require 'sinatra/base'
|
5
|
-
require 'nokogiri'
|
1
|
+
#lib = File.expand_path('../lib', __FILE__)
|
2
|
+
#$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
3
|
|
7
|
-
|
4
|
+
require 'simple_api_client/testing_server'
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
get '/hello_world' do
|
12
|
-
request.accept.each do |type|
|
13
|
-
case type.to_s
|
14
|
-
when 'application/json'
|
15
|
-
output = {greeting: 'Hello World!'}.to_json
|
16
|
-
when 'application/xml'
|
17
|
-
builder = Nokogiri::XML::Builder.new do |xml|
|
18
|
-
xml.root {
|
19
|
-
xml.greeting {
|
20
|
-
xml << "Hello World!"
|
21
|
-
}
|
22
|
-
}
|
23
|
-
end
|
24
|
-
output = builder.to_xml
|
25
|
-
end
|
26
|
-
halt output
|
27
|
-
end
|
28
|
-
error 406
|
29
|
-
end
|
30
|
-
|
31
|
-
post '/send_data' do
|
32
|
-
put_post_response
|
33
|
-
end
|
34
|
-
|
35
|
-
put '/put' do
|
36
|
-
put_post_response
|
37
|
-
end
|
38
|
-
|
39
|
-
def put_post_response
|
40
|
-
data = request.body.read
|
41
|
-
request.accept.each do |type|
|
42
|
-
case type.to_s
|
43
|
-
when 'application/json'
|
44
|
-
content_type :json
|
45
|
-
#if we get json we can parse it!
|
46
|
-
output = JSON.parse(data).to_json
|
47
|
-
when 'application/xml'
|
48
|
-
content_type :xml
|
49
|
-
#in-valid xml will raise an exception
|
50
|
-
xml = Nokogiri::XML(data){|config| config.strict}
|
51
|
-
puts xml.to_xml
|
52
|
-
output = xml.to_s
|
53
|
-
end
|
54
|
-
halt output
|
55
|
-
end
|
56
|
-
error 406
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
run TestingServer.new
|
6
|
+
run SimpleApiClient::TestingServer.new
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module HttpCaller
|
4
|
+
class NetHttp
|
5
|
+
|
6
|
+
# ==== Exceptions
|
7
|
+
# ::Errno::ECONNREFUSED:: - if the server is unresponsive
|
8
|
+
# ::Errno::ECONNRESET:: - the remote host reset the connection request
|
9
|
+
# ::Errno::ETIMEDOUT:: - timed out
|
10
|
+
def call opts
|
11
|
+
accept = HttpCaller::APPLICATION_TYPES[opts.fetch(:accept, :json)]
|
12
|
+
content_type = HttpCaller::APPLICATION_TYPES[opts.fetch(:content_type, :json)]
|
13
|
+
|
14
|
+
http = Net::HTTP.new(opts[:uri].host, opts[:uri].port)
|
15
|
+
case opts[:method]
|
16
|
+
when :post
|
17
|
+
request = Net::HTTP::Post.new(opts[:uri].request_uri)
|
18
|
+
request.body = opts[:payload]
|
19
|
+
request['Content-Type'] = content_type
|
20
|
+
when :get
|
21
|
+
request = Net::HTTP::Get.new(opts[:uri].request_uri)
|
22
|
+
when :put
|
23
|
+
request = Net::HTTP::Put.new(opts[:uri].request_uri)
|
24
|
+
request.body = opts[:payload]
|
25
|
+
request['Content-type'] = content_type
|
26
|
+
else
|
27
|
+
raise ArgumentError.new("Unknown call method: #{opts[:method]}")
|
28
|
+
end
|
29
|
+
request['Accept'] = accept
|
30
|
+
response = http.request(request)
|
31
|
+
Response.new(response.code.to_i, response.body)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module SimpleApiClient
|
6
|
+
class TestingServer < Sinatra::Base
|
7
|
+
|
8
|
+
set :raise_errors, true
|
9
|
+
|
10
|
+
get '/hello_world' do
|
11
|
+
request.accept.each do |type|
|
12
|
+
case type.to_s
|
13
|
+
when 'application/json'
|
14
|
+
output = {greeting: 'Hello World!'}.to_json
|
15
|
+
when 'application/xml'
|
16
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
17
|
+
xml.root {
|
18
|
+
xml.greeting {
|
19
|
+
xml << "Hello World!"
|
20
|
+
}
|
21
|
+
}
|
22
|
+
end
|
23
|
+
output = builder.to_xml
|
24
|
+
end
|
25
|
+
halt output
|
26
|
+
end
|
27
|
+
error 406
|
28
|
+
end
|
29
|
+
|
30
|
+
post '/send_data' do
|
31
|
+
put_post_response
|
32
|
+
end
|
33
|
+
|
34
|
+
put '/put' do
|
35
|
+
put_post_response
|
36
|
+
end
|
37
|
+
|
38
|
+
def put_post_response
|
39
|
+
data = request.body.read
|
40
|
+
request.accept.each do |type|
|
41
|
+
case type.to_s
|
42
|
+
when 'application/json'
|
43
|
+
content_type :json
|
44
|
+
#if we get json we can parse it!
|
45
|
+
output = JSON.parse(data).to_json
|
46
|
+
when 'application/xml'
|
47
|
+
content_type :xml
|
48
|
+
#in-valid xml will raise an exception
|
49
|
+
xml = Nokogiri::XML(data){|config| config.strict}
|
50
|
+
output = xml.to_s
|
51
|
+
end
|
52
|
+
halt output
|
53
|
+
end
|
54
|
+
error 406
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/lib/simple_api_client.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require 'simple_api_client/version'
|
2
|
-
require 'simple_api_client/http_caller/
|
2
|
+
require 'simple_api_client/http_caller/constants'
|
3
|
+
require 'simple_api_client/http_caller/net_http'
|
3
4
|
require 'simple_api_client/http_caller/response'
|
4
|
-
require 'simple_api_client/http_caller/application_types'
|
5
5
|
require 'active_support/core_ext/module/delegation'
|
6
|
-
require 'json'
|
7
|
-
|
8
6
|
|
9
7
|
module SimpleApiClient
|
10
8
|
|
data/simple_api_client.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["davevallance@gmail.com"]
|
11
11
|
spec.summary = %q{A module to help create api clients quickly.}
|
12
12
|
spec.description = %q{A module to help create api clients quickly. The idea is you should only have to define your endpoints and not worry about setup and the http client.}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/dvallance/simple_api_client"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'activesupport'
|
22
|
-
spec.add_runtime_dependency 'curb'
|
23
22
|
|
24
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
24
|
spec.add_development_dependency "rake"
|
@@ -2,7 +2,7 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
#NOTE: tests require the server to be running. I use the shotgun gem but any rack server should work just run on port 9393 as its hardcoded below. (A sinatra app is created for these tests in config.ru)
|
4
4
|
|
5
|
-
class
|
5
|
+
class NetHttpClient
|
6
6
|
include SimpleApiClient
|
7
7
|
|
8
8
|
# format can be :json or :xml (defaults to :json)
|
@@ -35,9 +35,9 @@ class CurbClient
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
describe HttpCaller::
|
38
|
+
describe HttpCaller::NetHttp do
|
39
39
|
|
40
|
-
subject {
|
40
|
+
subject { NetHttpClient.new('//127.0.0.1:9393/', HttpCaller::NetHttp.new) }
|
41
41
|
|
42
42
|
it '#call - a get request to /hello_world works when requesting json' do
|
43
43
|
result = subject.hello_world(:json)
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Vallance
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: curb
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: bundler
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,15 +68,16 @@ files:
|
|
82
68
|
- Rakefile
|
83
69
|
- config.ru
|
84
70
|
- lib/simple_api_client.rb
|
85
|
-
- lib/simple_api_client/http_caller/
|
86
|
-
- lib/simple_api_client/http_caller/
|
71
|
+
- lib/simple_api_client/http_caller/constants.rb
|
72
|
+
- lib/simple_api_client/http_caller/net_http.rb
|
87
73
|
- lib/simple_api_client/http_caller/response.rb
|
74
|
+
- lib/simple_api_client/testing_server.rb
|
88
75
|
- lib/simple_api_client/version.rb
|
89
76
|
- simple_api_client.gemspec
|
90
|
-
- spec/http_caller/
|
77
|
+
- spec/http_caller/net_http_spec.rb
|
91
78
|
- spec/simple_api_client_spec.rb
|
92
79
|
- spec/spec_helper.rb
|
93
|
-
homepage:
|
80
|
+
homepage: https://github.com/dvallance/simple_api_client
|
94
81
|
licenses:
|
95
82
|
- MIT
|
96
83
|
metadata: {}
|
@@ -115,6 +102,6 @@ signing_key:
|
|
115
102
|
specification_version: 4
|
116
103
|
summary: A module to help create api clients quickly.
|
117
104
|
test_files:
|
118
|
-
- spec/http_caller/
|
105
|
+
- spec/http_caller/net_http_spec.rb
|
119
106
|
- spec/simple_api_client_spec.rb
|
120
107
|
- spec/spec_helper.rb
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'curb'
|
2
|
-
|
3
|
-
module HttpCaller
|
4
|
-
class Curb
|
5
|
-
|
6
|
-
def response http
|
7
|
-
Response.new(http.response_code, http.body_str)
|
8
|
-
end
|
9
|
-
|
10
|
-
def call opts
|
11
|
-
accept = HttpCaller::APPLICATION_TYPES[opts.fetch(:accept, :json)]
|
12
|
-
content_type = HttpCaller::APPLICATION_TYPES[opts.fetch(:content_type, :json)]
|
13
|
-
|
14
|
-
begin
|
15
|
-
case opts[:method]
|
16
|
-
when :post
|
17
|
-
http = Curl.post(opts[:uri].to_s, opts[:payload]) do |http|
|
18
|
-
http.headers['Content-Type'] = content_type
|
19
|
-
http.headers['Accept'] = accept
|
20
|
-
end
|
21
|
-
response(http)
|
22
|
-
when :get
|
23
|
-
http = Curl.get(opts[:uri].to_s) do |http|
|
24
|
-
http.headers['Accept'] = accept
|
25
|
-
end
|
26
|
-
response(http)
|
27
|
-
when :put
|
28
|
-
http = Curl.put(opts[:uri].to_s, opts[:payload]) do |http|
|
29
|
-
http.headers['Content-Type'] = content_type
|
30
|
-
http.headers['Accept'] = accept
|
31
|
-
end
|
32
|
-
response(http)
|
33
|
-
else
|
34
|
-
raise ArgumentError.new("Unknown call method: #{opts[:method]}")
|
35
|
-
end
|
36
|
-
rescue Curl::Err::ConnectionFailedError
|
37
|
-
raise Errno::ECONNREFUSED
|
38
|
-
rescue Curl::Err::TimeoutError
|
39
|
-
raise Errno::ETIMEDOUT
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|