mixtli-fred 0.1.3

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.
Files changed (7) hide show
  1. data/Manifest +6 -0
  2. data/README.rdoc +0 -0
  3. data/Rakefile +16 -0
  4. data/fred.gemspec +31 -0
  5. data/init.rb +1 -0
  6. data/lib/fred.rb +107 -0
  7. metadata +64 -0
data/Manifest ADDED
@@ -0,0 +1,6 @@
1
+ README.rdoc
2
+ Manifest
3
+ fred.gemspec
4
+ init.rb
5
+ lib/fred.rb
6
+ Rakefile
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # Rakefile
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('fred', '0.1.4') do |p|
7
+ p.description = "Generate a unique token with Active Record."
8
+ p.url = "http://github.com/mixtli/fred"
9
+ p.author = "Ron McClain"
10
+ p.email = "unknown"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
16
+
data/fred.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{fred}
5
+ s.version = "0.1.3"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ron McClain"]
9
+ s.date = %q{2009-04-14}
10
+ s.description = %q{Generate a unique token with Active Record.}
11
+ s.email = %q{unknown}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/fred.rb"]
13
+ s.files = ["README.rdoc", "Manifest", "fred.gemspec", "init.rb", "lib/fred.rb", "Rakefile"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/mixtli/fred}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fred", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{fred}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Generate a unique token with Active Record.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "fred"
data/lib/fred.rb ADDED
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'hpricot'
4
+ require 'activeresource'
5
+
6
+
7
+ module Fred
8
+ class Base
9
+ cattr_accessor :api_key
10
+ class_inheritable_accessor :base_url, :primary_key, :collection_path, :element_path
11
+ attr_reader :attributes
12
+ self.base_url = "http://api.stlouisfed.org/fred"
13
+
14
+ def initialize(attrs)
15
+ @attributes = attrs
16
+ end
17
+
18
+ def self.get(id)
19
+ uri = base_url + "/#{element_path}?api_key=#{api_key}&#{primary_key}=#{id}"
20
+ RAILS_DEFAULT_LOGGER.debug uri
21
+ doc = Hpricot.XML(open(uri))
22
+ new(doc.search("/*/*")[1].attributes)
23
+ end
24
+
25
+ def self.find(params = {})
26
+ results = []
27
+ uri = base_url + "/" + collection_path + "?" + encode_params(params)
28
+ puts uri
29
+ doc = Hpricot.XML(open(uri))
30
+ doc.search("/*/*").each do |el|
31
+ results << new(el.attributes) if el.respond_to?(:attributes)
32
+ end
33
+ results
34
+ end
35
+
36
+ def self.encode_params(args)
37
+ args['api_key'] ||= api_key
38
+ args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join('&') unless args.blank?
39
+ end
40
+ end
41
+
42
+ class Category < Base
43
+ self.collection_path = "category"
44
+ self.element_path = "category"
45
+ self.primary_key = "category_id"
46
+ end
47
+
48
+ class Release < Base
49
+ self.collection_path = "releases"
50
+ self.element_path = "release"
51
+ self.primary_key = "release_id"
52
+ end
53
+
54
+ class Series < Base
55
+ self.collection_path = "series/search"
56
+ self.element_path = "series"
57
+ self.primary_key = "series_id"
58
+
59
+ def observations(params = {})
60
+ data = []
61
+ puts params.inspect
62
+ params[:series_id] = attributes["id"]
63
+ uri = base_url + "/series/observations?" + self.class.encode_params(params)
64
+ puts "URI = " + uri
65
+ doc = Hpricot.XML(open(uri))
66
+ doc.search("/*/*").each do |el|
67
+ data << el.attributes if el.respond_to?(:attributes)
68
+ end
69
+ data
70
+ end
71
+ end
72
+
73
+ class Source < Base
74
+ self.collection_path = "sources"
75
+ self.element_path = "source"
76
+ self.primary_key = "source_id"
77
+ end
78
+
79
+ end
80
+
81
+ #Fred::Base.api_key = "xxxxxxxxxxxxxxxxxxxx"
82
+ #
83
+ #puts "Category.get"
84
+ #puts Fred::Category.get(125).inspect
85
+ #
86
+ #puts "Category.find"
87
+ #puts Fred::Category.find.inspect
88
+ #
89
+ #puts "Release.get"
90
+ #puts Fred::Release.get(178).inspect
91
+ #
92
+ #puts "Release.find"
93
+ #puts Fred::Release.find.inspect
94
+ #
95
+ #puts "Series.find"
96
+ #puts Fred::Series.find(:search_text => 'money').inspect
97
+ #
98
+ #puts "Series.get"
99
+ #s = Fred::Series.get('M2ASL')
100
+ #puts s.observations.inspect
101
+ #
102
+ #
103
+ #puts "Source.find"
104
+ #puts Fred::Source.find.inspect
105
+ #
106
+ #puts "Source.get"
107
+ #puts Fred::Source.get(36).inspect
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixtli-fred
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Ron McClain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate a unique token with Active Record.
17
+ email: unknown
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/fred.rb
25
+ files:
26
+ - README.rdoc
27
+ - Manifest
28
+ - fred.gemspec
29
+ - init.rb
30
+ - lib/fred.rb
31
+ - Rakefile
32
+ has_rdoc: true
33
+ homepage: http://github.com/mixtli/fred
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --line-numbers
37
+ - --inline-source
38
+ - --title
39
+ - Fred
40
+ - --main
41
+ - README.rdoc
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "1.2"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: fred
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Generate a unique token with Active Record.
63
+ test_files: []
64
+