rest-graph 1.0.0 → 1.1.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/CHANGES CHANGED
@@ -1,5 +1,20 @@
1
1
  = rest-graph changes history
2
2
 
3
+ == rest-graph 1.1.0 -- 2010-05-13
4
+ * Main repository was moved to http://github.com/cardinalblue/rest-graph
5
+ Sorry for the inconvenience. I'll keep pushing to both repositories until
6
+ I am too lazy to do that.
7
+
8
+ * Better way to deal with default attributes, use class methods.
9
+
10
+ * If you want to auto load config, do require 'rest-graph/auto_load'
11
+ if it's rails, it would load the config from config/rest-graph.y(a)ml.
12
+ if you're using rails plugin, we do require 'rest-graph/auto_load'
13
+ for you.
14
+
15
+ * Config could be load manually as well. require 'rest-graph/load_config' and
16
+ RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'env')
17
+
3
18
  == rest-graph 1.0.0 -- 2010-05-06
4
19
  * now access_token is saved in data attributes.
5
20
  * cookies related methods got renamed, and saved all data in RestGraph
@@ -1,12 +1,11 @@
1
- = rest-graph 1.0.0
2
- by Lin Jen-Shin (aka godfat-真常[http://godfat.org])
3
- godfat (XD) godfat.org
1
+ = rest-graph 1.1.0
2
+ by Cardinal Blue ( http://cardinalblue.com )
4
3
 
5
4
  == LINKS:
6
5
 
7
- * {github}[http://github.com/godfat/rest-graph]
6
+ * {github}[http://github.com/cardinalblue/rest-graph]
8
7
  * {rubygems}[http://rubygems.org/gems/rest-graph]
9
- * {rdoc}[http://rdoc.info/projects/godfat/rest-graph]
8
+ * {rdoc}[http://rdoc.info/projects/cardinalblue/rest-graph]
10
9
 
11
10
  == DESCRIPTION:
12
11
 
@@ -31,53 +30,72 @@ by Lin Jen-Shin (aka godfat-真常[http://godfat.org])
31
30
  :auto_decode => true, # decode by json
32
31
  :app_id => '123',
33
32
  :secret => '1829')
34
- rg.get('me')
35
- rg.post('feed/me', :message => 'bread!')
36
33
 
37
- # for rack
38
- lambda{ |env|
39
- rg = RestGraph.new(:app_id => '123', :secret => '1829')
40
- rg.parse_rack_env!(env) # auto save access_token if sig checked
41
- rg.access_token # => ...
42
- rg.data['uid'] # => facebook uid
43
- }
34
+ rg.get('me') # GET https://graph.facebook.com/me?access_token=...
35
+ rg.get('4/likes') # GET https://graph.facebook.com/4/likes?access_token=...
36
+
37
+ # GET https://graph.facebook.com/search?q=taiwan&access_token=...
38
+ rg.get('search', :q => 'taiwan')
39
+
40
+ # GET https://graph.facebook.com/me?metadata=1&access_token=...
41
+ rg.get('me', :metadata => '1')
42
+
43
+ # POST https://graph.facebook.com/feed/me?message=bread%21&access_token=...
44
+ rg.post('feed/me', :message => 'bread!')
44
45
 
45
46
  # for fully blown cookies hash
46
47
  rg = RestGraph.new(:app_id => '123', :secret => '1829')
47
48
  rg.parse_cookies!(cookies) # auto save access_token if sig checked
48
- rg.access_token # => ...
49
- rg.data['uid'] # => facebook uid
50
-
51
- # for fbs in cookies
52
- app_id = '456'
53
- rg = RestGraph.new(:app_id => '123', :secret => '1829')
54
- fbs = cookies["fbs_#{rg.app_id}"]
55
- rg.parse_fbs!(fbs) # auto save access_token if sig checked
56
- rg.access_token # => ...
57
- rg.data['uid'] # => facebook uid
49
+ rg.data['uid'] # => facebook uid
58
50
 
59
- # fql query
51
+ # fql query, same as:
52
+ # GET https://api.facebook.com/method/fql.query?query=
53
+ # SELECT+name+FROM+page+WHERE+page_id%3D%22123%22&
54
+ # format=json&access_token=...
60
55
  rg.fql('SELECT name FROM page WHERE page_id="123"')
61
56
 
57
+ # default setting:
58
+ class RestGraph
59
+ def self.default_app_id
60
+ '456'
61
+ end
62
+
63
+ def self.default_secret
64
+ 'category theory'
65
+ end
66
+ end
67
+
68
+ # auto load config
69
+ require 'rest-graph'
70
+ require 'rest-graph/auto_load' # under Rails, load config/rest-graph.yaml
71
+ RestGraph.new # all default options would honor config
72
+ RestGraph.new(:app_id => '123') # default could be override as well
73
+
74
+ # manually load config
75
+ require 'rest-graph/load_config'
76
+ RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'env')
77
+
78
+ # see test/config/rest-graph.yaml for an example for config
79
+
62
80
  == REQUIREMENTS:
63
81
 
64
82
  * tested with MRI 1.8.7 and 1.9.1
65
83
  * gem install rest-client
66
- * gem install json (optional)
84
+ * gem install json (optional)
67
85
  * gem install json_pure (optional)
68
- * gem install rack (optional, to parse access_token in HTTP_COOKIE)
86
+ * gem install rack (optional, to parse access_token in HTTP_COOKIE)
69
87
 
70
88
  == INSTALL:
71
89
 
72
90
  > gem install rest-graph
73
91
  # or if you want rails plugin and bleeding edge
74
- > script/plugin install git://github.com/godfat/rest-graph.git
92
+ > script/plugin install git://github.com/cardinalblue/rest-graph.git
75
93
 
76
94
  == LICENSE:
77
95
 
78
96
  Apache License 2.0
79
97
 
80
- Copyright (c) 2010, Lin Jen-Shin (aka godfat 真常)
98
+ Copyright (c) 2010, Cardinal Blue
81
99
 
82
100
  Licensed under the Apache License, Version 2.0 (the "License");
83
101
  you may not use this file except in compliance with the License.
data/Rakefile CHANGED
@@ -24,12 +24,12 @@ Bones{
24
24
  depend_on 'bacon', :development => true
25
25
 
26
26
  name proj
27
- url "http://github.com/godfat/#{proj}"
28
- authors 'Lin Jen-Shin (aka godfat 真常)'
29
- email 'godfat (XD) godfat.org'
27
+ url "http://github.com/cardinalblue/#{proj}"
28
+ authors ['Cardinal Blue', 'Lin Jen-Shin (aka godfat 真常)']
29
+ email 'dev (XD) cardinalblue.com'
30
30
 
31
31
  history_file 'CHANGES'
32
- readme_file 'README'
32
+ readme_file 'README.rdoc'
33
33
  ignore_file '.gitignore'
34
34
  rdoc.include ['\w+']
35
35
  rdoc.exclude ['test', 'doc', 'Rakefile']
@@ -41,7 +41,3 @@ task :default do
41
41
  Rake.application.options.show_task_pattern = /./
42
42
  Rake.application.display_tasks_and_comments
43
43
  end
44
-
45
- task 'doc:rdoc' do
46
- sh 'cp -r ~/.gem/ruby/1.9.1/gems/rdoc-2.5.6/lib/rdoc/generator/template/darkfish/* doc/'
47
- end
data/init.rb CHANGED
@@ -1 +1,2 @@
1
1
  require 'rest-graph'
2
+ require 'rest-graph/auto_load'
@@ -0,0 +1,4 @@
1
+
2
+ require 'rest-graph/load_config'
3
+
4
+ RestGraph::LoadConfig.auto_load!
@@ -0,0 +1,42 @@
1
+
2
+ require 'erb'
3
+ require 'yaml'
4
+
5
+ require 'rest-graph'
6
+
7
+ class RestGraph < RestGraphStruct
8
+ module LoadConfig
9
+ module_function
10
+ def auto_load!
11
+ LoadConfig.load_if_rails!
12
+ end
13
+
14
+ def load_if_rails!
15
+ return unless Object.const_defined?(:Rails)
16
+ root = Rails.root
17
+ file = ["#{root}/config/rest-graph.yaml", # YAML should use .yaml
18
+ "#{root}/config/rest-graph.yml"].find{|path| File.exist?(path)}
19
+ return unless file
20
+
21
+ LoadConfig.load_config!(file, Rails.env)
22
+ end
23
+
24
+ def load_config! file, env
25
+ config = YAML.load(ERB.new(File.read(file)).result(binding))
26
+ defaults = config[env]
27
+ return unless defaults
28
+
29
+ mod = Module.new
30
+ mod.module_eval(defaults.inject([]){ |r, (k, v)|
31
+ r << <<-RUBY
32
+ def default_#{k}
33
+ # quote strings, leave others free (e.g. false, numbers, etc)
34
+ #{v.kind_of?(String) ? "'#{v}'" : v}
35
+ end
36
+ RUBY
37
+ }.join)
38
+
39
+ RestGraph.send(:extend, mod)
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,6 @@
1
1
 
2
- class RestGraph
3
- VERSION = '1.0.0'
2
+ require 'rest-graph'
3
+
4
+ class RestGraph < RestGraphStruct
5
+ VERSION = '1.1.0'
4
6
  end
data/lib/rest-graph.rb CHANGED
@@ -3,18 +3,42 @@ require 'rest_client'
3
3
 
4
4
  require 'cgi'
5
5
 
6
- class RestGraph < Struct.new(:data, :graph_server, :fql_server,
7
- :accept, :lang, :auto_decode, :app_id, :secret)
8
- def initialize o = {}
9
- self.graph_server = o[:graph_server] || 'https://graph.facebook.com/'
10
- self.fql_server = o[:fql_server] || 'https://api.facebook.com/'
11
- self.accept = o[:accept] || 'text/javascript'
12
- self.lang = o[:lang] || 'en-us'
13
- self.auto_decode = o.key?(:auto_decode) ? o[:auto_decode] : true
14
- self.app_id = o[:app_id]
15
- self.secret = o[:secret]
16
- self.access_token = o[:access_token]
6
+ # the data structure used in RestGraph
7
+ RestGraphStruct = Struct.new(:data, :auto_decode,
8
+ :graph_server, :fql_server,
9
+ :accept, :lang,
10
+ :app_id, :secret)
11
+
12
+ class RestGraph < RestGraphStruct
13
+ Attributes = RestGraphStruct.members.map(&:to_sym)
14
+
15
+ # honor default attributes
16
+ Attributes.each{ |name|
17
+ module_eval <<-RUBY
18
+ def #{name}
19
+ (r = super).nil? ? (self.#{name} = self.class.default_#{name}) : r
20
+ end
21
+ RUBY
22
+ }
23
+
24
+ # setup defaults
25
+ module DefaultAttributes
26
+ extend self
27
+ def default_data ; {} ; end
28
+ def default_auto_decode ; true ; end
29
+ def default_graph_server; 'https://graph.facebook.com/'; end
30
+ def default_fql_server ; 'https://api.facebook.com/' ; end
31
+ def default_accept ; 'text/javascript' ; end
32
+ def default_lang ; 'en-us' ; end
33
+ def default_app_id ; nil ; end
34
+ def default_secret ; nil ; end
35
+ end
36
+ extend DefaultAttributes
17
37
 
38
+ def initialize o = {}
39
+ (Attributes + [:access_token]).each{ |name|
40
+ send("#{name}=", o[name]) if o.key?(name)
41
+ }
18
42
  check_arguments!
19
43
  end
20
44
 
@@ -26,10 +50,6 @@ class RestGraph < Struct.new(:data, :graph_server, :fql_server,
26
50
  data['access_token'] = token
27
51
  end
28
52
 
29
- def data
30
- super || self.data = {}
31
- end
32
-
33
53
  def authorized?
34
54
  !!access_token
35
55
  end
@@ -102,7 +122,7 @@ class RestGraph < Struct.new(:data, :graph_server, :fql_server,
102
122
  def build_query_string q = {}
103
123
  query = q.merge(access_token ? {:access_token => access_token} : {})
104
124
  return '' if query.empty?
105
- return '?' + query.map{ |(k, v)| "#{k}=#{CGI.escape(v)}" }.join('&')
125
+ return '?' + query.map{ |(k, v)| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
106
126
  end
107
127
 
108
128
  def build_headers
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rest-graph}
5
+ s.version = "1.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Cardinal Blue", "Lin Jen-Shin (aka godfat 真常)"]
9
+ s.date = %q{2010-05-11}
10
+ s.description = %q{ super simple facebook open graph api client}
11
+ s.email = %q{godfat (XD) cardinalblue.com}
12
+ s.extra_rdoc_files = ["CHANGES", "LICENSE", "README", "TODO", "bench/config.ru"]
13
+ s.files = ["CHANGES", "LICENSE", "README", "Rakefile", "TODO", "bench/config.ru", "init.rb", "lib/rest-graph.rb", "lib/rest-graph/load_config.rb", "lib/rest-graph/version.rb", "test/common.rb", "test/config/rest-graph.yaml", "test/test_load_config.rb", "test/test_rest-graph.rb"]
14
+ s.homepage = %q{http://github.com/cardinalblue/rest-graph}
15
+ s.rdoc_options = ["--main", "README"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{rest-graph}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{super simple facebook open graph api client}
20
+ s.test_files = ["test/test_load_config.rb", "test/test_rest-graph.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<rest-client>, [">= 1.5.0"])
28
+ s.add_development_dependency(%q<json>, [">= 1.4.3"])
29
+ s.add_development_dependency(%q<rack>, [">= 1.1.0"])
30
+ s.add_development_dependency(%q<rr>, [">= 0.10.11"])
31
+ s.add_development_dependency(%q<webmock>, [">= 1.1.0"])
32
+ s.add_development_dependency(%q<bacon>, [">= 1.1.0"])
33
+ s.add_development_dependency(%q<bones>, [">= 3.4.1"])
34
+ else
35
+ s.add_dependency(%q<rest-client>, [">= 1.5.0"])
36
+ s.add_dependency(%q<json>, [">= 1.4.3"])
37
+ s.add_dependency(%q<rack>, [">= 1.1.0"])
38
+ s.add_dependency(%q<rr>, [">= 0.10.11"])
39
+ s.add_dependency(%q<webmock>, [">= 1.1.0"])
40
+ s.add_dependency(%q<bacon>, [">= 1.1.0"])
41
+ s.add_dependency(%q<bones>, [">= 3.4.1"])
42
+ end
43
+ else
44
+ s.add_dependency(%q<rest-client>, [">= 1.5.0"])
45
+ s.add_dependency(%q<json>, [">= 1.4.3"])
46
+ s.add_dependency(%q<rack>, [">= 1.1.0"])
47
+ s.add_dependency(%q<rr>, [">= 0.10.11"])
48
+ s.add_dependency(%q<webmock>, [">= 1.1.0"])
49
+ s.add_dependency(%q<bacon>, [">= 1.1.0"])
50
+ s.add_dependency(%q<bones>, [">= 3.4.1"])
51
+ end
52
+ end
data/test/common.rb ADDED
@@ -0,0 +1,28 @@
1
+
2
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
3
+ require 'rack' if RUBY_VERSION < '1.9.0' # autoload broken in 1.8?
4
+ require 'rest-graph'
5
+
6
+ require 'rr'
7
+ require 'webmock'
8
+ require 'bacon'
9
+
10
+ include RR::Adapters::RRMethods
11
+ include WebMock
12
+ WebMock.disable_net_connect!
13
+ Bacon.summary_on_exit
14
+
15
+ module TestHelper
16
+ module_function
17
+ def ensure_rollback
18
+ yield
19
+
20
+ ensure # the defaults should remain the same!
21
+ RestGraph.send(:extend, RestGraph::DefaultAttributes.dup)
22
+
23
+ RestGraph::Attributes.each{ |name|
24
+ RestGraph.new.send(name).should ==
25
+ RestGraph::DefaultAttributes.send("default_#{name}")
26
+ }
27
+ end
28
+ end
@@ -0,0 +1,6 @@
1
+
2
+ test:
3
+ app_id: 41829
4
+ secret: <%= 'r41829'.reverse %>
5
+ auto_decode: false
6
+ lang: zh-tw
@@ -0,0 +1,36 @@
1
+
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'common'
4
+ else
5
+ require File.dirname(__FILE__) + '/common'
6
+ end
7
+
8
+ require 'rest-graph/load_config'
9
+
10
+ describe RestGraph::LoadConfig do
11
+
12
+ it 'would honor rails config' do
13
+ ::Rails = Object.new
14
+ mock(Rails).env { 'test' }
15
+ mock(Rails).root{ File.dirname(__FILE__) }
16
+
17
+ check = lambda{
18
+ RestGraph.default_app_id.should == 41829
19
+ RestGraph.default_secret.should == 'r41829'.reverse
20
+ RestGraph.default_auto_decode.should == false
21
+ RestGraph.default_lang.should == 'zh-tw'
22
+ }
23
+
24
+ TestHelper.ensure_rollback{
25
+ RestGraph::LoadConfig.load_if_rails!
26
+ check
27
+ }
28
+
29
+ TestHelper.ensure_rollback{
30
+ RestGraph::LoadConfig.load_config!(
31
+ "#{File.dirname(__FILE__)}/config/rest-graph.yaml",
32
+ 'test')
33
+ check
34
+ }
35
+ end
36
+ end
@@ -1,16 +1,9 @@
1
1
 
2
- require 'rubygems' if RUBY_VERSION < '1.9.0'
3
- require 'rack' if RUBY_VERSION < '1.9.0' # autoload broken in 1.8?
4
- require 'rest-graph'
5
-
6
- require 'rr'
7
- require 'webmock'
8
- require 'bacon'
9
-
10
- include RR::Adapters::RRMethods
11
- include WebMock
12
- WebMock.disable_net_connect!
13
- Bacon.summary_on_exit
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'common'
4
+ else
5
+ require File.dirname(__FILE__) + '/common'
6
+ end
14
7
 
15
8
  describe RestGraph do
16
9
  before do
@@ -156,4 +149,34 @@ describe RestGraph do
156
149
 
157
150
  RestGraph.new(:access_token => token).fql(fql).should == []
158
151
  end
152
+
153
+ it 'would honor default attributes' do
154
+ RestGraph::Attributes.each{ |name|
155
+ RestGraph.new.send(name).should ==
156
+ RestGraph.send("default_#{name}")
157
+
158
+ RestGraph.new.send(name).should ==
159
+ RestGraph::DefaultAttributes.send("default_#{name}")
160
+ }
161
+ end
162
+
163
+ it 'would convert query to string' do
164
+ mock(o = Object.new).to_s{ 'i am mock' }
165
+ stub_request(:get, "https://graph.facebook.com/search?q=i%20am%20mock").
166
+ to_return(:body => 'ok')
167
+ RestGraph.new(:auto_decode => false).get('search', :q => o).should == 'ok'
168
+ end
169
+
170
+ it 'could use module to override default attributes' do
171
+ module BlahAttributes
172
+ def default_app_id
173
+ '1829'
174
+ end
175
+ end
176
+
177
+ TestHelper.ensure_rollback{
178
+ RestGraph.send(:extend, BlahAttributes)
179
+ RestGraph.default_app_id.should == '1829'
180
+ }
181
+ end
159
182
  end
metadata CHANGED
@@ -4,17 +4,18 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 1.0.0
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
+ - Cardinal Blue
12
13
  - "Lin Jen-Shin (aka godfat \xE7\x9C\x9F\xE5\xB8\xB8)"
13
14
  autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-06 00:00:00 +08:00
18
+ date: 2010-05-13 00:00:00 +08:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -116,7 +117,7 @@ dependencies:
116
117
  type: :development
117
118
  version_requirements: *id007
118
119
  description: " super simple facebook open graph api client"
119
- email: godfat (XD) godfat.org
120
+ email: dev (XD) cardinalblue.com
120
121
  executables: []
121
122
 
122
123
  extensions: []
@@ -124,26 +125,32 @@ extensions: []
124
125
  extra_rdoc_files:
125
126
  - CHANGES
126
127
  - LICENSE
127
- - README
128
128
  - TODO
129
+ - rest-graph.gemspec
129
130
  files:
130
131
  - CHANGES
131
132
  - LICENSE
132
- - README
133
+ - README.rdoc
133
134
  - Rakefile
134
135
  - TODO
135
136
  - init.rb
136
137
  - lib/rest-graph.rb
138
+ - lib/rest-graph/auto_load.rb
139
+ - lib/rest-graph/load_config.rb
137
140
  - lib/rest-graph/version.rb
141
+ - rest-graph.gemspec
142
+ - test/common.rb
143
+ - test/config/rest-graph.yaml
144
+ - test/test_load_config.rb
138
145
  - test/test_rest-graph.rb
139
146
  has_rdoc: true
140
- homepage: http://github.com/godfat/rest-graph
147
+ homepage: http://github.com/cardinalblue/rest-graph
141
148
  licenses: []
142
149
 
143
150
  post_install_message:
144
151
  rdoc_options:
145
152
  - --main
146
- - README
153
+ - README.rdoc
147
154
  require_paths:
148
155
  - lib
149
156
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -168,4 +175,5 @@ signing_key:
168
175
  specification_version: 3
169
176
  summary: super simple facebook open graph api client
170
177
  test_files:
178
+ - test/test_load_config.rb
171
179
  - test/test_rest-graph.rb