the_castle_client 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in the_castle_client.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1 @@
1
+ This is the client gem to connect to the api for The Castle, the PRX data warehouse.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module TheCastleClient
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,86 @@
1
+ require 'oauth'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ module TheCastleClient
6
+ class << self
7
+
8
+ attr_accessor :key, :secret, :scheme, :host, :port, :version
9
+
10
+ def account_data(account_id, options={})
11
+ url = "/api/#{version}/accounts/#{account_id}#{to_query(options)}"
12
+ get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
13
+ end
14
+
15
+ def account_aggregates(account_id)
16
+ url = "/api/#{version}/accounts/#{account_id}/aggregates"
17
+ get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
18
+ end
19
+
20
+ def piece_data(piece_id, options={})
21
+ url = "/api/#{version}/pieces/#{piece_id}#{to_query(options)}"
22
+ get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
23
+ end
24
+
25
+ def piece_aggregates(piece_id)
26
+ url = "/api/#{version}/pieces/#{piece_id}/aggregates"
27
+ get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
28
+ end
29
+
30
+ def popular_pieces(popular_on=Date.today, limit=25)
31
+ popular_on = Date.parse(popular_on) if popular_on.is_a?(String)
32
+ popular_on_s = popular_on.to_s
33
+ options = {'popular_on'=>popular_on_s, 'limit'=>limit}
34
+ url = "/api/#{version}/pieces/popular#{to_query(options)}"
35
+ get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
36
+ end
37
+
38
+ protected
39
+
40
+ [:delete, :get, :head, :post, :put, :request].each do |method|
41
+ define_method method do |*args|
42
+ access_token.send(method, *args)
43
+ end
44
+ end
45
+
46
+ def consumer
47
+ @consumer ||= OAuth::Consumer.new(key,
48
+ secret,
49
+ :site => "#{scheme || 'http'}://#{host}:#{port}",
50
+ :http_method => :get)
51
+ end
52
+
53
+ def access_token
54
+ @access_token ||= OAuth::AccessToken.new(consumer)
55
+ end
56
+
57
+ def to_query(hash)
58
+ params = ''
59
+ stack = []
60
+
61
+ hash.each do |k, v|
62
+ if v.is_a?(Hash)
63
+ stack << [k,v]
64
+ else
65
+ params << "#{k}=#{v}&"
66
+ end
67
+ end
68
+
69
+ stack.each do |parent, h|
70
+ h.each do |k, v|
71
+ if v.is_a?(Hash)
72
+ stack << ["#{parent}[#{k}]", v]
73
+ else
74
+ params << "#{parent}[#{k}]=#{v}&"
75
+ end
76
+ end
77
+ end
78
+
79
+ params.chop! # trailing &
80
+ (params.nil? || params.size <=0) ? '' : "?#{params}"
81
+ end
82
+
83
+
84
+ end
85
+
86
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'the_castle_client'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ class TestTheCastleClient < Test::Unit::TestCase
4
+
5
+ def setup
6
+ TheCastleClient.key = '9NmQzsWBWs8TyfJswqMmkmjwAH3ItNBIC72KLjQK'
7
+ TheCastleClient.secret = '592BSoaCb4FfwqsZ6Ql8WShVZ6HVnecR4Dk9lRfQ'
8
+ TheCastleClient.host = "development.prx.org"
9
+ TheCastleClient.port = 3000
10
+ TheCastleClient.version = 'v1'
11
+ end
12
+
13
+ def test_account_data
14
+ response = TheCastleClient.account_data(7018, {:scale=>'week', :start_on=>'2010-01-01', :end_on=>'2010-04-01'})
15
+ puts response.read_body
16
+ end
17
+
18
+ def test_account_aggregates
19
+ response = TheCastleClient.account_aggregates(7018)
20
+ puts response.read_body
21
+ end
22
+
23
+ def test_piece_aggregates
24
+ response = TheCastleClient.piece_aggregates(37745)
25
+ puts response.read_body
26
+ end
27
+
28
+ def test_piece_data
29
+ response = TheCastleClient.piece_data(37745, {:scale=>'week', :start_on=>'2010-01-01', :end_on=>'2010-04-01'})
30
+ puts response.read_body
31
+ end
32
+
33
+ def test_popular_pieces
34
+ response = TheCastleClient.popular_pieces(Date.parse('2011-01-03'), 10)
35
+ puts response.read_body
36
+ end
37
+
38
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "the_castle_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "the_castle_client"
7
+ s.version = TheCastleClient::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["kookster"]
10
+ s.email = ["andrew@prx.org"]
11
+ s.homepage = ""
12
+ s.summary = %q{Get data from the castle, the PRX data warehouse.}
13
+ s.description = %q{Client to connect via 2-legged oauth, and get data from the castle.}
14
+
15
+ s.rubyforge_project = "the_castle_client"
16
+
17
+ s.add_dependency("activesupport")
18
+ s.add_dependency("oauth")
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_castle_client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - kookster
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-11 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: oauth
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Client to connect via 2-legged oauth, and get data from the castle.
50
+ email:
51
+ - andrew@prx.org
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - README.md
62
+ - Rakefile
63
+ - lib/the_castle_client.rb
64
+ - lib/the_castle_client/version.rb
65
+ - test/helper.rb
66
+ - test/test_the_castle_client.rb
67
+ - the_castle_client.gemspec
68
+ has_rdoc: true
69
+ homepage: ""
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: the_castle_client
98
+ rubygems_version: 1.4.2
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Get data from the castle, the PRX data warehouse.
102
+ test_files:
103
+ - test/helper.rb
104
+ - test/test_the_castle_client.rb