atduskgreg-dm-fql-adapter 0.0.1 → 0.0.2

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.
@@ -6,7 +6,7 @@ For usage, see example_app.rb.
6
6
 
7
7
  As of 7/16/09, this only ran on the DataMapper Next branch (i.e. 0.10). For details on how to upgrade to that branch, see here: http://sick.snusnu.info/2009/06/03/migrating-to-datamapper-0100/
8
8
 
9
- At some point, I will probably factor this into a gem. You know, if people bug me.
9
+ Install as a gem: sudo gem install atduskgreg-dm-fql-adapter
10
10
 
11
11
  Also, this repo includes Facemask, a more straightforward approach to querying the Facebook API than some of the more complex libs out there.
12
12
 
@@ -9,37 +9,50 @@ require 'json'
9
9
 
10
10
  module DataMapper
11
11
  module Adapters
12
-
13
- class DataObjectsAdapter
14
- module SQL
15
- def quote_name(name)
16
- name
17
- end
18
- end
19
- end
12
+
20
13
 
21
14
  class FqlAdapter < AbstractAdapter
22
15
 
23
16
  include DataMapper::Adapters::DataObjectsAdapter::SQL
24
17
 
18
+ # override DataObjectsAdapter::SQL#quote_name
19
+ def quote_name(name)
20
+ name
21
+ end
22
+
23
+ def quote_value(value)
24
+ case value
25
+ when Array
26
+ "(#{value.map { |entry| quote_value(entry) }.join(', ')})"
27
+ else
28
+ "\"#{value}\""
29
+ end
30
+ end
31
+
25
32
  def initialize(name, options={})
26
33
  super
27
34
 
28
35
  self.resource_naming_convention = DataMapper::NamingConventions::Resource::Underscored
29
36
 
30
- @facebook = Facemask.new :api_key => options[:api_key],
31
- :secret_key => options[:secret_key],
32
- :session_key => options[:session_key]
37
+ @facebook = Facemask.new :api_key => options[:api_key],
38
+ :secret_key => options[:secret_key],
39
+ :session_key => options[:session_key]
33
40
  end
34
41
 
35
42
  def read(query)
36
- q = select_statement(query).to_s
37
-
38
- # hack around the mysterious '?'
39
- q = q.gsub(/\?(\w*)/){"\"#{$1}\""}
40
-
41
- results = @facebook.find_by_fql(q)
43
+ statement, bind_values = select_statement(query)
44
+
45
+ statement.gsub!(/\?/) { quote_value(bind_values.shift) }
46
+
47
+ results = @facebook.find_by_fql(statement)
48
+
42
49
  results = JSON.parse(results)
50
+
51
+ puts
52
+ puts "DM JSON**********"
53
+ puts results.inspect
54
+ puts
55
+
43
56
  query.filter_records(results)
44
57
  end
45
58
 
@@ -12,8 +12,16 @@ class User
12
12
  property :pic_big, String
13
13
  end
14
14
 
15
+ class Stream
16
+ include DataMapper::Resource
17
+
18
+ property :source_id, Integer
19
+ property :post_id, Integer, :key => true
20
+ property :message, String
21
+ end
22
+
15
23
  describe DataMapper::Adapters::FqlAdapter do
16
- before :all do
24
+ before do
17
25
  Facemask.stub!(:new).and_return(@f = mock('Facemask'))
18
26
 
19
27
  @adapter = DataMapper.setup(:default, :adapter => 'fql',
@@ -26,4 +34,16 @@ describe DataMapper::Adapters::FqlAdapter do
26
34
  User.get(12345)
27
35
  end
28
36
 
29
- end
37
+ it "should package up the objects correctly even if there are multiple entries returned" do
38
+
39
+ @f.stub!(:find_by_fql).and_return('[{"post_id":"103592400571_124417260882","source_id":103592400571,"message":"This is another post."},{"post_id":"103592400571_126720720199","source_id":103592400571,"message":"Check it out!"}]')
40
+
41
+ result = Stream.all( :source_id => 103592400571)
42
+
43
+ result.collect{|s| s.message}.should include("This is another post.")
44
+ result.collect{|s| s.message}.should include("Check it out!")
45
+ end
46
+
47
+
48
+
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atduskgreg-dm-fql-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Borenstein