mogli 0.0.43 → 0.0.44

Sign up to get free protection for your applications and to get access to all the features.
@@ -109,7 +109,18 @@ module Mogli
109
109
  end
110
110
 
111
111
  add_creation_method(method_name,klass)
112
+ end
112
113
 
114
+ def self.hash_populating_accessor_with_default_field(method_name,default_field,*klass)
115
+ define_method "#{method_name}=" do |hash|
116
+ hash={default_field=>hash} if hash.is_a?(String)
117
+ instance_variable_set("@#{method_name}",client.map_data(hash,klass))
118
+ end
119
+ define_method "#{method_name}" do
120
+ instance_variable_get "@#{method_name}"
121
+ end
122
+
123
+ add_creation_method(method_name,klass)
113
124
  end
114
125
 
115
126
  def self.add_creation_method(name,klass)
@@ -3,7 +3,7 @@ module Mogli
3
3
  class Place < Model
4
4
 
5
5
  define_properties :id, :name, :category, :username, :can_post, :phone, :website, :checkins, :link, :public_transit, :likes, :picture
6
- hash_populating_accessor :location, "Address"
6
+ hash_populating_accessor_with_default_field :location, "street", "Address"
7
7
  hash_populating_accessor :restaurant_services, "RestaurantServices"
8
8
  hash_populating_accessor :restaurant_specialties, "RestaurantSpecialties"
9
9
  hash_populating_accessor :parking, "Parking"
@@ -4,7 +4,7 @@ require 'json'
4
4
  module Mogli
5
5
  class Post < Model
6
6
 
7
- define_properties :id, :to, :message, :picture, :link, :name, :caption,
7
+ define_properties :id, :from, :to, :story, :message, :picture, :link, :name, :caption,
8
8
  :description, :source, :icon, :attribution, :actions, :likes,
9
9
  :created_time, :updated_time, :privacy, :type, :object_id, :properties
10
10
 
@@ -204,26 +204,35 @@ describe Mogli::Client do
204
204
 
205
205
 
206
206
  describe "fql queries" do
207
+
208
+ def wrap_in_parsed_response(data)
209
+ stub(:parsed_response=>data)
210
+ end
211
+
212
+ let(:blank_response) {
213
+ wrap_in_parsed_response({"data"=>[]})
214
+ }
215
+
207
216
  it "defaults to json" do
208
- Mogli::Client.should_receive(:get).with("https://graph.facebook.com/fql",:query=>{:q=>"query",:format=>"json",:access_token=>"1234"}).and_return({:data=>[]})
217
+ Mogli::Client.should_receive(:get).with("https://graph.facebook.com/fql",:query=>{:q=>"query",:format=>"json",:access_token=>"1234"}).and_return(blank_response)
209
218
  client = Mogli::Client.new("1234")
210
219
  client.fql_query("query")
211
220
  end
212
221
 
213
222
  it "supports xml" do
214
- Mogli::Client.should_receive(:get).with("https://graph.facebook.com/fql",:query=>{:q=>"query",:format=>"xml",:access_token=>"1234"}).and_return({:data=>[]})
223
+ Mogli::Client.should_receive(:get).with("https://graph.facebook.com/fql",:query=>{:q=>"query",:format=>"xml",:access_token=>"1234"}).and_return(blank_response)
215
224
  client = Mogli::Client.new("1234")
216
225
  client.fql_query("query",nil,"xml")
217
226
  end
218
227
 
219
228
  it "creates objects if given a class" do
220
- Mogli::Client.should_receive(:get).and_return({:data=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" }})
229
+ Mogli::Client.should_receive(:get).and_return(wrap_in_parsed_response({"data"=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" }}))
221
230
  client = Mogli::Client.new("1234")
222
231
  client.fql_query("query","user").should be_an_instance_of(Mogli::User)
223
232
  end
224
233
 
225
234
  it "Maps the fields if necessary" do
226
- Mogli::Client.should_receive(:get).and_return({:data=>
235
+ Mogli::Client.should_receive(:get).and_return(wrap_in_parsed_response({"data"=>
227
236
  [
228
237
  {
229
238
  :eid=> 182880348415052,
@@ -234,7 +243,7 @@ describe Mogli::Client do
234
243
  :name=> "Cake Walk - Featuring the Artwork of Emily Pelton"
235
244
  }
236
245
  ]
237
- })
246
+ }))
238
247
  client = Mogli::Client.new("1234")
239
248
  events = client.fql_query("query","event")
240
249
  events.first.should be_an_instance_of(Mogli::Event)
@@ -244,13 +253,13 @@ describe Mogli::Client do
244
253
 
245
254
 
246
255
  it "returns a hash if no class is given" do
247
- Mogli::Client.should_receive(:get).and_return(:data=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" })
256
+ Mogli::Client.should_receive(:get).and_return(wrap_in_parsed_response("data"=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" }))
248
257
  client = Mogli::Client.new("1234")
249
258
  client.fql_query("query").should be_an_instance_of(Hash)
250
259
  end
251
260
 
252
261
  it "doesn't create objects if the format is xml" do
253
- Mogli::Client.should_receive(:get).and_return(:data=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" })
262
+ Mogli::Client.should_receive(:get).and_return(wrap_in_parsed_response("data"=>{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" }))
254
263
  client = Mogli::Client.new("1234")
255
264
  client.fql_query("query","user","xml").should be_an_instance_of(Hash)
256
265
  end
@@ -6,6 +6,7 @@ class TestModel < Mogli::Model
6
6
  has_association :comments, "Comment"
7
7
 
8
8
  hash_populating_accessor :from, "User"
9
+ hash_populating_accessor_with_default_field :location,:street, "Location"
9
10
  hash_populating_accessor :activities, "Activity"
10
11
  hash_populating_accessor :actions, "Action"
11
12
  end
@@ -54,6 +55,11 @@ describe Mogli::Model do
54
55
  model.comments.last.message.should == "second"
55
56
  end
56
57
 
58
+ it "Can handle defaults" do
59
+ model = TestModel.new("id"=>1, :location=>"address")
60
+ model.location.street.should == "address"
61
+ end
62
+
57
63
  it "only fetches activities once" do
58
64
  mock_client.should_receive(:get_and_map).once.with("1/comments","Comment", {}).and_return([])
59
65
  model.comments
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mogli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.43
4
+ version: 0.0.44
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie