hapi 0.0.2
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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +48 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/lib/hapi.rb +51 -0
- data/test/helper.rb +10 -0
- data/test/test_hapi.rb +7 -0
- metadata +94 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tim Sharpe
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
HAPI
|
2
|
+
====
|
3
|
+
|
4
|
+
HAPI provides a generic abstraction layer for dealing with simple HTTP APIs
|
5
|
+
that return XML content.
|
6
|
+
|
7
|
+
Installing
|
8
|
+
----------
|
9
|
+
|
10
|
+
gem install hapi
|
11
|
+
|
12
|
+
Using
|
13
|
+
-----
|
14
|
+
|
15
|
+
Making an API call is a simple matter.
|
16
|
+
1. Create the HAPI::APICall object
|
17
|
+
2. Set the parameters you want to send
|
18
|
+
3. Fire the post() or get() methods
|
19
|
+
|
20
|
+
require 'hapi'
|
21
|
+
h = HAPI::APICall.new "https://path.to/my/api"
|
22
|
+
h.params = {'user' => 'rodjek, 'foo' => 'bar'}
|
23
|
+
response = h.post
|
24
|
+
|
25
|
+
The XML content result is parsed by [xml-object][0] and returned at a Ruby
|
26
|
+
object for simplicity.
|
27
|
+
|
28
|
+
i.e.
|
29
|
+
|
30
|
+
<response>
|
31
|
+
<result>
|
32
|
+
<command>foo</command>
|
33
|
+
<code>0</code>
|
34
|
+
</result>
|
35
|
+
</response>
|
36
|
+
|
37
|
+
Can be accessed as follows
|
38
|
+
|
39
|
+
response.result.command
|
40
|
+
=> "foo"
|
41
|
+
response.result.code
|
42
|
+
=> "0"
|
43
|
+
|
44
|
+
Copyright
|
45
|
+
---------
|
46
|
+
Copyright (c) 2010 Tim Sharpe. See LICENSE for details.
|
47
|
+
|
48
|
+
[0]: http://github.com/jordi/xml-object
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "hapi"
|
8
|
+
gem.summary = %Q{Generic HTTP API wrapper for Ruby}
|
9
|
+
gem.description = %Q{Generic HTTP API wrapper for Ruby}
|
10
|
+
gem.email = "tim@sharpe.id.au"
|
11
|
+
gem.homepage = "http://github.com/rodjek/hapi"
|
12
|
+
gem.authors = ["Tim Sharpe"]
|
13
|
+
gem.add_dependency "xml-object", ">= 0"
|
14
|
+
gem.add_dependency "libxml-ruby"
|
15
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/**/test_*.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rcov/rcovtask'
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
task :test => :check_dependencies
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "hapi #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/lib/hapi.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
require 'xml-object'
|
6
|
+
require 'xml-object/adapters/libxml'
|
7
|
+
|
8
|
+
module HAPI
|
9
|
+
class APICall
|
10
|
+
attr_accessor :params
|
11
|
+
def initialize(url)
|
12
|
+
@uri = URI.parse(url)
|
13
|
+
end
|
14
|
+
|
15
|
+
def url
|
16
|
+
"#{@uri.scheme}://#{@uri.host}#{@uri.path}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def url=(url)
|
20
|
+
@uri = URI.parse(url)
|
21
|
+
end
|
22
|
+
|
23
|
+
def post
|
24
|
+
call("post")
|
25
|
+
end
|
26
|
+
|
27
|
+
def get
|
28
|
+
call("get")
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(method)
|
32
|
+
http = Net::HTTP.new(@uri.host, @uri.port)
|
33
|
+
http.use_ssl = (@uri.scheme == 'https')
|
34
|
+
if http.use_ssl
|
35
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
36
|
+
end
|
37
|
+
if method == 'post'
|
38
|
+
request = Net::HTTP::Post.new(@uri.path)
|
39
|
+
request.set_form_data(@params)
|
40
|
+
else
|
41
|
+
params = @params.keys.map { |r|
|
42
|
+
"#{r}=#{@params[r]}"
|
43
|
+
}.join("&")
|
44
|
+
request = Net::HTTP::Get.new("#{@uri.path}?#{params}")
|
45
|
+
end
|
46
|
+
response = http.request(request)
|
47
|
+
response = response.body.gsub(/^\<\?xml.+\?\>/, '')
|
48
|
+
XMLObject.new(response)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_hapi.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Sharpe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-10 00:00:00 +11:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: xml-object
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: libxml-ruby
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: shoulda
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: Generic HTTP API wrapper for Ruby
|
46
|
+
email: tim@sharpe.id.au
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.markdown
|
54
|
+
files:
|
55
|
+
- .document
|
56
|
+
- .gitignore
|
57
|
+
- LICENSE
|
58
|
+
- README.markdown
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- lib/hapi.rb
|
62
|
+
- test/helper.rb
|
63
|
+
- test/test_hapi.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/rodjek/hapi
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options:
|
70
|
+
- --charset=UTF-8
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.5
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Generic HTTP API wrapper for Ruby
|
92
|
+
test_files:
|
93
|
+
- test/test_hapi.rb
|
94
|
+
- test/helper.rb
|