hullio 0.2.1 → 0.3.0

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/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ example
data/README.md CHANGED
@@ -24,6 +24,8 @@ Or install it yourself as:
24
24
  c.org_url = "http://ORG-NAMESPACE.hullapp.io"
25
25
  end
26
26
 
27
+ In Rails, you can include this in an initializer.
28
+
27
29
  ### Making API Calls
28
30
 
29
31
  `get`, `put`, `post` and `delete` methods are directly available on Hull.
@@ -46,7 +48,38 @@ with Hull entities :
46
48
  Hull.delete('entity', { uid: 'http://example.com' })
47
49
 
48
50
 
49
- ### Compiling widgets and tempaltes with Rails' Assets Pipeline
51
+ ### Making API calls as as a User
52
+
53
+ From its user ID
54
+
55
+ Hull.as('51fa7afd09e50d11f1000002').get('me')
56
+
57
+ From a user UID
58
+
59
+ Hull.as('twitter:hull').get('me')
60
+ Hull.as('external:3637').get('me')
61
+
62
+
63
+
64
+ ### Getting the current User
65
+
66
+ Hull.authenticate_user allows you to get the current User's ID.
67
+
68
+ #### Rails
69
+
70
+
71
+ class MyController < ApplicationController
72
+ def current_hull_user_id
73
+ @current_hull_user_id ||= Hull.authenticate_user(request.env)
74
+ end
75
+ def current_hull_user
76
+ // You probably should cache this or record this information in a session
77
+ // to avoid making calls to Hull's API on each request
78
+ @current_hull_user ||= Hull.get(current_hull_user_id)
79
+ end
80
+ end
81
+
82
+ ### Compiling widgets and templates with Rails' Assets Pipeline
50
83
 
51
84
  Load `handlebars_assets` in your Gemfile as part of the assets group
52
85
 
@@ -12,6 +12,10 @@ module Hull
12
12
  Hull::Client.new(options)
13
13
  end
14
14
 
15
+ def as(user_id)
16
+ Hull::Client.new({ user_id: user_id })
17
+ end
18
+
15
19
  # Delegate to hull::Client
16
20
  def method_missing(method, *args, &block)
17
21
  return super unless new.respond_to?(method)
@@ -25,8 +25,9 @@ module Hull
25
25
 
26
26
  def credentials
27
27
  {
28
- :app_id => app_id,
29
- :app_secret => app_secret
28
+ :app_id => @app_id,
29
+ :app_secret => @app_secret,
30
+ :user_id => @user_id,
30
31
  }
31
32
  end
32
33
 
@@ -44,6 +44,7 @@ module Hull
44
44
  :cache_store,
45
45
  :logger,
46
46
  :current_user,
47
+ :user_id,
47
48
  :authenticate_users,
48
49
  :user_attributes,
49
50
  :js_url
@@ -0,0 +1,44 @@
1
+ require 'json'
2
+ require 'rack/request'
3
+
4
+ module Hull::Middlewares
5
+ class Hook
6
+
7
+ attr_reader :options
8
+
9
+ def initialize app, options={}, &handler
10
+ @app = app
11
+ @options = options
12
+ if block_given?
13
+ @handler = handler
14
+ elsif options[:handler]
15
+ @handler = options[:handler]
16
+ end
17
+ end
18
+
19
+ def call env
20
+ path = options[:path] || "/__hull-hooks__"
21
+ if env['PATH_INFO'] == path && valid?(env)
22
+ begin
23
+ request = Rack::Request.new(env)
24
+ event = JSON.parse(request.body.read)
25
+ response = (@handler.call(event, request) || "ok").to_s
26
+ [200, { 'Content-Type' => 'text/html' }, [response]]
27
+ rescue => err
28
+ [500, { 'Content-Type' => 'text/html' }, ["Invalid Request: #{err.inspect}"]]
29
+ end
30
+ else
31
+ @app.call(env)
32
+ end
33
+ end
34
+
35
+ def valid? env
36
+ body = env['rack.input'].read
37
+ env['rack.input'].rewind
38
+ timestamp, nonce, signature = (env['HTTP_HULL_SIGNATURE'] || "").split('.')
39
+ data = [timestamp, nonce, body].join("-")
40
+ signature == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), options[:secret], data)
41
+ end
42
+
43
+ end
44
+ end
@@ -6,7 +6,8 @@ module Hull
6
6
 
7
7
  def call(env)
8
8
  env[:request_headers]["Hull-Access-Token"] = @credentials[:app_secret]
9
- env[:request_headers]["Hull-App-id"] = @credentials[:app_id]
9
+ env[:request_headers]["Hull-App-Id"] = @credentials[:app_id]
10
+ env[:request_headers]["Hull-User-Id"] = @credentials[:user_id] if @credentials[:user_id]
10
11
  @app.call(env)
11
12
  end
12
13
 
@@ -1,3 +1,3 @@
1
1
  module Hull
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hullio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
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-07-13 00:00:00.000000000 Z
13
+ date: 2013-11-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -116,6 +116,7 @@ files:
116
116
  - lib/hull/config.rb
117
117
  - lib/hull/connection.rb
118
118
  - lib/hull/core_ext/hash.rb
119
+ - lib/hull/middlewares/hook.rb
119
120
  - lib/hull/paywall.rb
120
121
  - lib/hull/request.rb
121
122
  - lib/hull/request/auth.rb
@@ -146,4 +147,3 @@ signing_key:
146
147
  specification_version: 3
147
148
  summary: Hull Ruby Client
148
149
  test_files: []
149
- has_rdoc: