jamesotron-digitalnz 0.1.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,36 @@
1
+ = DigitalNZ
2
+
3
+ == Description
4
+
5
+ A small gem to interface with the DigitalNZ API at http://www.digitalnz.org/
6
+
7
+ == Installation
8
+
9
+ === Stable
10
+
11
+ sudo gem install digitalnz
12
+
13
+ === Bleeding Edge
14
+
15
+ $ git clone git://github.com/jamesotron/digitalnz.git
16
+ $ cd digitalnz
17
+ $ rake gem && sudo gem install pkg/digitalnz-<version>.gem
18
+
19
+ == Usage
20
+
21
+ To get started you'll need to grab an API key from DigialNZ, you can get this here: http://www.digitalnz.org/dashboard/api_key
22
+
23
+ Now that you've got your API key you can try using it:
24
+
25
+ $ irb -r rubygems
26
+ >> require 'digitalnz'
27
+
28
+ Next, set your API key (you only need to do this once per session):
29
+
30
+ >> DigitalNZ.api_key = '<your api key here>'
31
+
32
+ == License
33
+
34
+ Copyright (c) 2009 James Harton (jamesotron@gmail.com)
35
+
36
+ Please see the LICENSE file with this distribution.
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/digitalnz/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'digitalnz'
11
+ s.version = DigitalNZ::Version.to_s
12
+ s.has_rdoc = true
13
+ s.extra_rdoc_files = %w(README.rdoc)
14
+ s.rdoc_options = %w(--main README.rdoc)
15
+ s.summary = "A small gem to interface with the DigitalNZ API"
16
+ s.author = 'James Harton'
17
+ s.email = 'jamesotron@gmail.com'
18
+ s.homepage = 'http://www.helicopter.geek.nz/'
19
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
20
+
21
+ end
22
+
23
+ Rake::GemPackageTask.new(spec) do |pkg|
24
+ pkg.gem_spec = spec
25
+ end
26
+
27
+ Rake::TestTask.new do |t|
28
+ t.libs << 'test'
29
+ t.test_files = FileList["test/**/*_test.rb"]
30
+ t.verbose = true
31
+ end
32
+
33
+ desc 'Generate the gemspec to serve this Gem from Github'
34
+ task :github do
35
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
36
+ File.open(file, 'w') {|f| f << spec.to_ruby }
37
+ puts "Created gemspec: #{file}"
38
+ end
@@ -0,0 +1,39 @@
1
+ module DigitalNZ
2
+
3
+ @@api_key = 0
4
+
5
+ def self.api_key
6
+ @@api_key
7
+ end
8
+
9
+ def self.api_key=(x)
10
+ @@api_key = x
11
+ end
12
+
13
+ def self.search(params)
14
+ params = { :search_text => params.to_s } unless params.is_a?(Hash)
15
+ params[:api_key]=@@api_key
16
+ DigitalNZ::Search.new(params)
17
+ end
18
+
19
+ def self.record(url)
20
+ DigitalNZ::Record.new(url)
21
+ end
22
+
23
+ end
24
+
25
+ def require_local(suffix)
26
+ require(File.expand_path(File.join(File.dirname(__FILE__), suffix)))
27
+ end
28
+
29
+ # External requires
30
+ require('rubygems')
31
+ require('net/http')
32
+ require('uri')
33
+ require('json')
34
+ require('cgi')
35
+
36
+ # Local requires.
37
+ require_local('digitalnz/search')
38
+ require_local('digitalnz/record')
39
+ require_local('digitalnz/version')
@@ -0,0 +1,35 @@
1
+ class DigitalNZ::Record
2
+
3
+ def initialize(url)
4
+ query = [ "api_key=" + CGI.escape(DigitalNZ.api_key) ]
5
+ url += ".json?" + query * "&"
6
+ puts url
7
+ res = fetch(url)
8
+ res = JSON.parse(res.body)
9
+ puts res.to_yaml
10
+ end
11
+
12
+ def id
13
+ @id
14
+ end
15
+
16
+ def url
17
+ @url
18
+ end
19
+
20
+ private
21
+
22
+ def fetch(uri_str, limit = 10)
23
+ # You should choose better exception.
24
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
25
+
26
+ response = Net::HTTP.get_response(URI.parse(uri_str))
27
+ case response
28
+ when Net::HTTPSuccess then response
29
+ when Net::HTTPRedirection then fetch(response['location'], limit - 1)
30
+ else
31
+ response.error!
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,130 @@
1
+ class DigitalNZ::Search
2
+
3
+ def initialize(params)
4
+ url = 'http://api.digitalnz.org/records/v1.json/?'
5
+ query = []
6
+ for k,v in params
7
+ query += [ k.to_s + '=' + CGI.escape(v) ]
8
+ end
9
+ url += query * "&"
10
+ puts url
11
+ res = fetch(url)
12
+ res = JSON.parse(res.body)
13
+ @orig = res
14
+ @num_results_requestes = res['num_results_requested'] || nil
15
+ @count = res['result_count'] || nil
16
+ @start = res['start'] || nil
17
+ @results = []
18
+ for r in res['results']
19
+ @results << Result.new(r)
20
+ end
21
+ end
22
+
23
+ def num_results_requested
24
+ @num_results_requested
25
+ end
26
+
27
+ def count
28
+ @count
29
+ end
30
+
31
+ def start
32
+ @start
33
+ end
34
+
35
+ def results
36
+ @results
37
+ end
38
+
39
+ def to_yaml
40
+ @orig.to_yaml
41
+ end
42
+
43
+ class Result
44
+
45
+ def initialize(args)
46
+ @args = args
47
+ @category = args['category'] if args['category']
48
+ @title = args['title'] if args['title']
49
+ @content_provider = args['content_provider'] if args['content_provider']
50
+ @date = DateTime.parse(args['date']) if (args['date'] and args['date'].size > 1)
51
+ @syndication_date = DateTime.parse(args['syndication_date']) if args['syndication_date'] and args['syndication_date'].size
52
+ @description = args['description'] if args['description']
53
+ @id = args['id'] if args['id']
54
+ @source_url = args['source_url'] if args['source_url']
55
+ @display_url = args['display_url'] if args['display_url']
56
+ @thumbnail_url = args['thumbnail_url'] if args['thumbnail_url']
57
+ @metadata_url = args['metadata_url'] if args['metadata_url']
58
+ end
59
+
60
+ def category
61
+ @category
62
+ end
63
+
64
+ def title
65
+ @title
66
+ end
67
+
68
+ def content_provider
69
+ @content_provider
70
+ end
71
+
72
+ def date
73
+ @date
74
+ end
75
+
76
+ def syndication_date
77
+ @syndication_date
78
+ end
79
+
80
+ def description
81
+ @description
82
+ end
83
+
84
+ def display_url
85
+ @display_url
86
+ end
87
+
88
+ def id
89
+ @id
90
+ end
91
+
92
+ def metadata
93
+ if !@metadata.nil?
94
+ @metadata
95
+ else
96
+ @metadata = DigitalNZ.record(@metadata_url) if @metadata_url
97
+ end
98
+ end
99
+
100
+ def source_url
101
+ @source_url
102
+ end
103
+
104
+ def thumbnail_url
105
+ @thumbnail_url
106
+ end
107
+
108
+ def to_yaml
109
+ @args.to_yaml
110
+ end
111
+
112
+ end
113
+
114
+ private
115
+
116
+ def fetch(uri_str, limit = 10)
117
+ # You should choose better exception.
118
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
119
+
120
+ response = Net::HTTP.get_response(URI.parse(uri_str))
121
+ case response
122
+ when Net::HTTPSuccess then response
123
+ when Net::HTTPRedirection then fetch(response['location'], limit - 1)
124
+ else
125
+ response.error!
126
+ end
127
+ end
128
+
129
+ end
130
+
@@ -0,0 +1,13 @@
1
+ module DigitalNZ
2
+ module Version # :nodoc:
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 1
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jamesotron-digitalnz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - James Harton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: jamesotron@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - lib/digitalnz
28
+ - lib/digitalnz/search.rb
29
+ - lib/digitalnz/record.rb
30
+ - lib/digitalnz/version.rb
31
+ - lib/digitalnz.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/jamesotron/digitalnz/tree/master
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --main
37
+ - README.rdoc
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project: digitalnz
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A small gem to interface with the DigitalNZ API
59
+ test_files: []
60
+