Module: RestGraph::TestUtil

Extended by:
RR::Adapters::RRMethods
Defined in:
lib/rest-graph/test_util.rb

Constant Summary collapse

Methods =
[:get, :delete, :post, :put]

Class Method Summary collapse

Class Method Details

.default_dataObject



60
61
62
# File 'lib/rest-graph/test_util.rb', line 60

def default_data
  @default_data ||= {'uid' => '1234'}
end

.default_responseObject



56
57
58
# File 'lib/rest-graph/test_util.rb', line 56

def default_response
  @default_response ||= {'data' => []}
end

.historyObject



68
69
70
# File 'lib/rest-graph/test_util.rb', line 68

def history
  @history ||= []
end

.login(id = ) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rest-graph/test_util.rb', line 72

def  id=default_data['uid']
  teardown
  setup

           uid = id.to_s
       expires = '123456789'
        app_id = RestGraph.default_app_id || '5678'
   session_key = "2.random_string.3600.#{expires}-#{uid}"
          salt = 'random-salt'
  access_token = "#{app_id}|#{session_key}|#{salt}"

  self.default_data = {         'uid' =>          uid,
                       'access_token' => access_token,
                        'session_key' =>  session_key}

  get('me'){ user(uid) }
  self
end

.setupObject Also known as: before



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rest-graph/test_util.rb', line 13

def setup
  any_instance_of(RestGraph){ |rg|
    stub(rg).data{default_data}

    stub(rg).fetch{ |opts, uri, meth, payload|
      history << [meth, uri, payload]
      http     = 'https?://[\w\d]+(\.[\w\d]+)+/'
      response = case uri
                   when %r{#{http}method/fql.multiquery}
                     RestGraph.json_decode(
                       Rack::Utils.parse_query(
                         URI.parse(opts[:uri] || uri).query)['queries']).
                           keys.map{ |q|
                             {'name' => q,
                              'fql_result_set' => [default_response]}
                           }
                   when %r{#{http}method/(\w+\.\w+)}
                     case $2
                       when 'friends.getAppUsers'
                         [5678]
                       else
                         [default_response]
                     end
                   else
                     default_response
                 end
      RestGraph.json_encode(response)
    }
  }
  self
end

.teardownObject Also known as: after



46
47
48
49
50
51
52
53
# File 'lib/rest-graph/test_util.rb', line 46

def teardown
  history.clear
  [:default_response, :default_data].each{ |meth| send("#{meth}=", nil) }
  RR::Injections::DoubleInjection.instances[RestGraph].keys.each{ |meth|
    RR::Injections::DoubleInjection.reset_double(RestGraph, meth)
  }
  self
end

.user(id) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rest-graph/test_util.rb', line 91

def user id
  {         'id' => id,
          'name' => 'rest-graph stubbed-user',
    'first_name' => 'rest-graph',
     'last_name' => 'stubbed-user',
          'link' => 'http://www.facebook.com/rest-graph',
         'about' => 'this is a stubbed user in rest-graph',
      'hometown' => {'id' => id*2, 'name' => 'Taiwan'},
           'bio' => 'A super simple Facebook Open Graph API client',
        'quotes' => 'Write programs that do one thing and do it well.',
      'timezone' => 8,
        'locale' => 'en_US',
      'verified' => true,
  'updated_time' => '2010-05-07T15:04:08+0000'}
end