dailycred 0.1.30 → 0.1.31

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/Gemfile +4 -0
  2. data/README.md +102 -2
  3. data/Rakefile +10 -0
  4. data/app/controllers/sessions_controller.rb +32 -0
  5. data/app/views/sessions/info.html.erb +7 -0
  6. data/config/routes.rb +7 -0
  7. data/dailycred.gemspec +3 -3
  8. data/docs/lib/dailycred/acts_as_dailycred.html +42 -0
  9. data/docs/lib/dailycred/client.html +214 -0
  10. data/docs/lib/dailycred/engine.html +112 -0
  11. data/docs/lib/dailycred/helper.html +119 -0
  12. data/docs/lib/dailycred/middleware.html +42 -0
  13. data/docs/lib/dailycred/tests_helper.html +42 -0
  14. data/docs/lib/dailycred/user.html +133 -0
  15. data/docs/lib/dailycred.html +7 -177
  16. data/docs/lib/generators/dailycred_generator.html +7 -3
  17. data/docs/lib/generators/templates/migration_create_user.html +2 -1
  18. data/docs/lib/generators/templates/omniauth.html +36 -3
  19. data/docs/lib/generators/templates/sessions_controller.html +7 -2
  20. data/docs/lib/generators/templates/user.html +7 -3
  21. data/docs/lib/omniauth/strategies/dailycred.html +14 -9
  22. data/lib/dailycred/acts_as_dailycred.rb +25 -0
  23. data/lib/dailycred/client.rb +103 -0
  24. data/lib/dailycred/engine.rb +51 -0
  25. data/lib/dailycred/helper.rb +39 -1
  26. data/lib/{middleware → dailycred}/middleware.rb +12 -14
  27. data/lib/dailycred/tests_helper.rb +5 -0
  28. data/lib/dailycred/user.rb +2 -2
  29. data/lib/dailycred.rb +1 -106
  30. data/lib/generators/dailycred_generator.rb +3 -59
  31. data/lib/generators/templates/info.html.erb +1 -1
  32. data/lib/generators/templates/omniauth.rb +6 -17
  33. data/lib/generators/templates/user.rb +1 -16
  34. data/lib/omniauth/strategies/dailycred.rb +6 -5
  35. data/script/download_info.rb +11 -0
  36. data/spec/omniauth/strategies/dailycred_spec.rb +2 -2
  37. data/spec/support/dailycred_spec.rb +2 -2
  38. data/test/generator_test.rb +5 -36
  39. metadata +20 -6
  40. data/lib/omniauth-dailycred/version.rb +0 -5
@@ -13,15 +13,19 @@
13
13
  <div id="jump_wrapper">
14
14
  <div id="jump_page">
15
15
  <a class="source" href="../../dailycred.html">dailycred.rb</a>
16
+ <a class="source" href="../../dailycred/acts_as_dailycred.html">acts_as_dailycred.rb</a>
17
+ <a class="source" href="../../dailycred/client.html">client.rb</a>
18
+ <a class="source" href="../../dailycred/engine.html">engine.rb</a>
19
+ <a class="source" href="../../dailycred/helper.html">helper.rb</a>
20
+ <a class="source" href="../../dailycred/middleware.html">middleware.rb</a>
21
+ <a class="source" href="../../dailycred/tests_helper.html">tests_helper.rb</a>
22
+ <a class="source" href="../../dailycred/user.html">user.rb</a>
16
23
  <a class="source" href="../dailycred_generator.html">dailycred_generator.rb</a>
17
24
  <a class="source" href="migration_create_user.html">migration_create_user.rb</a>
18
25
  <a class="source" href="omniauth.html">omniauth.rb</a>
19
26
  <a class="source" href="sessions_controller.html">sessions_controller.rb</a>
20
27
  <a class="source" href="user.html">user.rb</a>
21
- <a class="source" href="../../middleware/middleware.html">middleware.rb</a>
22
- <a class="source" href="../../omniauth-dailycred/version.html">version.rb</a>
23
28
  <a class="source" href="../../omniauth/strategies/dailycred.html">dailycred.rb</a>
24
- <a class="source" href="../../user/user.html">user.rb</a>
25
29
  </div>
26
30
  </div>
27
31
  </div>
@@ -33,6 +37,35 @@
33
37
  </tr>
34
38
  </thead>
35
39
  <tbody>
40
+ <tr id='section-1'>
41
+ <td class=docs>
42
+ <div class="pilwrap">
43
+ <a class="pilcrow" href="#section-1">&#182;</a>
44
+ </div>
45
+
46
+ </td>
47
+ <td class=code>
48
+ <div class='highlight'><pre><span class="no">Rails</span><span class="o">.</span><span class="n">configuration</span><span class="o">.</span><span class="n">DAILYCRED_CLIENT_ID</span> <span class="o">=</span> <span class="s2">&quot;&lt;%= client_id %&gt;&quot;</span>
49
+ <span class="no">Rails</span><span class="o">.</span><span class="n">configuration</span><span class="o">.</span><span class="n">DAILYCRED_SECRET_KEY</span> <span class="o">=</span> <span class="s2">&quot;&lt;%= secret_key %&gt;&quot;</span></pre></div>
50
+ </td>
51
+ </tr>
52
+ <tr id='section-2'>
53
+ <td class=docs>
54
+ <div class="pilwrap">
55
+ <a class="pilcrow" href="#section-2">&#182;</a>
56
+ </div>
57
+ <p>configure where users should be redirected after authentication</p>
58
+
59
+ <p>Rails.configuration.DAILYCRED<em>OPTIONS = {
60
+ :after</em>auth =&gt; &lsquo;/hello&rsquo;, #after login
61
+ :after_unauth =&gt; &lsquo;/goodbye&rsquo; #after logout
62
+ }</p>
63
+
64
+ </td>
65
+ <td class=code>
66
+ <div class='highlight'><pre></pre></div>
67
+ </td>
68
+ </tr>
36
69
  </table>
37
70
  </div>
38
71
  </body>
@@ -13,6 +13,8 @@
13
13
  <div id="jump_wrapper">
14
14
  <div id="jump_page">
15
15
  <a class="source" href="../../dailycred.html">dailycred.rb</a>
16
+ <a class="source" href="../../dailycred/helper.html">helper.rb</a>
17
+ <a class="source" href="../../dailycred/user.html">user.rb</a>
16
18
  <a class="source" href="../dailycred_generator.html">dailycred_generator.rb</a>
17
19
  <a class="source" href="migration_create_user.html">migration_create_user.rb</a>
18
20
  <a class="source" href="omniauth.html">omniauth.rb</a>
@@ -21,7 +23,6 @@
21
23
  <a class="source" href="../../middleware/middleware.html">middleware.rb</a>
22
24
  <a class="source" href="../../omniauth-dailycred/version.html">version.rb</a>
23
25
  <a class="source" href="../../omniauth/strategies/dailycred.html">dailycred.rb</a>
24
- <a class="source" href="../../user/user.html">user.rb</a>
25
26
  </div>
26
27
  </div>
27
28
  </div>
@@ -57,7 +58,7 @@
57
58
  <div class='highlight'><pre> <span class="k">def</span> <span class="nf">create</span>
58
59
  <span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find_or_create_with_omniauth</span> <span class="n">auth_hash</span>
59
60
  <span class="n">session</span><span class="o">[</span><span class="ss">:user_id</span><span class="o">]</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">id</span>
60
- <span class="n">redirect_to</span> <span class="s2">&quot;/auth&quot;</span>
61
+ <span class="n">redirect_to</span> <span class="n">auth_path</span>
61
62
  <span class="k">end</span></pre></div>
62
63
  </td>
63
64
  </tr>
@@ -75,6 +76,10 @@
75
76
  <span class="n">redirect_to</span> <span class="n">auth_path</span>
76
77
  <span class="k">end</span>
77
78
 
79
+ <span class="k">def</span> <span class="nf">failure</span>
80
+ <span class="n">redirect_to</span> <span class="n">auth_path</span><span class="p">,</span> <span class="n">notice</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:message</span><span class="o">]</span>
81
+ <span class="k">end</span>
82
+
78
83
  <span class="k">def</span> <span class="nf">info</span>
79
84
  <span class="k">end</span>
80
85
 
@@ -13,15 +13,19 @@
13
13
  <div id="jump_wrapper">
14
14
  <div id="jump_page">
15
15
  <a class="source" href="../../dailycred.html">dailycred.rb</a>
16
+ <a class="source" href="../../dailycred/acts_as_dailycred.html">acts_as_dailycred.rb</a>
17
+ <a class="source" href="../../dailycred/client.html">client.rb</a>
18
+ <a class="source" href="../../dailycred/engine.html">engine.rb</a>
19
+ <a class="source" href="../../dailycred/helper.html">helper.rb</a>
20
+ <a class="source" href="../../dailycred/middleware.html">middleware.rb</a>
21
+ <a class="source" href="../../dailycred/tests_helper.html">tests_helper.rb</a>
22
+ <a class="source" href="../../dailycred/user.html">user.rb</a>
16
23
  <a class="source" href="../dailycred_generator.html">dailycred_generator.rb</a>
17
24
  <a class="source" href="migration_create_user.html">migration_create_user.rb</a>
18
25
  <a class="source" href="omniauth.html">omniauth.rb</a>
19
26
  <a class="source" href="sessions_controller.html">sessions_controller.rb</a>
20
27
  <a class="source" href="user.html">user.rb</a>
21
- <a class="source" href="../../middleware/middleware.html">middleware.rb</a>
22
- <a class="source" href="../../omniauth-dailycred/version.html">version.rb</a>
23
28
  <a class="source" href="../../omniauth/strategies/dailycred.html">dailycred.rb</a>
24
- <a class="source" href="../../user/user.html">user.rb</a>
25
29
  </div>
26
30
  </div>
27
31
  </div>
@@ -13,15 +13,19 @@
13
13
  <div id="jump_wrapper">
14
14
  <div id="jump_page">
15
15
  <a class="source" href="../../dailycred.html">dailycred.rb</a>
16
+ <a class="source" href="../../dailycred/acts_as_dailycred.html">acts_as_dailycred.rb</a>
17
+ <a class="source" href="../../dailycred/client.html">client.rb</a>
18
+ <a class="source" href="../../dailycred/engine.html">engine.rb</a>
19
+ <a class="source" href="../../dailycred/helper.html">helper.rb</a>
20
+ <a class="source" href="../../dailycred/middleware.html">middleware.rb</a>
21
+ <a class="source" href="../../dailycred/tests_helper.html">tests_helper.rb</a>
22
+ <a class="source" href="../../dailycred/user.html">user.rb</a>
16
23
  <a class="source" href="../../generators/dailycred_generator.html">dailycred_generator.rb</a>
17
24
  <a class="source" href="../../generators/templates/migration_create_user.html">migration_create_user.rb</a>
18
25
  <a class="source" href="../../generators/templates/omniauth.html">omniauth.rb</a>
19
26
  <a class="source" href="../../generators/templates/sessions_controller.html">sessions_controller.rb</a>
20
27
  <a class="source" href="../../generators/templates/user.html">user.rb</a>
21
- <a class="source" href="../../middleware/middleware.html">middleware.rb</a>
22
- <a class="source" href="../../omniauth-dailycred/version.html">version.rb</a>
23
28
  <a class="source" href="dailycred.html">dailycred.rb</a>
24
- <a class="source" href="../../user/user.html">user.rb</a>
25
29
  </div>
26
30
  </div>
27
31
  </div>
@@ -71,7 +75,7 @@
71
75
  <td class=code>
72
76
  <div class='highlight'><pre> <span class="n">option</span> <span class="ss">:client_options</span><span class="p">,</span> <span class="p">{</span>
73
77
  <span class="ss">:site</span> <span class="o">=&gt;</span> <span class="s2">&quot;https://www.dailycred.com&quot;</span><span class="p">,</span>
74
- <span class="ss">:authorize_url</span> <span class="o">=&gt;</span> <span class="s1">&#39;/connect&#39;</span><span class="p">,</span>
78
+ <span class="ss">:authorize_url</span> <span class="o">=&gt;</span> <span class="s1">&#39;/oauth/authorize&#39;</span><span class="p">,</span>
75
79
  <span class="ss">:token_url</span> <span class="o">=&gt;</span> <span class="s1">&#39;/oauth/access_token&#39;</span>
76
80
  <span class="p">}</span></pre></div>
77
81
  </td>
@@ -124,6 +128,7 @@
124
128
  </td>
125
129
  <td class=code>
126
130
  <div class='highlight'><pre> <span class="k">def</span> <span class="nf">request_phase</span>
131
+ <span class="nb">p</span> <span class="n">session</span><span class="o">[</span><span class="s1">&#39;omniauth.state&#39;</span><span class="o">]</span>
127
132
  <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Strategies</span><span class="o">::</span><span class="no">Dailycred</span><span class="o">::</span><span class="no">AUTH_PARAMS</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">param</span><span class="o">|</span>
128
133
  <span class="n">val</span> <span class="o">=</span> <span class="n">session</span><span class="o">[</span><span class="s1">&#39;omniauth.params&#39;</span><span class="o">][</span><span class="n">param</span><span class="o">]</span>
129
134
  <span class="k">if</span> <span class="n">val</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">val</span><span class="o">.</span><span class="n">empty?</span>
@@ -147,7 +152,7 @@
147
152
  <td class=code>
148
153
  <div class='highlight'><pre> <span class="k">def</span> <span class="nf">user</span>
149
154
  <span class="k">return</span> <span class="vi">@duser</span> <span class="k">if</span> <span class="o">!</span><span class="vi">@duser</span><span class="o">.</span><span class="n">nil?</span>
150
- <span class="n">connection</span> <span class="o">=</span> <span class="no">Faraday</span><span class="o">::</span><span class="no">Connection</span><span class="o">.</span><span class="n">new</span> <span class="n">options</span><span class="o">.</span><span class="n">client_options</span><span class="o">[</span><span class="ss">:site</span><span class="o">]</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">client_options</span><span class="o">[</span><span class="ss">:ssl</span><span class="o">]</span>
155
+ <span class="n">connection</span> <span class="o">=</span> <span class="no">Faraday</span><span class="o">::</span><span class="no">Connection</span><span class="o">.</span><span class="n">new</span> <span class="n">options</span><span class="o">.</span><span class="n">client_options</span><span class="o">[</span><span class="ss">:site</span><span class="o">]</span><span class="p">,</span> <span class="ss">:ssl</span> <span class="o">=&gt;</span> <span class="n">options</span><span class="o">.</span><span class="n">client_options</span><span class="o">[</span><span class="ss">:ssl</span><span class="o">]</span>
151
156
  <span class="n">response</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;/graph/me.json?access_token=</span><span class="si">#{</span><span class="n">access_token</span><span class="o">.</span><span class="n">token</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
152
157
  <span class="n">json</span> <span class="o">=</span> <span class="no">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">body</span><span class="p">)</span></pre></div>
153
158
  </td>
@@ -166,10 +171,10 @@
166
171
  <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Strategies</span><span class="o">::</span><span class="no">Dailycred</span><span class="o">::</span><span class="no">ATTRIBUTES</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="kp">attr</span><span class="o">|</span>
167
172
  <span class="vi">@duser</span><span class="o">[</span><span class="kp">attr</span><span class="o">]</span> <span class="o">=</span> <span class="n">json</span><span class="o">[</span><span class="kp">attr</span><span class="o">]</span>
168
173
  <span class="k">end</span>
169
- <span class="k">if</span> <span class="o">!</span><span class="n">json</span><span class="o">[</span><span class="s2">&quot;FACEBOOK&quot;</span><span class="o">].</span><span class="n">nil?</span>
170
- <span class="vi">@duser</span><span class="o">[</span><span class="s1">&#39;facebook&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="n">json</span><span class="o">[</span><span class="s2">&quot;FACEBOOK&quot;</span><span class="o">][</span><span class="s2">&quot;members&quot;</span><span class="o">]</span>
171
- <span class="vi">@duser</span><span class="o">[</span><span class="s1">&#39;facebook&#39;</span><span class="o">][</span><span class="s1">&#39;access_token&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="n">json</span><span class="o">[</span><span class="s2">&quot;FACEBOOK&quot;</span><span class="o">][</span><span class="s1">&#39;access_token&#39;</span><span class="o">]</span>
172
- <span class="k">end</span></pre></div>
174
+ <span class="n">json</span><span class="o">[</span><span class="s2">&quot;identities&quot;</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span>
175
+ <span class="vi">@duser</span><span class="o">[</span><span class="n">k</span><span class="o">]</span> <span class="o">=</span> <span class="n">v</span>
176
+ <span class="vi">@duser</span><span class="o">[</span><span class="n">k</span><span class="o">][</span><span class="ss">:access_token</span><span class="o">]</span> <span class="o">=</span> <span class="n">json</span><span class="o">[</span><span class="s2">&quot;access_tokens&quot;</span><span class="o">][</span><span class="n">k</span><span class="o">]</span>
177
+ <span class="k">end</span> <span class="k">if</span> <span class="o">!</span><span class="n">json</span><span class="o">[</span><span class="s2">&quot;identities&quot;</span><span class="o">].</span><span class="n">nil?</span></pre></div>
173
178
  </td>
174
179
  </tr>
175
180
  <tr id='section-9'>
@@ -0,0 +1,25 @@
1
+ module ActsAsDailycred
2
+ def acts_as_dailycred
3
+ serialize :facebook, Hash
4
+ serialize :twitter, Hash
5
+ serialize :google, Hash
6
+ serialize :github, Hash
7
+ serialize :tags, Array
8
+ serialize :referred, Array
9
+
10
+ attr_accessible :email, :username, :created, :verified, :admin, :referred_by, :referred,
11
+ :facebook, :tags, :provider, :uid, :token, :twitter, :google, :github
12
+ extend ActsAsDailycred::SingletonMethods
13
+ end
14
+ module SingletonMethods
15
+ def find_or_create_with_omniauth(model)
16
+ @user = User.find_by_provider_and_uid(model[:provider], model[:uid]) || User.new
17
+ @user.update_attributes model[:info]
18
+ @user
19
+ end
20
+ end
21
+ module InstanceMethods
22
+ end
23
+ end
24
+ require 'active_record'
25
+ ActiveRecord::Base.extend ActsAsDailycred
@@ -0,0 +1,103 @@
1
+ module Dailycred
2
+ class Client
3
+ attr_accessor :client_id, :secret_key, :options, :url
4
+
5
+ URL = "https://www.dailycred.com"
6
+
7
+ ROUTES = {
8
+ :signup => "/user/api/signup.json",
9
+ :login => "/user/api/signin.json"
10
+ }
11
+
12
+ # Initializes a dailycred object
13
+ #
14
+ # - @param [String] client\_id the client's daiycred client id
15
+ # - @param [String] secret\_key the clients secret key
16
+ # - @param [Hash] opts a hash of options
17
+ def initialize(client_id, secret_key="", opts={})
18
+ @client_id = client_id
19
+ @secret_key = secret_key
20
+ @options = opts
21
+ opts[:client_options] ||= {}
22
+ @url = opts[:client_options][:site] || Dailycred::Client::URL
23
+ end
24
+
25
+ # Generates a Dailycred event
26
+ #
27
+ # - @param [String] user_id the user's dailycred user id
28
+ # - @param [String] key the name of the event type
29
+ # - @param [String] val the value of the event (optional)
30
+ def event(user_id, key, val="")
31
+ opts = {
32
+ :key => key,
33
+ :valuestring => val,
34
+ :user_id => user_id
35
+ }
36
+ post "/admin/api/customevent.json", opts
37
+ end
38
+
39
+ # Tag a user in dailycred
40
+ #
41
+ # - @param [String] user_id the user's dailycred user id
42
+ # - @param [String] tag the desired tag to add
43
+ def tag(user_id, tag)
44
+ opts = {
45
+ :user_id => user_id,
46
+ :tag => tag
47
+ }
48
+ post "/admin/api/user/tag.json", opts
49
+ end
50
+
51
+ # Untag a user in dailycred
52
+ # (see #tag)
53
+ def untag(user_id, tag)
54
+ opts = {
55
+ :user_id => user_id,
56
+ :tag => tag
57
+ }
58
+ post "/admin/api/user/untag.json", opts
59
+ end
60
+
61
+ # Send a reset password email
62
+ #
63
+ # - @param [string] user the user's email or username
64
+ def passReset(user)
65
+ opts = {
66
+ :user => user
67
+ }
68
+ post "/password/api/reset", opts
69
+ end
70
+
71
+ # A wildcard for making any post requests to dailycred.
72
+ # client_id and client_secret are automatically added to the request
73
+ #
74
+ # - @param [string] url
75
+ # - @param [hash] opts
76
+ # - @param [boolean] secure whether the client_secret should be passed. Defaults to true
77
+ def post(url, opts, secure=true)
78
+ opts.merge! base_opts(secure)
79
+ response = get_conn.post url, opts
80
+ end
81
+
82
+ private
83
+
84
+ def ssl_opts
85
+ opts = {}
86
+ if @options[:client_options] && @options[:client_options][:ssl]
87
+ opts[:ssl] = @options[:client_options][:ssl]
88
+ end
89
+ opts
90
+ end
91
+
92
+ def base_opts secure=true
93
+ opts = {:client_id => @client_id}
94
+ opts[:client_secret] = @secret_key if secure
95
+ opts
96
+ end
97
+
98
+ def get_conn
99
+ Faraday::Connection.new @url, ssl_opts
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,51 @@
1
+ require 'rails'
2
+ module Dailycred
3
+
4
+ class Engine < Rails::Engine
5
+
6
+ initializer 'dailycred.setup', :after=>"dailycred" do |app|
7
+ conf = Rails.configuration# alias for configuration
8
+ if conf.respond_to? 'DAILYCRED_CLIENT_ID'
9
+ id = conf.DAILYCRED_CLIENT_ID
10
+ secret = conf.DAILYCRED_SECRET_KEY
11
+
12
+ # setup omniauth options, can be overridden. ex:
13
+ # Rails.application.DAILYCRED_OPTIONS = {
14
+ # :client_options => {
15
+ # :site => 'https://www.dailycred.com',
16
+ # :authorize_url => '/connect',
17
+ # :token_url => '/oauth/access_token'
18
+ # }
19
+ # }
20
+ if conf.respond_to? 'DAILYCRED_OPTIONS'
21
+ opts = conf.DAILYCRED_OPTIONS
22
+ opts[:client_options] ||= {}
23
+ opts[:client_options][:ssl] = {}
24
+ else
25
+ opts = {:client_options => {:ssl => {}}}
26
+ conf.DAILYCRED_OPTIONS = opts
27
+ end
28
+
29
+ if File.exists?('/etc/ssl/certs')
30
+ opts[:client_options][:ssl][:ca_path] = '/etc/ssl/certs'
31
+ end
32
+ if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
33
+ opts[:client_options][:ssl][:ca_file] = '/opt/local/share/curl/curl-ca-bundle.crt'
34
+ end
35
+
36
+ Rails.application.config.middleware.use OmniAuth::Builder do
37
+ provider :dailycred, id, secret, opts
38
+ end
39
+
40
+ Rails.application.config.middleware.use "Dailycred::Middleware", id
41
+
42
+ ActiveSupport.on_load(:action_controller) do
43
+ include Dailycred::Helpers
44
+ end
45
+ end
46
+ end
47
+
48
+
49
+ end
50
+
51
+ end
@@ -1,4 +1,42 @@
1
- class DailycredHelper
1
+ module Dailycred
2
+ module Helpers
2
3
 
4
+ # use as a before_filter to only allow signed in users
5
+ # example:
6
+ # before_filter :authenticate
7
+ def authenticate
8
+ redirect_to_auth unless current_user
9
+ end
3
10
 
11
+ # helper method for getting an instance of dailycred
12
+ # example:
13
+ # dailycred.tagUser "user_id", "tag"
14
+ #
15
+ # for more documentation, visit https://www.dailycred.com/api/ruby
16
+ def dailycred
17
+ config = Rails.configuration
18
+ @dailycred ||= Dailycred.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY, config.DAILYCRED_OPTIONS)
19
+ end
20
+
21
+ # when making oauth calls, we may need to redirect to our oauth callback url
22
+ # make sure we have the correct state passed back and forth
23
+ def set_state
24
+ @state = session["omniauth.state"] = SecureRandom.hex(24)
25
+ end
26
+
27
+ def login_path(params={})
28
+ "/auth/dailycred"
29
+ end
30
+
31
+ def redirect_to_auth opts={}
32
+ conf = Rails.configuration.DAILYCRED_OPTIONS
33
+ path = !conf[:after_auth].nil? ? conf[:after_auth] : dailycred_engine.auth_info_path
34
+ redirect_to path, opts
35
+ end
36
+
37
+
38
+ ActiveSupport.on_load(:action_controller) do
39
+ helper_method :current_user, :login_path, :set_state, :dailycred, :authenticate
40
+ end
41
+ end
4
42
  end
@@ -1,4 +1,4 @@
1
- class Dailycred
1
+ module Dailycred
2
2
  class Middleware
3
3
  attr_accessor :client_id
4
4
 
@@ -33,36 +33,34 @@ class Dailycred
33
33
  private
34
34
 
35
35
  def render_dailycred_scripts
36
- <<-EOT
36
+ str =<<-EOT
37
37
  <!-- dailycred -->
38
38
  <script type="text/javascript">
39
39
  (function() {
40
- var dc, dlh, home, id, page, referrer, title, url;
40
+ var dc, url;
41
41
  window.dc_opts = {
42
42
  clientId: "#{@client_id}",
43
43
  home: "#{@opts[:url]}"
44
44
  };
45
- id = dc_opts.clientId;
46
- home = window.dc_opts.home || "https://www.dailycred.com";
47
- dlh = document.location.href;
48
- page = encodeURIComponent(dlh);
49
- title = document.title ? document.title : "";
50
- referrer = document.referrer ? encodeURIComponent(document.referrer) : "";
51
- dc = document.createElement("img");
52
- url = "" + home + "/dc.gif?url=" + page + "&title=" + title + "&client_id=" + window.dc_opts.clientId + "&referrer=" + referrer;
45
+ dc = document.createElement("script");
46
+ url = dc_opts.home + "/public/js/cred.coffee";
53
47
  dc.src = url;
54
48
  document.body.appendChild(dc);
55
49
  }).call(this);
56
50
  </script>
57
- #{'<!--' unless @opts[:sdk]}<script src="#{@opts[:url]}/public/js/dailycred.coffee"></script>
51
+ EOT
52
+ str2 =<<-EOT
53
+ <script src="#{@opts[:url]}/public/js/dailycred.coffee"></script>
58
54
  <script>
59
55
  DC.init({
60
56
  "showModal" : #{@opts[:modal]}
61
57
  #{',"modal":{"triggers":'+@opts[:triggers].to_s+'}' if @opts[:modal]}
62
58
  });
63
- </script>#{'-->' unless @opts[:sdk]}
64
- <!-- end dailycred -->
59
+ </script>
65
60
  EOT
61
+ str += str2 if @opts[:sdk]
62
+ str += "\n<!-- end dailycred -->"
63
+ str
66
64
  end
67
65
 
68
66
  end
@@ -0,0 +1,5 @@
1
+ module Dailycred
2
+ module TestHelper
3
+
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
- class Dailycred
2
- class Auth
1
+ module Dailycred
2
+ class User
3
3
  include ActiveModel::Validations
4
4
  include ActiveModel::Serialization
5
5
 
data/lib/dailycred.rb CHANGED
@@ -1,107 +1,2 @@
1
- require "omniauth-dailycred/version"
2
1
  require "omniauth/strategies/dailycred"
3
- require "middleware/middleware"
4
- # require "dailycred/helper"
5
- # require "dailycred/user"
6
-
7
- class Dailycred
8
-
9
- attr_accessor :client_id, :secret_key, :options, :url
10
-
11
- URL = "https://www.dailycred.com"
12
-
13
- ROUTES = {
14
- :signup => "/user/api/signup.json",
15
- :login => "/user/api/signin.json"
16
- }
17
-
18
- # Initializes a dailycred object
19
- #
20
- # - @param [String] client\_id the client's daiycred client id
21
- # - @param [String] secret\_key the clients secret key
22
- # - @param [Hash] opts a hash of options
23
- def initialize(client_id, secret_key="", opts={})
24
- @client_id = client_id
25
- @secret_key = secret_key
26
- @options = opts
27
- opts[:client_options] ||= {}
28
- @url = opts[:client_options][:site] || Dailycred::URL
29
- end
30
-
31
- # Generates a Dailycred event
32
- #
33
- # - @param [String] user_id the user's dailycred user id
34
- # - @param [String] key the name of the event type
35
- # - @param [String] val the value of the event (optional)
36
- def event(user_id, key, val="")
37
- opts = {
38
- :key => key,
39
- :valuestring => val,
40
- :user_id => user_id
41
- }
42
- post "/admin/api/customevent.json", opts
43
- end
44
-
45
- # Tag a user in dailycred
46
- #
47
- # - @param [String] user_id the user's dailycred user id
48
- # - @param [String] tag the desired tag to add
49
- def tag(user_id, tag)
50
- opts = {
51
- :user_id => user_id,
52
- :tag => tag
53
- }
54
- post "/admin/api/user/tag.json", opts
55
- end
56
-
57
- # Untag a user in dailycred
58
- # (see #tag)
59
- def untag(user_id, tag)
60
- opts = {
61
- :user_id => user_id,
62
- :tag => tag
63
- }
64
- post "/admin/api/user/untag.json", opts
65
- end
66
-
67
- # Send a reset password email
68
- #
69
- # - @param [string] user the user's email or username
70
- def passReset(user)
71
- opts = {
72
- :user => user
73
- }
74
- post "/password/api/reset", opts
75
- end
76
-
77
- # A wildcard for making any post requests to dailycred.
78
- # client_id and client_secret are automatically added to the request
79
- #
80
- # - @param [string] url
81
- # - @param [hash] opts
82
- # - @param [boolean] secure whether the client_secret should be passed. Defaults to true
83
- def post(url, opts, secure=true)
84
- opts.merge! base_opts(secure)
85
- response = get_conn.post url, opts
86
- end
87
-
88
- private
89
-
90
- def ssl_opts
91
- opts = {}
92
- if @options[:client_options] && @options[:client_options][:ssl]
93
- opts[:ssl] = @options[:client_options][:ssl]
94
- end
95
- opts
96
- end
97
-
98
- def base_opts secure=true
99
- opts = {:client_id => @client_id}
100
- opts[:client_secret] = @secret_key if secure
101
- opts
102
- end
103
-
104
- def get_conn
105
- Faraday::Connection.new @url, ssl_opts
106
- end
107
- end
2
+ Dir[File.expand_path('../dailycred/*', __FILE__)].each { |f| require f }