koinz 0.0.4 → 0.0.5

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.
data/README CHANGED
@@ -0,0 +1,54 @@
1
+ The 'Koinz' gem is the client-library for koinz services:
2
+ - The Koinz omniauth provider (for OAuth2 access)
3
+ - For publishing and subscribing to Koinz notifications!
4
+
5
+ # Token Authentication #
6
+
7
+ As a koinz client-application, you can make 2 types of calls to other
8
+ applications:
9
+ - within the context of the an end-user (Standard OAuth)
10
+ - directly invoking a call without the end-user (2-legged)
11
+
12
+ To invoke an 'token-authenticable' call with user-context
13
+ =========================================================
14
+
15
+ ## Only For User Manager ##
16
+ [param] auth_token: The auth token string
17
+ [param] uri: The uri to be invoked on the User Manager
18
+
19
+ [returns] Decoded Json information.
20
+
21
+ Koinz::OAuth2Client::UserManager.call(auth_token, uri)
22
+
23
+ Example:
24
+ Koinz::OAuth2Client::UserManager.call(auth_token, '/users/sign_out.json')
25
+
26
+
27
+ ## For other applications ##
28
+
29
+ [param] auth_token: The auth token string
30
+ [param] host: The host you want to invoke this on. MERCHANT_APP, PARTNER_APP etc.
31
+ [param] uri: The uri to be invoked on the host
32
+ [param] params: optional arguments
33
+
34
+ [returns] Decoded Json information.
35
+
36
+ Koinz::OAuth2Client.Application.oauth_call(auth_token, host, uri, params => {})
37
+
38
+ Example:
39
+ Koinz::OAuth2Client.Application.oauth_call(auth_token, MERCHANT_APP, '/merchant.json', :name => 'name'
40
+
41
+ To invoke a '2-legged' call without user-context
42
+ =================================================
43
+
44
+ [param] host: The host you want to invoke this on. MERCHANT_APP, PARTNER_APP etc.
45
+ [param] uri: The uri to be invoked on the host
46
+ [param] params: optional arguments
47
+
48
+ Koinz::OAuth2Client::Application.two_legged_call(host, uri, params = {})
49
+
50
+ NOTE: This call is a protected call - internally it picks up the APP, APP_ID and APP_SECRET
51
+ of the application and secures a oauth_token. In this particular case, if the application
52
+ token has expired, it will be automatically refreshed!
53
+
54
+
@@ -15,7 +15,7 @@ module Koinz
15
15
  # psubscribe
16
16
 
17
17
  # Process pending messages first!
18
- REDIS_SUB.pending_messages(events.flatten, &block)
18
+ REDIS_SUB.backlog(events.flatten, &block)
19
19
 
20
20
  REDIS_SUB.psubscribe(*(events.flatten)) do |on|
21
21
  on.psubscribe do |event, total|
@@ -31,8 +31,9 @@ module Koinz
31
31
  # @param [Hash] params to be passed in the URL
32
32
  # TODO: Do we need to support Http-post?
33
33
  # Example:
34
- # Koinz::OAuth2Client.Application.call(@user_session.auth_token, MERCHANT_APP, '/merchant', :name => 'name'
35
- def self.call(access_token, host, uri, params = {})
34
+ # Koinz::OAuth2Client.Application.oauth_call(auth_token, MERCHANT_APP,
35
+ # '/merchant', :name => 'name'
36
+ def self.oauth_call(access_token, host, uri, params = {})
36
37
  # Prepare the entire URL
37
38
  request_uri = "#{host}/#{uri}?access_token=#{access_token}"
38
39
 
@@ -42,6 +43,11 @@ module Koinz
42
43
  end
43
44
 
44
45
  response = Net::HTTP.get_response(URI.parse(request_uri))
46
+
47
+ # Raise an exception if response is not valid
48
+ # A redirect implies token is not valid
49
+ raise OAuth2::AccessDenied.new if [:found, :unauthorized].include?(response.code)
50
+
45
51
  result = ActiveSupport::JSON.decode(response.body)
46
52
  return result
47
53
 
@@ -49,7 +55,35 @@ module Koinz
49
55
  return { :error => e.message }
50
56
  end
51
57
 
52
- def self.secure_token(strategy)
58
+
59
+ # This is a method with which any application can make a 2-legged oauth call
60
+ # to the other apps. Its caters to token-expiry and exceptions.
61
+ # In case of error, it returns Json: { :error => <err-message> }
62
+ def self.two_legged_call(host, uri, params = {})
63
+ result = {}
64
+ 3.times do |attempts|
65
+ @@SECURE_TOKEN ||= secure_token
66
+ result = oauth_call(@@SECURE_TOKEN.token, host, uri, params)
67
+
68
+ if result.is_a?(Hash) and result[:error] && result[:error] =~ /401|302/
69
+ # OAuth2::AccessDenied (Received HTTP 401 during request.)
70
+ # Token expired -- refresh and retry
71
+
72
+ @@SECURE_TOKEN = secure_token
73
+ else
74
+ break
75
+ end
76
+ end
77
+ return result
78
+ end
79
+
80
+ private
81
+
82
+ # Note: There is a dependency on APP, APP_ID, APP_SECRET
83
+ # set in the initializer of the application
84
+ def self.secure_token
85
+ Rails.logger.info("#{APP}, #{APP_ID}, #{APP_SECRET}")
86
+ strategy = OmniAuth::Strategies::Koinz.new(APP, APP_ID, APP_SECRET)
53
87
  client = strategy.client
54
88
 
55
89
  req_params = client.web_server.access_token_params(nil, { :redirect_uri => strategy.secure_callback_url})
data/lib/koinz/redis.rb CHANGED
@@ -12,11 +12,13 @@ class KoinzRedis < Redis
12
12
  timestamp = Time.now.to_i
13
13
  zadd(channel, timestamp, MultiJson.encode([channel, message]))
14
14
  super(channel, MultiJson.encode(message))
15
+
16
+ prune(channel)
15
17
  end
16
18
 
17
19
  # returns the pending messages [ event, payload ] pairs
18
20
  # Events are ordered sets based on timstamp!
19
- def pending_messages(channels, &block)
21
+ def backlog(channels, &block)
20
22
  return if @timestamp == 0
21
23
 
22
24
  Rails.logger.info('Processing Pending messages')
@@ -41,4 +43,15 @@ class KoinzRedis < Redis
41
43
 
42
44
  Rails.logger.info('Completed processing of pending messages')
43
45
  end
46
+
47
+ # For the sake of simplicity, speed and efficiency, we prune only events
48
+ # which we have just published!
49
+ def prune(channel)
50
+ # All the application timestamps. We have to pass varargs hence we send
51
+ # *keys to mget!
52
+ threshold = mget(*keys('*_timestamp_*')).sort.first # The earliest timestamp
53
+
54
+ # threshold is the timestamp before which all events should be destroyed
55
+ zremrangebyscore(channel, '-inf', threshold)
56
+ end
44
57
  end
data/lib/koinz/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Koinz
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koinz
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gautam Rege
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-10 00:00:00 +05:30
18
+ date: 2011-01-14 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency