simple_api_client 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 31a920e8c543ee107138c812571ace77aa3a631f
4
+ data.tar.gz: 7da0b4e707fb83026cfa7891d8182a9f7d28c368
5
+ SHA512:
6
+ metadata.gz: 75f5d97afe7bffd0aa05caf6acd4619300f2b494e58cdfbd7c8022a03cc88dd008b0de8a7d496dcb0bd185c41130714b03b09f4222a5554609e936c0147a4a53
7
+ data.tar.gz: 6ff56c241ce06e7f6f573bf260722cca2521d74ad5997cb9d8a51b54f84dedb8936869a1fde06583e9a989d8ae18d541d3102393b03ddb5ac8751f1cb25cd1a0
@@ -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,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_api_client.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard-minitest'
8
+ gem 'nokogiri'
9
+ gem 'shotgun'
10
+ gem 'sinatra'
11
+ end
@@ -0,0 +1,6 @@
1
+ guard :minitest do
2
+ watch(%r{^spec/(.*)_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
5
+ watch(%r{^lib/*}) {'spec'}
6
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dave Vallance
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.
@@ -0,0 +1,29 @@
1
+ # SimpleApiClient
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_api_client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_api_client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/simple_api_client/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "spec/*_spec.rb"
6
+ end
7
+
8
+
@@ -0,0 +1,61 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'json'
4
+ require 'sinatra/base'
5
+ require 'nokogiri'
6
+
7
+ class TestingServer < Sinatra::Base
8
+
9
+ set :raise_errors, true
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
@@ -0,0 +1,33 @@
1
+ require 'simple_api_client/version'
2
+ require 'simple_api_client/http_caller/curb'
3
+ require 'simple_api_client/http_caller/response'
4
+ require 'simple_api_client/http_caller/application_types'
5
+ require 'active_support/core_ext/module/delegation'
6
+ require 'json'
7
+
8
+
9
+ module SimpleApiClient
10
+
11
+ ARGUMENT_ERROR_MESSAGE_HOST_MISSING = 'First argument must be a URI object or a URI string that provides a host. (i.e //myhost or http://myhost). Note: Scheme defaults to http if not provided.'
12
+ ARGUMENT_ERROR_MESSAGE_RESPOND_TO_CALL_NEEDED = 'Second argument must be a class that response_to?(:call).'
13
+
14
+ delegate :host, :port, to: :@uri
15
+ delegate :call, to: :@caller
16
+ attr_reader :caller
17
+
18
+ def initialize uri, api_caller
19
+ @uri = URI(uri)
20
+ raise ArgumentError.new(ARGUMENT_ERROR_MESSAGE_HOST_MISSING) if @uri.host.nil?
21
+ raise ArgumentError.new(ARGUMENT_ERROR_MESSAGE_RESPOND_TO_CALL_NEEDED) unless api_caller.respond_to?(:call)
22
+ @caller = api_caller
23
+ end
24
+
25
+ def scheme
26
+ @uri.scheme || 'http'
27
+ end
28
+
29
+ def uri relative_path
30
+ URI("#{scheme}://#{host}:#{port}#{relative_path}")
31
+ end
32
+
33
+ end
@@ -0,0 +1,8 @@
1
+ module HttpCaller
2
+
3
+ APPLICATION_TYPES = {
4
+ json: 'application/json',
5
+ xml: 'application/xml'
6
+ }
7
+
8
+ end
@@ -0,0 +1,44 @@
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
@@ -0,0 +1,4 @@
1
+ module HttpCaller
2
+ Response = Struct.new(:code, :body)
3
+ end
4
+
@@ -0,0 +1,3 @@
1
+ module SimpleApiClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_api_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_api_client"
8
+ spec.version = SimpleApiClient::VERSION
9
+ spec.authors = ["Dave Vallance"]
10
+ spec.email = ["davevallance@gmail.com"]
11
+ spec.summary = %q{A module to help create api clients quickly.}
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 = ""
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_runtime_dependency 'activesupport'
22
+ spec.add_runtime_dependency 'curb'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,91 @@
1
+ require_relative '../spec_helper'
2
+
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
+
5
+ class CurbClient
6
+ include SimpleApiClient
7
+
8
+ # format can be :json or :xml (defaults to :json)
9
+ def hello_world format
10
+ call(
11
+ method: :get,
12
+ uri: uri('/hello_world'),
13
+ accept: format
14
+ )
15
+ end
16
+
17
+ # format can be :json or :xml (defaults to :json)
18
+ def send_data payload, format
19
+ call(
20
+ method: :post,
21
+ uri: uri('/send_data'),
22
+ payload: payload,
23
+ accept: format
24
+ )
25
+ end
26
+
27
+ # format can be :json or :xml (defaults to :json)
28
+ def put payload, format
29
+ call(
30
+ method: :put,
31
+ uri: uri('/put'),
32
+ payload: payload,
33
+ accept: format
34
+ )
35
+ end
36
+ end
37
+
38
+ describe HttpCaller::Curb do
39
+
40
+ subject { CurbClient.new('//127.0.0.1:9393/', HttpCaller::Curb.new) }
41
+
42
+ it '#call - a get request to /hello_world works when requesting json' do
43
+ result = subject.hello_world(:json)
44
+ JSON.parse(result.body)['greeting'].must_equal 'Hello World!'
45
+ end
46
+
47
+ it '#call - a get request to /hello_world works when requesting xml' do
48
+ result = subject.hello_world :xml
49
+ xml = Nokogiri::XML(result.body)
50
+ xml.at_xpath('//greeting').content.must_equal 'Hello World!'
51
+ end
52
+
53
+ it '#call - a post request to /send_data works when sending json' do
54
+ put_post_json_test
55
+ end
56
+
57
+ it '#call - a post request to /send_data works when sending xml' do
58
+ put_post_xml_test
59
+ end
60
+
61
+ it '#call - a put request to /put works when sending json' do
62
+ put_post_json_test
63
+ end
64
+
65
+ it '#call - a put request to /put works when sending xml' do
66
+ put_post_xml_test
67
+ end
68
+
69
+ private
70
+
71
+ def put_post_json_test
72
+ data = {"my_data" => "Is very simple"}
73
+ result = subject.put data.to_json, :json
74
+ assert JSON.parse(result.body).must_equal data
75
+ end
76
+
77
+ def put_post_xml_test
78
+ builder = Nokogiri::XML::Builder.new do |xml|
79
+ xml.root {
80
+ xml.data {
81
+ xml << "My data!"
82
+ }
83
+ }
84
+ end
85
+
86
+ result = subject.send_data builder.to_xml, :xml
87
+ xml = Nokogiri::XML(result.body)
88
+ xml.at_xpath('//data').content.must_equal 'My data!'
89
+ end
90
+
91
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'spec_helper'
2
+
3
+ class TestClient
4
+ include SimpleApiClient
5
+ end
6
+
7
+ class TestCaller
8
+ def call(opts)
9
+ end
10
+ end
11
+
12
+ describe TestClient do
13
+
14
+ it '#initialize expects a valid uri string' do
15
+ err = proc {
16
+ TestClient.new(nil, nil)
17
+ }.must_raise ArgumentError
18
+
19
+ assert err.to_s =~ /expected URI/, 'wrong Argument error'
20
+ end
21
+
22
+ it '#initialize expects a valid uri string containg a host' do
23
+ err = proc {
24
+ TestClient.new('not_valid', nil)
25
+ }.must_raise ArgumentError
26
+
27
+ err.to_s.must_equal TestClient::ARGUMENT_ERROR_MESSAGE_HOST_MISSING
28
+ end
29
+
30
+ it '#initialize expects a http_client that response to :call' do
31
+ err = proc {
32
+ TestClient.new('//www.test.com', nil)
33
+ }.must_raise ArgumentError
34
+
35
+ err.to_s.must_equal TestClient::ARGUMENT_ERROR_MESSAGE_RESPOND_TO_CALL_NEEDED
36
+ end
37
+
38
+ it "#initialize works with valid parameters" do
39
+ client = TestClient.new("http://127.0.0.1:9393", TestCaller.new)
40
+ client.scheme.must_equal 'http'
41
+ client.host.must_equal '127.0.0.1'
42
+ client.port.must_equal 9393
43
+ end
44
+
45
+ it "#initialize provides a default port of 80 if not supplied in uri" do
46
+ client = TestClient.new("http://127.0.0.1", TestCaller.new)
47
+ client.port.must_equal 80
48
+ end
49
+
50
+ it "#initialize provides a default scheme of http if not provided in uri" do
51
+ client = TestClient.new("//127.0.0.1", TestCaller.new)
52
+ client.scheme.must_equal 'http'
53
+ end
54
+
55
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../lib/simple_api_client'
2
+ require 'minitest/autorun'
3
+ require 'minitest/spec'
4
+ require 'minitest/benchmark'
5
+ require 'nokogiri'
6
+ #require 'mocha/mini_test'
7
+ #require 'debugger'
8
+
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Vallance
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A module to help create api clients quickly. The idea is you should only
70
+ have to define your endpoints and not worry about setup and the http client.
71
+ email:
72
+ - davevallance@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - config.ru
84
+ - lib/simple_api_client.rb
85
+ - lib/simple_api_client/http_caller/application_types.rb
86
+ - lib/simple_api_client/http_caller/curb.rb
87
+ - lib/simple_api_client/http_caller/response.rb
88
+ - lib/simple_api_client/version.rb
89
+ - simple_api_client.gemspec
90
+ - spec/http_caller/curb_spec.rb
91
+ - spec/simple_api_client_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: ''
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: A module to help create api clients quickly.
117
+ test_files:
118
+ - spec/http_caller/curb_spec.rb
119
+ - spec/simple_api_client_spec.rb
120
+ - spec/spec_helper.rb