chartbeat 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,8 +23,8 @@ historical calls and options
23
23
  c.alerts :since => 1276980619
24
24
  c.snapshots :timestamp => 1276980619
25
25
  c.stats
26
- c.data_series
27
- c.day_data_series
26
+ c.data_series #not working right now
27
+ c.day_data_series :timestamp => 1276980619, :type => 'paths'
28
28
 
29
29
  other calls and options
30
30
 
data/Rakefile CHANGED
@@ -10,7 +10,8 @@ begin
10
10
  gem.email = "almshaw@gmail.com"
11
11
  gem.homepage = "http://github.com/ashaw/chartbeat"
12
12
  gem.authors = ["Al Shaw"]
13
- gem.add_dependency "httparty"
13
+ gem.add_dependency "rest-client"
14
+ gem.add_dependency "crack"
14
15
  gem.add_development_dependency "rspec", ">= 1.2.9"
15
16
  gem.add_development_dependency "fakeweb"
16
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chartbeat}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Al Shaw"]
12
- s.date = %q{2010-06-19}
12
+ s.date = %q{2010-09-17}
13
13
  s.description = %q{A Ruby wrapper for the Chartbeat API}
14
14
  s.email = %q{almshaw@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -54,16 +54,19 @@ Gem::Specification.new do |s|
54
54
  s.specification_version = 3
55
55
 
56
56
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
- s.add_runtime_dependency(%q<httparty>, [">= 0"])
57
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
58
+ s.add_runtime_dependency(%q<crack>, [">= 0"])
58
59
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
59
60
  s.add_development_dependency(%q<fakeweb>, [">= 0"])
60
61
  else
61
- s.add_dependency(%q<httparty>, [">= 0"])
62
+ s.add_dependency(%q<rest-client>, [">= 0"])
63
+ s.add_dependency(%q<crack>, [">= 0"])
62
64
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
63
65
  s.add_dependency(%q<fakeweb>, [">= 0"])
64
66
  end
65
67
  else
66
- s.add_dependency(%q<httparty>, [">= 0"])
68
+ s.add_dependency(%q<rest-client>, [">= 0"])
69
+ s.add_dependency(%q<crack>, [">= 0"])
67
70
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
68
71
  s.add_dependency(%q<fakeweb>, [">= 0"])
69
72
  end
@@ -1,124 +1,35 @@
1
- require 'httparty'
1
+ require 'crack'
2
+ require 'rest_client'
2
3
 
3
4
  class Chartbeat
4
- include HTTParty
5
-
6
- base_uri 'chartbeat.com/api'
7
- format :json
8
-
5
+ YESTERDAY = Time.now.to_i - 86400
6
+ BASE_URI = 'chartbeat.com/api'
7
+ METHODS = [:pages, :pathsummary, :recent, :summize, :toppages, :histogram, :summary]
8
+ DASHAPI_METHODS = [:alerts, :snapshots, :stats, :data_series, :day_data_series]
9
+ DEFAULT_ARG_VALS = {:path => '/', :keys => 'n', :types => 'n', :since => YESTERDAY,
10
+ :timestamp => YESTERDAY, :days => 1, :minutes => 20, :type => 'path',
11
+ :breaks => 'n'}
12
+
9
13
  # c = Chartbeat.new :apikey => 'yourkey', :host => 'yourdomain.com'
10
14
  def initialize(opts = {})
11
- self.class.default_params :apikey => opts[:apikey]
12
- self.class.default_params :host => opts[:host]
13
- end
14
-
15
- #
16
- # real-time calls
17
- #
18
-
19
- # c.pages :path => '/'
20
- def pages(opts = {})
21
- q = { :path => nil
22
- }.merge!(opts)
23
-
24
- self.class.get("/pages/", :query => q)
25
- end
26
-
27
- def pathsummary(opts = {})
28
- q = { :keys => 'n',
29
- :types => 'n'
30
- }.merge!(opts)
31
-
32
- self.class.get("/pathsummary/", :query => q)
15
+ raise RuntimeError, "You must provide an API key or host" unless opts[:apikey] && opts[:host]
16
+ @apikey = opts[:apikey]
17
+ @host = opts[:host]
33
18
  end
34
-
35
- def recent(opts = {})
36
- q = { :limit => nil,
37
- :path => '/'
38
- }.merge!(opts)
39
19
 
40
- self.class.get("/recent/", :query => q)
41
- end
42
-
43
- def summize(opts = {})
44
- q = { :path => '/'
45
- }.merge!(opts)
46
-
47
- self.class.get("/summize/", :query => q)
48
- end
49
-
50
- def toppages(opts = {})
51
- q = { :limit => nil
52
- }.merge!(opts)
53
-
54
- self.class.get("/toppages/", :query => q)
55
- end
56
-
57
- #
58
- # historical calls
59
- #
60
-
61
- YESTERDAY = Time.now.to_i - 86400
62
-
63
- #defaults to yesterday
64
- def alerts(opts = {})
65
- q = { :since => YESTERDAY
66
- }.merge!(opts)
67
-
68
- self.class.get("/dashapi/alerts/", :query => q)
69
- end
70
-
71
- def snapshots(opts = {})
72
- q = { :timestamp => YESTERDAY
73
- }.merge!(opts)
74
-
75
- self.class.get("/dashapi/snapshots/", :query => q)
76
- end
77
-
78
- def stats
79
- self.class.get("/dashapi/stats/")
80
- end
81
-
82
- #NOT WORKING, "We're most likely pushing out some new changes."
83
- def data_series(opts = {})
84
- q = { :timestamp => YESTERDAY,
85
- :days => 1,
86
- :minutes => 20,
87
- :type => 'path',
88
- :val => nil
89
- }.merge!(opts)
20
+ def method_missing(m, *args)
21
+ super unless METHODS.include?(m) || DASHAPI_METHODS.include?(m)
90
22
 
91
- self.class.get("/dashapi/data_series/", :query => q)
92
- end
93
-
94
- def day_data_series(opts = {})
95
- q = { :timestamp => YESTERDAY,
96
- :type => 'paths'
97
- }.merge!(opts)
98
-
99
- self.class.get("/dashapi/day_data_series/", :query => q)
23
+ query = *args
24
+ query_to_perform = {:apikey => @apikey, :host => @host}
25
+ DEFAULT_ARG_VALS.each do |k,v|
26
+ if query && query[k]
27
+ v = query[k]
28
+ end
29
+ query_to_perform[k] = v
30
+ end
31
+ prefix = DASHAPI_METHODS.include?(m) ? '/dashapi' : ''
32
+ data = Crack::JSON.parse(RestClient.get("http://" + BASE_URI + prefix + '/' + m.to_s + '/', :params => query_to_perform))
100
33
  end
101
-
102
- #
103
- # other calls
104
- #
105
-
106
- def histogram(opts = {})
107
- q = { :keys => 'n',
108
- :breaks => 'n',
109
- :path => nil
110
- }.merge!(opts)
111
-
112
- self.class.get('/histogram/', :query => q)
113
- end
114
-
115
- def summary(opts = {})
116
- q = { :keys => 'n',
117
- :types => 'n',
118
- :path => nil
119
- }.merge!(opts)
120
34
 
121
- self.class.get('/summary/', :query => q)
122
- end
123
-
124
35
  end
@@ -4,97 +4,97 @@ HOST = 'fakehost.com'
4
4
  KEY = 'fake_key'
5
5
 
6
6
  describe "Chartbeat" do
7
-
8
- before :each do
9
- @c = Chartbeat.new :apikey => KEY, :host => HOST
10
- end
11
-
7
+
8
+ before :each do
9
+ @c = Chartbeat.new :apikey => KEY, :host => HOST
10
+ end
11
+
12
12
  it "should get and parse the 'pages' endpoint" do
13
- register_uri("http://chartbeat.com/api/pages/?apikey=#{KEY}&host=#{HOST}&path=", "pages")
14
-
15
- pages = @c.pages
16
- titles = pages['titles'].to_a
17
- titles[0][1].should eql('Talking Points Memo | Breaking News and Analysis')
13
+ register_uri("http://chartbeat.com/api/pages/", "pages")
14
+
15
+ pages = @c.pages
16
+ titles = pages['titles'].to_a
17
+ titles[0][1].should eql('Talking Points Memo | Breaking News and Analysis')
18
18
  end
19
19
 
20
20
  it "should get and parse the 'pathsummary' endpoint" do
21
- register_uri("http://chartbeat.com/api/pathsummary/?apikey=#{KEY}&host=#{HOST}&keys=n&types=n", "pathsummary")
22
-
23
- pathsummary = @c.pathsummary.to_a
24
- pathsummary[0][0].should eql('/2010/06/the_gaza_flotilla_raid_what_do_we_know.php')
21
+ register_uri("http://chartbeat.com/api/pathsummary/", "pathsummary")
22
+
23
+ pathsummary = @c.pathsummary.to_a
24
+ pathsummary[0][0].should eql('/2010/06/the_gaza_flotilla_raid_what_do_we_know.php')
25
25
  end
26
26
 
27
27
  it "should get and parse the 'recent' endpoint" do
28
- register_uri("http://chartbeat.com/api/recent/?apikey=#{KEY}&host=#{HOST}&path=%2F&limit=", "recent")
29
-
30
- recent = @c.recent
31
- recent[0]['i'].should eql('Talking Points Memo | Breaking News and Analysis')
28
+ register_uri("http://chartbeat.com/api/recent/", "recent")
29
+
30
+ recent = @c.recent
31
+ recent[0]['i'].should eql('Talking Points Memo | Breaking News and Analysis')
32
32
  end
33
33
 
34
34
  it "should get and parse the 'summize' endpoint" do
35
- register_uri("http://chartbeat.com/api/summize/?apikey=#{KEY}&host=#{HOST}&path=%2F", "summize")
36
-
37
- summize = @c.summize
38
- summize['new'].should eql(40)
39
- summize['idle'].should eql(382)
35
+ register_uri("http://chartbeat.com/api/summize/", "summize")
36
+
37
+ summize = @c.summize
38
+ summize['new'].should eql(40)
39
+ summize['idle'].should eql(382)
40
40
  end
41
41
 
42
42
  it "should get and parse the 'toppages' endpoint" do
43
- register_uri("http://chartbeat.com/api/toppages/?apikey=#{KEY}&host=#{HOST}&limit=", "toppages")
44
-
45
- toppages = @c.toppages
46
- toppages[0]['i'].should eql('Talking Points Memo | Breaking News and Analysis')
47
- toppages[0]['path'].should eql('/')
43
+ register_uri("http://chartbeat.com/api/toppages/", "toppages")
44
+
45
+ toppages = @c.toppages
46
+ toppages[0]['i'].should eql('Talking Points Memo | Breaking News and Analysis')
47
+ toppages[0]['path'].should eql('/')
48
48
  end
49
-
50
- # Not implemented yet because chartbeat is returning blank arrays for this call
49
+
50
+ # Not implemented yet because chartbeat is returning blank arrays for this call
51
51
  it "should get and parse the 'alerts' endpoint"
52
52
 
53
53
  it "should get and parse the 'snapshots' endpoint" do
54
- register_uri("http://chartbeat.com/api/dashapi/snapshots/?apikey=#{KEY}&host=#{HOST}&timestamp=#{Chartbeat::YESTERDAY}","snapshots")
55
-
56
- snapshots = @c.snapshots.to_a
57
-
58
- titles = snapshots[0]
59
- geo = snapshots[1]
60
- visitlength = snapshots[2]
61
-
62
- titles[1]['/2010/06/jon-stewart-barton-was-actually-reading-disdainful-asshle-digest-at-the-bp-hearings-video.php?ref=fpblg'].should eql ("Jon Stewart: Barton Was Actually Reading 'Disdainful Assh*le Digest' At The BP Hearings (VIDEO) | TPM LiveWire")
54
+ register_uri("http://chartbeat.com/api/dashapi/snapshots/","snapshots")
55
+
56
+ snapshots = @c.snapshots.to_a
57
+
58
+ titles = snapshots[0]
59
+ geo = snapshots[1]
60
+ visitlength = snapshots[2]
61
+
62
+ titles[1]['/2010/06/jon-stewart-barton-was-actually-reading-disdainful-asshle-digest-at-the-bp-hearings-video.php?ref=fpblg'].should eql ("Jon Stewart: Barton Was Actually Reading 'Disdainful Assh*le Digest' At The BP Hearings (VIDEO) | TPM LiveWire")
63
63
  geo[1]['countries']['US'].should eql(474)
64
64
  visitlength[1][0]['idle'].should eql(101.0)
65
65
  end
66
66
 
67
67
  it "should get and parse the 'stats' endpoint" do
68
- register_uri("http://chartbeat.com/api/dashapi/stats/?apikey=#{KEY}&host=#{HOST}","stats")
69
-
70
- stats = @c.stats
71
-
72
- stats['read_min'].should eql(5)
73
- stats['people_max'].should eql(3742)
68
+ register_uri("http://chartbeat.com/api/dashapi/stats/","stats")
69
+
70
+ stats = @c.stats
71
+
72
+ stats['read_min'].should eql(5)
73
+ stats['people_max'].should eql(3742)
74
74
  end
75
-
76
- # Not implemented yet. Chartbeat is returning an error for this call at this time.
75
+
76
+ # Not implemented yet. Chartbeat is returning an error for this call at this time.
77
77
  it "should get and parse the 'data_series' endpoint"
78
78
 
79
79
  it "should get and parse the 'day_data_series' endpoint" do
80
- register_uri("http://chartbeat.com/api/dashapi/day_data_series/?apikey=#{KEY}&host=#{HOST}&type=paths&timestamp=#{Chartbeat::YESTERDAY}","day_data_series")
81
-
82
- day_data_series = @c.day_data_series.to_a
83
- day_data_series[0][0].should eql("/archives/2010/06/painfully_funny php")
80
+ register_uri("http://chartbeat.com/api/dashapi/day_data_series/","day_data_series")
81
+
82
+ day_data_series = @c.day_data_series.to_a
83
+ day_data_series[0][0].should eql("/archives/2010/06/painfully_funny php")
84
84
  end
85
85
 
86
86
  it "should get and parse the 'histogram' endpoint" do
87
- register_uri("http://chartbeat.com/api/histogram/?apikey=#{KEY}&host=#{HOST}&keys=n&breaks=n&path=","histogram")
88
-
89
- histogram = @c.histogram
90
- histogram['n']['data']['sumOfSquares'].should eql(91.0)
87
+ register_uri("http://chartbeat.com/api/histogram/","histogram")
88
+
89
+ histogram = @c.histogram
90
+ histogram['n']['data']['sumOfSquares'].should eql(91.0)
91
91
  end
92
92
 
93
93
  it "should get and parse the 'summary' endpoint" do
94
- register_uri("http://chartbeat.com/api/summary/?apikey=#{KEY}&host=#{HOST}&keys=n&types=n&path=","summary")
94
+ register_uri("http://chartbeat.com/api/summary/","summary")
95
95
 
96
- summary = @c.summary
97
- summary['n']['data']['sumOfSquares'].should eql(99.0)
98
- end
99
-
96
+ summary = @c.summary
97
+ summary['n']['data']['sumOfSquares'].should eql(99.0)
98
+ end
99
+
100
100
  end
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
3
4
  require 'chartbeat'
4
5
  require 'fakeweb'
5
6
  require 'spec'
@@ -15,7 +16,8 @@ def fixture_file(filename)
15
16
  end
16
17
 
17
18
  def register_uri(uri, method)
18
- FakeWeb.register_uri(:get, uri, :body => fixture_file("#{method}.json"))
19
+ query_string = "?breaks=n&type=path&types=n&path=%2F&since=#{Chartbeat::YESTERDAY}&apikey=fake_key&timestamp=#{Chartbeat::YESTERDAY}&days=1&keys=n&minutes=20&host=fakehost.com"
20
+ FakeWeb.register_uri(:get, "#{uri}#{query_string}", :body => fixture_file("#{method}.json"))
19
21
  end
20
22
 
21
23
  Spec::Runner.configure do |config|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chartbeat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Shaw
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-19 00:00:00 -04:00
18
+ date: 2010-09-17 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: httparty
22
+ name: rest-client
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
@@ -33,9 +33,23 @@ dependencies:
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: rspec
36
+ name: crack
37
37
  prerelease: false
38
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
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
39
53
  none: false
40
54
  requirements:
41
55
  - - ">="
@@ -47,11 +61,11 @@ dependencies:
47
61
  - 9
48
62
  version: 1.2.9
49
63
  type: :development
50
- version_requirements: *id002
64
+ version_requirements: *id003
51
65
  - !ruby/object:Gem::Dependency
52
66
  name: fakeweb
53
67
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
68
+ requirement: &id004 !ruby/object:Gem::Requirement
55
69
  none: false
56
70
  requirements:
57
71
  - - ">="
@@ -61,7 +75,7 @@ dependencies:
61
75
  - 0
62
76
  version: "0"
63
77
  type: :development
64
- version_requirements: *id003
78
+ version_requirements: *id004
65
79
  description: A Ruby wrapper for the Chartbeat API
66
80
  email: almshaw@gmail.com
67
81
  executables: []