happyfunjuice 0.0.1.beta.2 → 0.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@ class Happyfunjuice::HappyfunjuiceController < ApplicationController
11
11
 
12
12
  render :json=> {
13
13
  activities: Happyfunjuice.activities(
14
+ since: params[:since],
14
15
  page: params[:page],
15
16
  per_page: params[:per_page],
16
17
  type: params[:activity],
@@ -1,15 +1,23 @@
1
1
  module Happyfunjuice
2
- mattr_accessor :activity_list
2
+ mattr_accessor :activity_list, :activity_meta
3
3
  @@activity_list = {}
4
+ @@activity_meta = {}
4
5
 
5
6
  DEFAULT_PER_PAGE = 50
6
- MAX_PER_PAGE = 1000
7
7
 
8
8
  class << self
9
9
  def push_activity( key, scope, opts={}, &block )
10
10
  Happyfunjuice.activity_list[key.to_sym] = Happyfunjuice::Activity.new( key, scope, opts, &block )
11
11
  end
12
12
 
13
+ def set_field_meta( key, field, opts={} )
14
+ @@activity_meta[key] ||= {}
15
+ @@activity_meta[key][field] ||= {}
16
+ opts.each do |k,v|
17
+ @@activity_meta[key][field][k] = v
18
+ end
19
+ end
20
+
13
21
  # Query the list of activities. Data and metadata combined into one method since the
14
22
  # queries are otherwise identical.
15
23
  #
@@ -26,21 +34,26 @@ module Happyfunjuice
26
34
  def activities( opts = {} )
27
35
  # Default options
28
36
  opts.reverse_merge!({
29
- page:1,
37
+ page:nil,
30
38
  per_page: DEFAULT_PER_PAGE,
39
+ since:nil, # The beginning of time
31
40
  type: nil,
32
41
  include_metadata: true,
33
42
  query_activities: true
34
43
  })
35
44
 
36
- # If page unspecified or invalid, return page 1
37
- _per_page = [(opts[:per_page] || DEFAULT_PER_PAGE).to_i,1].max
38
- _page = [opts[:page].to_i || 1,1].max
39
-
40
- raise StandardError.new("No more than #{MAX_PER_PAGE} records may be returned in a single response.") if _per_page > MAX_PER_PAGE
45
+ if opts[:page].nil?
46
+ _per_page = nil
47
+ _page = nil
48
+ else
49
+ _per_page = [(opts[:per_page] || DEFAULT_PER_PAGE).to_i,1].max
50
+ _page = [opts[:page].to_i || 1,1].max
51
+ end
41
52
 
42
53
  ret = {}
43
54
 
55
+ _since = Time.zone.at(opts[:since].try(:to_i)) rescue nil
56
+
44
57
  # Choose specified activity or else all activities
45
58
  types = [(opts[:type].to_sym rescue nil)].reject(&:nil?)
46
59
  types = Happyfunjuice.activity_list.keys if types.empty?
@@ -56,7 +69,7 @@ module Happyfunjuice
56
69
  activity = Happyfunjuice.activity_list[k]
57
70
 
58
71
  _ret = {}
59
- _ret.merge!({data: activity.query(_page,_per_page)}) if opts[:query_activities]
72
+ _ret.merge!({data: activity.query(_page,_per_page,_since)}) if opts[:query_activities]
60
73
  _ret.merge!({metadata: activity.metadata }) if opts[:include_metadata]
61
74
 
62
75
  ret[k] = _ret
@@ -82,20 +95,30 @@ module Happyfunjuice
82
95
  block.yield(@scope.new).keys
83
96
  end
84
97
 
85
- # Execute the query. Reorder (not order, just in case a default order is specified) by id desc
86
- # and paginate.
87
- def query( page=1, limit=DEFAULT_PER_PAGE )
88
- scope.reorder('id desc').limit(limit).offset((page-1)*limit).collect do |activity|
98
+ # Execute the query.
99
+ def query( page, limit, _since )
100
+ _scope = scope.reorder("id desc")
101
+ _scope = _scope.reorder("#{@options[:time_column]} desc") unless @options[:time_column].nil?
102
+ _scope = _scope.where("#{@options[:time_column] || 'created_at'} > ?",_since) unless _since.nil?
103
+ _scope = _scope.limit(limit).offset((page-1)*limit) unless page.nil? or limit.nil?
104
+
105
+ _scope.collect do |activity|
89
106
  block.yield activity
90
107
  end
91
108
  end
92
109
 
93
110
  # Metadata for this activity
94
111
  def metadata
112
+ activity_meta = Happyfunjuice.activity_meta[self.key.to_sym] || {}
113
+ field_meta = {}
114
+ fields.each do |field|
115
+ field_meta[field] = activity_meta[field] || {}
116
+ end
117
+
95
118
  {
96
- key: @key,
119
+ #key: @key, # There shouldn't be any need to specify this since the value of this key is this hash
97
120
  name: @options[:name] || @key.to_s.humanize,
98
- fields: fields
121
+ fields: field_meta
99
122
  }
100
123
  end
101
124
  end
@@ -3,6 +3,9 @@ module Happyfunjuice
3
3
  class << self
4
4
 
5
5
  def setup( &block )
6
+
7
+ # Add option for this later. Filter now.
8
+ Rails.configuration.filter_parameters += [:api_key]
6
9
 
7
10
  # This MUST happen after initialization otherwise configuration variables
8
11
  # are not known to the models at the time activity reports are configured:
@@ -37,6 +40,10 @@ module Happyfunjuice
37
40
  Happyfunjuice.push_activity( key, scope, opts, &block )
38
41
  end
39
42
 
43
+ def meta( key, field, opts={} )
44
+ Happyfunjuice.set_field_meta( key, field, opts )
45
+ end
46
+
40
47
  def mailer= value
41
48
  unless %w( mailchimp sendgrid ).include? value.downcase
42
49
  raise ArgumentError.new('Mailer host must be either sendgrid or mailchimp. Choose one and reconfigure it later if necessary.')
@@ -19,8 +19,8 @@ module ActionDispatch::Routing
19
19
 
20
20
  def activities
21
21
  @mapper.namespace :happyfunjuice do
22
- @mapper.get 'activities' => 'happyfunjuice#activities'
23
- @mapper.get 'metadata' => 'happyfunjuice#metadata'
22
+ @mapper.match ':api_version/activities' => 'happyfunjuice#activities', :via=>:post
23
+ @mapper.match ':api_version/metadata' => 'happyfunjuice#metadata', :via=>:post
24
24
  end
25
25
  end
26
26
 
@@ -1,5 +1,5 @@
1
1
  module Happyfunjuice
2
- VERSION = "0.0.1.beta.2"
2
+ VERSION = "0.1.0.beta.1"
3
3
 
4
4
  class << self
5
5
  def version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: happyfunjuice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta.2
4
+ version: 0.1.0.beta.1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-10 00:00:00.000000000 Z
13
+ date: 2013-05-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: haml