heroix_api 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/heroix_api.gemspec +68 -0
- data/lib/heroix_api.rb +60 -0
- data/lib/heroix_api/data.rb +78 -0
- data/lib/heroix_api/processor.rb +52 -0
- data/test/api_processor_test.rb +93 -0
- data/test/heroix_api_test.rb +18 -0
- data/test/test_helper.rb +18 -0
- metadata +132 -0
data/.document
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.6)
|
5
|
+
crack (0.1.8)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.6.4)
|
8
|
+
bundler (~> 1.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rake
|
11
|
+
json (1.5.3)
|
12
|
+
rake (0.9.2)
|
13
|
+
rcov (0.9.9)
|
14
|
+
webmock (1.6.4)
|
15
|
+
addressable (> 2.2.5, ~> 2.2)
|
16
|
+
crack (>= 0.1.7)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (~> 1.0.0)
|
23
|
+
jeweler (~> 1.6.4)
|
24
|
+
json
|
25
|
+
rcov
|
26
|
+
webmock
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 - 2012 EverydayHero
|
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.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= heroix_api
|
2
|
+
|
3
|
+
== Installation
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
== Notes
|
8
|
+
|
9
|
+
== Contributing to heroix_api
|
10
|
+
|
11
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
12
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
13
|
+
* Fork the project
|
14
|
+
* Start a feature/bugfix branch
|
15
|
+
* Commit and push until you are happy with your contribution
|
16
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
17
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
18
|
+
|
19
|
+
== Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2011, 2012 EverydayHero. See LICENSE.txt for
|
22
|
+
further details.
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "heroix_api"
|
18
|
+
gem.homepage = "http://github.com/stuart/heroix_api"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Heroix API client code}
|
21
|
+
gem.description = %Q{Client code to make requests to the Heroix API. See http://heroix.everydayhero.com.au/doc/api/v1/}
|
22
|
+
gem.email = "stuart.coyle@gmail.com"
|
23
|
+
gem.authors = ["Stuart Coyle"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/*_test.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "heroix_api #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/heroix_api.gemspec
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{heroix_api}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stuart Coyle"]
|
12
|
+
s.date = %q{2012-02-29}
|
13
|
+
s.description = %q{Client code to make requests to the Heroix API. See http://heroix.everydayhero.com.au/doc/api/v1/}
|
14
|
+
s.email = %q{stuart.coyle@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"heroix_api.gemspec",
|
28
|
+
"lib/heroix_api.rb",
|
29
|
+
"lib/heroix_api/data.rb",
|
30
|
+
"lib/heroix_api/processor.rb",
|
31
|
+
"test/api_processor_test.rb",
|
32
|
+
"test/heroix_api_test.rb",
|
33
|
+
"test/test_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/stuart/heroix_api}
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.6.2}
|
39
|
+
s.summary = %q{Heroix API client code}
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<webmock>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<webmock>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<json>, [">= 0"])
|
53
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
56
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
57
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<json>, [">= 0"])
|
61
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/lib/heroix_api.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'heroix_api/processor.rb'
|
2
|
+
require 'heroix_api/data.rb'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module HeroixApi
|
6
|
+
class << self
|
7
|
+
attr_accessor :root_url, :api_key, :login, :timeout, :ssl
|
8
|
+
|
9
|
+
##
|
10
|
+
# Configure Heroix Api.
|
11
|
+
#
|
12
|
+
# HeroixApi.configure do |config|
|
13
|
+
# config.api_key = "AAAAAABBBBBCCCCC1111122222"
|
14
|
+
# config.login = "user_name"
|
15
|
+
# config.root_url = "https://heroix.everydayhero.com.au/api/v1/"
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Configurable settings
|
19
|
+
#
|
20
|
+
# [api_key = String] Your Heroix API key
|
21
|
+
# [login = String] Your Heroix API login
|
22
|
+
# [root_url = String] The url to the Heroix api host. Defaults to the current production API host.
|
23
|
+
# [timeout = Integer] The time in seconds to wait for the server to respond before raising a timeout error.
|
24
|
+
#
|
25
|
+
def configure
|
26
|
+
yield self
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Log errors
|
31
|
+
#
|
32
|
+
# Override this method to provide your own error logging
|
33
|
+
# By default just prints errors to STDERR
|
34
|
+
def log_error error
|
35
|
+
printf $stderr, error
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Make an API call to get a resource from the server
|
40
|
+
#
|
41
|
+
# [path] The resource to get.
|
42
|
+
# [query_string] The query string to pass with the request.
|
43
|
+
#
|
44
|
+
# Example calls:
|
45
|
+
# @campaign = HeroixApi.get_resource("campaigns/2.json")
|
46
|
+
# @hero_pages = HeroixApi.get_resource('hero_pages.json', {active => true})
|
47
|
+
#
|
48
|
+
# Returns a HeroixApi::Data object
|
49
|
+
# The data is accessible via the data member on this object.
|
50
|
+
# It also provides meta_data for the object, primarily the pagination links.
|
51
|
+
#
|
52
|
+
def get_resource(path, params = {})
|
53
|
+
query = params.map do |key, value|
|
54
|
+
"#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
|
55
|
+
end.join '&'
|
56
|
+
query = "?#{query}" unless query.blank?
|
57
|
+
api_data = HeroixApi::Processor.get_api_response("#{path}#{query}")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'heroix_api'
|
2
|
+
require 'json'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
# HeroixApi::Data
|
6
|
+
#
|
7
|
+
# Encapsulates data from the HeroixApi in an easy to use form.
|
8
|
+
# This does not care about the data type too much and just returns it
|
9
|
+
# as an open struct. TODO: less abstracted data types.
|
10
|
+
module HeroixApi
|
11
|
+
class Data
|
12
|
+
|
13
|
+
##
|
14
|
+
# Contains the data as an OpenStruct
|
15
|
+
attr_accessor :data
|
16
|
+
|
17
|
+
##
|
18
|
+
# Contains the response meta data as an OpenStruct
|
19
|
+
attr_accessor :meta_data
|
20
|
+
|
21
|
+
##
|
22
|
+
# Constructor for HeroixApi::Data
|
23
|
+
def initialize(data,meta_data)
|
24
|
+
@data = data
|
25
|
+
@meta_data = meta_data
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Creates an HeroixApi::Data object from an Http::Response
|
30
|
+
def self.from_response(response)
|
31
|
+
data = self.parse_data(response)
|
32
|
+
meta_data = {:page_numbers => self.parse_link_headers(response)}
|
33
|
+
self.new(data, meta_data)
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Gives the page numbers for a paginated resource
|
38
|
+
# This is an OpenStruct with the following members:
|
39
|
+
# [page_numbers.next] Number of the next page
|
40
|
+
# [page_numbers.prev] Number of the previous page
|
41
|
+
# [page_numbers.current] Number of the current page
|
42
|
+
# [page_numbers.first] Number of the first page of data
|
43
|
+
# [page_numbers.last] Number of the last page
|
44
|
+
#
|
45
|
+
def page_numbers
|
46
|
+
meta_data[:page_numbers]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.parse_data(response)
|
52
|
+
resources = []
|
53
|
+
if response
|
54
|
+
resources_data = JSON response.body
|
55
|
+
if resources_data.is_a? Array
|
56
|
+
resources = resources_data.map{|resource_data| OpenStruct.new(resource_data)}
|
57
|
+
else
|
58
|
+
resources = OpenStruct.new(resources_data)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
resources = []
|
62
|
+
end
|
63
|
+
resources
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.parse_link_headers(response)
|
67
|
+
page_numbers = {}
|
68
|
+
return nil unless response && response["Link"]
|
69
|
+
response["Link"].split(",").each do |link|
|
70
|
+
match = /<(.*)>;\s*rel="(.*)"/.match(link)
|
71
|
+
page_number = CGI.parse(URI.parse(match[1]).query)["page"][0]
|
72
|
+
page_numbers[match[2].to_sym] = page_number
|
73
|
+
end
|
74
|
+
page_numbers[:current] = (page_numbers[:previous].to_i + 1).to_s
|
75
|
+
OpenStruct.new(page_numbers)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'heroix_api'
|
2
|
+
|
3
|
+
module HeroixApi
|
4
|
+
class Processor
|
5
|
+
require "net/http"
|
6
|
+
|
7
|
+
##
|
8
|
+
# Get an api response catching errors and logging them.
|
9
|
+
def self.get_api_response(query)
|
10
|
+
begin
|
11
|
+
response = get_api_response!(query)
|
12
|
+
rescue => e
|
13
|
+
HeroixApi.log_error "Heroix API get_api_response with query #{HeroixApi.root_url}#{query} error: #{e.inspect}"
|
14
|
+
end
|
15
|
+
|
16
|
+
return HeroixApi::Data.from_response(response)
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Get an api response allowing errors to be raised.
|
21
|
+
def self.get_api_response!(query)
|
22
|
+
query_url = "#{HeroixApi.root_url}#{query}"
|
23
|
+
response = self.request_api_url(query_url)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def self.request_api_url(full_url, redirect_count = 0)
|
29
|
+
uri = URI.parse(full_url)
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
31
|
+
http.read_timeout = HeroixApi.timeout || 3
|
32
|
+
http.use_ssl = HeroixApi.ssl || false
|
33
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
34
|
+
request.basic_auth(HeroixApi.login, HeroixApi.api_key)
|
35
|
+
res = http.start {|http| http.request(request) }
|
36
|
+
|
37
|
+
case res
|
38
|
+
when Net::HTTPSuccess
|
39
|
+
when Net::HTTPRedirection
|
40
|
+
if redirect_count < 5
|
41
|
+
res = request_api_url(res.response['Location'], redirect_count += 1)
|
42
|
+
else
|
43
|
+
raise "Too many redirects"
|
44
|
+
end
|
45
|
+
else
|
46
|
+
res.error!
|
47
|
+
end
|
48
|
+
|
49
|
+
return res
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
require 'heroix_api/processor'
|
3
|
+
|
4
|
+
class HeroixApi::ProcessorTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
HeroixApi.configure do |config|
|
8
|
+
config.api_key = "77836efba21877ccd70b36febbfad62a68d898b8"
|
9
|
+
config.login = "edhadmin"
|
10
|
+
config.root_url = "http://demo.everydayhero.com.au:5000/api/v1/"
|
11
|
+
config.ssl = false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_processor_returns_a_valid_response_when_successful
|
16
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
17
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
18
|
+
to_return(:status => 200, :body => '[{"name":"test event"}]', :headers => {})
|
19
|
+
|
20
|
+
@response = HeroixApi::Processor.get_api_response('test.json')
|
21
|
+
assert_equal "test event", @response.data[0].name
|
22
|
+
assert_equal nil, @response.page_numbers
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_processor_follows_redirects
|
26
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
27
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
28
|
+
to_return(:status => 302, :body => "", :headers => {:location => "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test2.json"} )
|
29
|
+
|
30
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test2.json").
|
31
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
32
|
+
to_return(:status => 200, :body => '{"test":"value"}', :headers => {})
|
33
|
+
|
34
|
+
@response = HeroixApi::Processor.get_api_response('test.json')
|
35
|
+
assert_equal "value", @response.data.test
|
36
|
+
assert_equal nil, @response.page_numbers
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_processor_raises_error_on_redirect_loop
|
40
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
41
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
42
|
+
to_return(:status => 302, :body => "", :headers => {:location => "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json"} )
|
43
|
+
assert_raises RuntimeError, "Too many redirects" do
|
44
|
+
@response = HeroixApi::Processor.get_api_response!('test.json')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_raises_error_on_404
|
49
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
50
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
51
|
+
to_return(:status => 404, :body => "{Not Found:404}", :headers => {} )
|
52
|
+
assert_raises Net::HTTPServerException do
|
53
|
+
@response = HeroixApi::Processor.get_api_response!('test.json')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_raises_an_error_when_not_authorised
|
58
|
+
HeroixApi.api_key = ""
|
59
|
+
stub_request(:get, "http://edhadmin:@demo.everydayhero.com.au:5000/api/v1/test.json").
|
60
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
61
|
+
to_return(:status => 401, :body => "{Not Authorised:403}", :headers => {} )
|
62
|
+
assert_raises Net::HTTPServerException do
|
63
|
+
@response = HeroixApi::Processor.get_api_response!('test.json')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_raises_error_on_timeout
|
68
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
69
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_timeout
|
70
|
+
assert_raises Timeout::Error do
|
71
|
+
@response = HeroixApi::Processor.get_api_response!('test.json')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_sets_pagination_links
|
76
|
+
link = <<-EOS
|
77
|
+
<http://localhost:3000/api/v1/charities.json?page=2>; rel="previous",
|
78
|
+
<http://localhost:3000/api/v1/charities.json?page=4>; rel="next",
|
79
|
+
<http://localhost:3000/api/v1/charities.json?page=1>; rel="first",
|
80
|
+
<http://localhost:3000/api/v1/charities.json?page=10>; rel="last"
|
81
|
+
EOS
|
82
|
+
|
83
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/test.json").
|
84
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
85
|
+
to_return(:status => 200, :body => '{"test":"value"}', :headers => {"Link" => link.split(',')})
|
86
|
+
@response = HeroixApi::Processor.get_api_response('test.json')
|
87
|
+
assert_equal "2", @response.page_numbers.previous
|
88
|
+
assert_equal "4", @response.page_numbers.next
|
89
|
+
assert_equal "1", @response.page_numbers.first
|
90
|
+
assert_equal "10", @response.page_numbers.last
|
91
|
+
assert_equal "3", @response.page_numbers.current
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class HeroixApiTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_get_resource_hits_the_correct_url
|
6
|
+
|
7
|
+
stub_request(:get, "http://edhadmin:77836efba21877ccd70b36febbfad62a68d898b8@demo.everydayhero.com.au:5000/api/v1/campaign?id=123").
|
8
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
9
|
+
to_return(:status => 200, :body => '{"test":"campaign"}', :headers => {})
|
10
|
+
response = HeroixApi.get_resource('campaign', {:id => 123})
|
11
|
+
|
12
|
+
assert_equal 'campaign', response.data.test
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_logging_errors
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'webmock/test_unit'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'heroix_api'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heroix_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stuart Coyle
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-29 00:00:00.000000000 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
requirement: &2165709640 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2165709640
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: webmock
|
28
|
+
requirement: &2165709040 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2165709040
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &2165708400 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2165708400
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &2165707540 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.6.4
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2165707540
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rcov
|
61
|
+
requirement: &2165706660 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *2165706660
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: webmock
|
72
|
+
requirement: &2165705720 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *2165705720
|
81
|
+
description: Client code to make requests to the Heroix API. See http://heroix.everydayhero.com.au/doc/api/v1/
|
82
|
+
email: stuart.coyle@gmail.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files:
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.rdoc
|
88
|
+
files:
|
89
|
+
- .document
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.rdoc
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- heroix_api.gemspec
|
97
|
+
- lib/heroix_api.rb
|
98
|
+
- lib/heroix_api/data.rb
|
99
|
+
- lib/heroix_api/processor.rb
|
100
|
+
- test/api_processor_test.rb
|
101
|
+
- test/heroix_api_test.rb
|
102
|
+
- test/test_helper.rb
|
103
|
+
has_rdoc: true
|
104
|
+
homepage: http://github.com/stuart/heroix_api
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
hash: -462328465431417884
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 1.6.2
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Heroix API client code
|
132
|
+
test_files: []
|