mage-hand 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -11,4 +11,6 @@ group :development do
11
11
  gem "bundler", "~> 1.0.0"
12
12
  gem "jeweler", "~> 1.5.1"
13
13
  gem "rcov", ">= 0"
14
+ gem "mocha", ">=0.9.8"
15
+ gem "redgreen"
14
16
  end
data/Gemfile.lock CHANGED
@@ -6,9 +6,12 @@ GEM
6
6
  bundler (~> 1.0.0)
7
7
  git (>= 1.2.5)
8
8
  rake
9
+ mocha (0.9.8)
10
+ rake
9
11
  oauth (0.4.4)
10
12
  rake (0.8.7)
11
13
  rcov (0.9.9)
14
+ redgreen (1.2.2)
12
15
  shoulda (2.11.3)
13
16
 
14
17
  PLATFORMS
@@ -17,6 +20,8 @@ PLATFORMS
17
20
  DEPENDENCIES
18
21
  bundler (~> 1.0.0)
19
22
  jeweler (~> 1.5.1)
23
+ mocha (>= 0.9.8)
20
24
  oauth (>= 0.4.4)
21
25
  rcov
26
+ redgreen
22
27
  shoulda
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/lib/mage-hand.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require 'ob_port/client'
2
+ require 'ob_port/base'
3
+ require 'ob_port/user'
4
+ require 'ob_port/campaign'
2
5
 
3
6
  module MageHand
4
7
 
@@ -8,7 +11,7 @@ module MageHand
8
11
  @mage_client = MageHand::Client.new(session[:request_token], session[:access_token_key],
9
12
  session[:access_token_secret], request.url, params)
10
13
  store_tokens
11
- return true unless @mage_client.access_token.nil?
14
+ return true if logged_in?
12
15
 
13
16
  redirect_to @mage_client.request_token.authorize_url
14
17
  false
@@ -19,7 +22,7 @@ module MageHand
19
22
  end
20
23
 
21
24
  def obsidian_portal
22
- @mage_client.access_token
25
+ @mage_client
23
26
  end
24
27
 
25
28
  def store_tokens
@@ -0,0 +1,17 @@
1
+ module MageHand
2
+ class Base
3
+ attr_accessor :id
4
+
5
+ def initialize(attributes)
6
+ attributes ||= {}
7
+ attributes.each do |key, value|
8
+ setter = "#{key}="
9
+ self.send setter, value if self.respond_to?(setter)
10
+ end
11
+ end
12
+
13
+ def self.model_name
14
+ @_model_name ||= ActiveModel::Name.new(self)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module MageHand
2
+ class Campaign < Base
3
+ attr_accessor :name, :campaign_url, :role, :visibility
4
+ end
5
+ end
@@ -1,42 +1,46 @@
1
1
  module MageHand
2
- LOGIN_URL = 'http://www.obsidianportal.com/'
3
- API_URL = 'http://api.obsidianportal.com/v1/users/me.json'
4
-
5
2
  class Client
6
- cattr_accessor :key, :secret
7
- attr_accessor :consumer, :request_token, :access_token,:access_token_key, :access_token_secret
3
+ attr_accessor :request_token, :access_token_key, :access_token_secret
8
4
 
9
5
  def self.configure(key, secret)
10
- self.key = key
11
- self.secret = secret
6
+ @@key = key
7
+ @@secret = secret
12
8
  end
13
-
9
+
14
10
  def initialize(session_request_token=nil, session_access_token_key=nil, session_access_token_secret=nil,
15
11
  callback=nil, params=nil)
16
12
  @request_token = session_request_token
17
13
  @access_token_key = session_access_token_key
18
14
  @access_token_secret = session_access_token_secret
19
-
20
- @consumer = OAuth::Consumer.new( Client.key,Client.secret, {
21
- :site => 'http://api.obsidianportal.com',
22
- :request_token_url => 'https://www.obsidianportal.com/oauth/request_token',
23
- :authorize_url => 'https://www.obsidianportal.com/oauth/authorize',
24
- :access_token_url => 'https://www.obsidianportal.com/oauth/access_token'})
25
15
 
26
- if logged_in?
27
- @access_token = OAuth::AccessToken.new(@consumer, access_token_key, access_token_secret)
28
- elsif params[:oauth_verifier]
16
+ if !logged_in? && params && params[:oauth_verifier]
29
17
  temp_token = @request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
30
18
  @access_token_key = temp_token.token
31
- @access_token_secret = temp_token.secret
32
- @access_token = OAuth::AccessToken.new(@consumer, @access_token_key, @access_token_secret)
33
- else
34
- @request_token = @consumer.get_request_token(:oauth_callback => callback)
19
+ @access_token_secret = temp_token.secret
20
+ elsif !logged_in?
21
+ @request_token = consumer.get_request_token(:oauth_callback => callback)
35
22
  end
36
23
  end
37
24
 
38
25
  def logged_in?
39
- !!access_token_secret
26
+ !!access_token_secret && !!access_token_key
27
+ end
28
+
29
+ def consumer
30
+ @consumer ||= OAuth::Consumer.new( @@key, @@secret, {
31
+ :site => 'http://api.obsidianportal.com',
32
+ :request_token_url => 'https://www.obsidianportal.com/oauth/request_token',
33
+ :authorize_url => 'https://www.obsidianportal.com/oauth/authorize',
34
+ :access_token_url => 'https://www.obsidianportal.com/oauth/access_token'})
35
+ end
36
+
37
+ def access_token
38
+ return nil unless logged_in?
39
+ @access_token ||= OAuth::AccessToken.new(consumer, access_token_key, access_token_secret)
40
+ end
41
+
42
+ def current_user
43
+ MageHand::User.new(JSON.parse(access_token.get('/v1/users/me.json').body))
40
44
  end
41
45
  end
42
46
  end
@@ -0,0 +1,19 @@
1
+ module MageHand
2
+ class User < Base
3
+ attr_accessor :username, :profile_url, :avatar_image_url,
4
+ :created_at, :last_seen_at, :utc_offset, :locale, :is_ascendant
5
+
6
+ alias :is_ascendant? :is_ascendant
7
+
8
+ def campaigns
9
+ @campaigns
10
+ end
11
+
12
+ def campaigns=(new_campaigns)
13
+ @campaigns = []
14
+ new_campaigns.each do |campaign|
15
+ @campaigns << Campaign.new(campaign)
16
+ end
17
+ end
18
+ end
19
+ end
data/mage-hand.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mage-hand}
8
- s.version = "0.1.2"
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 = ["Steven Hammond"]
12
- s.date = %q{2010-11-24}
12
+ s.date = %q{2010-11-30}
13
13
  s.description = %q{mage-hand is a ghostly hand that reaches across the internet to access the Obsidian Portal API.}
14
14
  s.email = %q{shammond@northpub.com}
15
15
  s.extra_rdoc_files = [
@@ -25,9 +25,13 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "lib/mage-hand.rb",
28
+ "lib/ob_port/base.rb",
29
+ "lib/ob_port/campaign.rb",
28
30
  "lib/ob_port/client.rb",
31
+ "lib/ob_port/user.rb",
29
32
  "mage-hand.gemspec",
30
33
  "test/helper.rb",
34
+ "test/test_client.rb",
31
35
  "test/test_mage-hand.rb"
32
36
  ]
33
37
  s.homepage = %q{http://github.com/shammond42/mage-hand}
@@ -37,6 +41,7 @@ Gem::Specification.new do |s|
37
41
  s.summary = %q{Ruby wrapper around the Obsidian Portal API}
38
42
  s.test_files = [
39
43
  "test/helper.rb",
44
+ "test/test_client.rb",
40
45
  "test/test_mage-hand.rb"
41
46
  ]
42
47
 
@@ -50,6 +55,8 @@ Gem::Specification.new do |s|
50
55
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
56
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
52
57
  s.add_development_dependency(%q<rcov>, [">= 0"])
58
+ s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
59
+ s.add_development_dependency(%q<redgreen>, [">= 0"])
53
60
  s.add_runtime_dependency(%q<oauth>, [">= 0.4.4"])
54
61
  else
55
62
  s.add_dependency(%q<oauth>, [">= 0.4.4"])
@@ -57,6 +64,8 @@ Gem::Specification.new do |s|
57
64
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
65
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
59
66
  s.add_dependency(%q<rcov>, [">= 0"])
67
+ s.add_dependency(%q<mocha>, [">= 0.9.8"])
68
+ s.add_dependency(%q<redgreen>, [">= 0"])
60
69
  s.add_dependency(%q<oauth>, [">= 0.4.4"])
61
70
  end
62
71
  else
@@ -65,6 +74,8 @@ Gem::Specification.new do |s|
65
74
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
75
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
67
76
  s.add_dependency(%q<rcov>, [">= 0"])
77
+ s.add_dependency(%q<mocha>, [">= 0.9.8"])
78
+ s.add_dependency(%q<redgreen>, [">= 0"])
68
79
  s.add_dependency(%q<oauth>, [">= 0.4.4"])
69
80
  end
70
81
  end
data/test/helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'redgreen'
2
3
  require 'bundler'
3
4
  begin
4
5
  Bundler.setup(:default, :development)
@@ -9,10 +10,12 @@ rescue Bundler::BundlerError => e
9
10
  end
10
11
  require 'test/unit'
11
12
  require 'shoulda'
13
+ require 'mocha'
12
14
 
13
15
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
16
  $LOAD_PATH.unshift(File.dirname(__FILE__))
15
17
  require 'mage-hand'
18
+ require 'oauth'
16
19
 
17
20
  class Test::Unit::TestCase
18
21
  end
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "The Obsidian Portal client" do
5
+ should "configure correctly with a key and secret" do
6
+ assert_nothing_raised {MageHand::Client.configure('asdfasdf','asdfasdfasdfasdfasdf')}
7
+ end
8
+ end
9
+
10
+ context "An instance of the client" do
11
+ setup do
12
+ response = Net::HTTPResponse.new("1.1", 200, "")
13
+ @request_token = stub('request token')
14
+ @access_token = stub_everything('access token')
15
+ @access_token.stubs(:token).returns('access_token')
16
+ @access_token.stubs(:secret).returns('access_secret')
17
+ Net::HTTPResponse.any_instance.stubs(:body).returns "{'hello' : 'world'}"
18
+ @request_token.stubs(:get_access_token).returns(@access_token)
19
+ OAuth::AccessToken.any_instance.stubs(:post).returns(response)
20
+ OAuth::Consumer.any_instance.stubs(:get_request_token).returns(@request_token)
21
+
22
+ MageHand::Client.configure('asdfasdf', 'asdfasdfasdfasdfasdf')
23
+ end
24
+ should "be configured to use different urls for authentication and api access" do
25
+ @client = MageHand::Client.new(nil, nil, nil)
26
+ assert @client.consumer.site =~ /api\.obsidianportal\.com/
27
+ assert @client.consumer.request_token_url =~ /https:\/\/www\.obsidianportal\.com/
28
+ assert @client.consumer.authorize_url =~ /https:\/\/www\.obsidianportal\.com/
29
+ assert @client.consumer.access_token_url =~ /https:\/\/www\.obsidianportal\.com/
30
+ end
31
+ should "have a request_token, and not an access token, when authorizing" do
32
+ @client = MageHand::Client.new(nil, nil, nil)
33
+ assert_not_nil @client
34
+ assert_equal @client.request_token, @request_token
35
+ assert_nil @client.access_token
36
+ end
37
+ should "create an access_token when an oauth_verifier and request_token is passed in" do
38
+ @client = MageHand::Client.new(@request_token, nil, nil, nil, {:oauth_verifier => 'asdfasdf'})
39
+ assert_not_nil @client
40
+ assert_not_nil @client.access_token
41
+ assert @client.logged_in?
42
+ end
43
+ should "be logged in if initialized with an access_key and access_secret" do
44
+ @client = MageHand::Client.new(nil, 'asdf', 'asdfasdfasdfasdf')
45
+ assert_not_nil @client
46
+ assert @client.logged_in?
47
+ assert_equal @client.access_token.token, 'asdf'
48
+ assert_equal @client.access_token.secret, 'asdfasdfasdfasdf'
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mage-hand
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steven Hammond
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-24 00:00:00 -05:00
18
+ date: 2010-11-30 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -97,6 +97,36 @@ dependencies:
97
97
  - !ruby/object:Gem::Dependency
98
98
  prerelease: false
99
99
  version_requirements: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 43
105
+ segments:
106
+ - 0
107
+ - 9
108
+ - 8
109
+ version: 0.9.8
110
+ name: mocha
111
+ requirement: *id006
112
+ type: :development
113
+ - !ruby/object:Gem::Dependency
114
+ prerelease: false
115
+ version_requirements: &id007 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ name: redgreen
125
+ requirement: *id007
126
+ type: :development
127
+ - !ruby/object:Gem::Dependency
128
+ prerelease: false
129
+ version_requirements: &id008 !ruby/object:Gem::Requirement
100
130
  none: false
101
131
  requirements:
102
132
  - - ">="
@@ -108,7 +138,7 @@ dependencies:
108
138
  - 4
109
139
  version: 0.4.4
110
140
  name: oauth
111
- requirement: *id006
141
+ requirement: *id008
112
142
  type: :runtime
113
143
  description: mage-hand is a ghostly hand that reaches across the internet to access the Obsidian Portal API.
114
144
  email: shammond@northpub.com
@@ -128,9 +158,13 @@ files:
128
158
  - Rakefile
129
159
  - VERSION
130
160
  - lib/mage-hand.rb
161
+ - lib/ob_port/base.rb
162
+ - lib/ob_port/campaign.rb
131
163
  - lib/ob_port/client.rb
164
+ - lib/ob_port/user.rb
132
165
  - mage-hand.gemspec
133
166
  - test/helper.rb
167
+ - test/test_client.rb
134
168
  - test/test_mage-hand.rb
135
169
  has_rdoc: true
136
170
  homepage: http://github.com/shammond42/mage-hand
@@ -168,4 +202,5 @@ specification_version: 3
168
202
  summary: Ruby wrapper around the Obsidian Portal API
169
203
  test_files:
170
204
  - test/helper.rb
205
+ - test/test_client.rb
171
206
  - test/test_mage-hand.rb