Class: Facebooker::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/facebooker/service.rb

Direct Known Subclasses

MockService

Defined Under Namespace

Classes: BaseService, CurlService, NetHttpService, TyphoeusMultiService, TyphoeusService

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Service) initialize(api_base, api_path, api_key)



4
5
6
7
8
# File 'lib/facebooker/service.rb', line 4

def initialize(api_base, api_path, api_key)
  @api_base = api_base
  @api_path = api_path
  @api_key = api_key
end

Class Method Details

+ (Object) active_service



11
12
13
14
15
16
17
18
19
20
# File 'lib/facebooker/service.rb', line 11

def self.active_service
  unless @active_service
    if Facebooker.use_curl?
      @active_service = Facebooker::Service::CurlService.new
    else
      @active_service = Facebooker::Service::NetHttpService.new
    end        
  end
  @active_service
end

+ (Object) active_service=(new_service)



22
23
24
# File 'lib/facebooker/service.rb', line 22

def self.active_service=(new_service)
  @active_service = new_service
end

+ (Object) process_async



58
59
60
# File 'lib/facebooker/service.rb', line 58

def self.process_async
  active_service.process
end

+ (Object) with_async(&proc)

Process all calls to Facebook in th block asynchronously nil will be returned from all calls and no results will be parsed. This is mostly useful for sending large numbers of notifications or sending a lot of profile updates

for example:

User.find_in_batches(:batch_size => 200) do |users|
  Faceboooker::Service.with_async do
    users.each {|u| u.facebook_session.send_notification(...)}
  end
end

Note: You shouldn't make more than about 200 api calls in a with_async block or you might exhaust all filehandles.

This functionality require the typhoeus gem



53
54
55
56
# File 'lib/facebooker/service.rb', line 53

def self.with_async(&proc)
  block_with_process = Proc.new { proc.call ; process_async}
  with_service(Facebooker::Service::TyphoeusMultiService.new,&block_with_process)
end

+ (Object) with_service(service)



26
27
28
29
30
31
32
33
34
# File 'lib/facebooker/service.rb', line 26

def self.with_service(service)
  old_service = active_service
  self.active_service = service
  begin
    yield
  ensure
    self.active_service = old_service
  end
end

Instance Method Details

- (Object) active_service



86
87
88
# File 'lib/facebooker/service.rb', line 86

def active_service
  self.class.active_service
end

- (Object) post(params)

TODO: support ssl



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/facebooker/service.rb', line 64

def post(params)
  attempt = 0
  if active_service.parse_results?
    Parser.parse(params[:method], post_form(url,params) )
  else
    post_form(url,params)
  end
rescue Errno::ECONNRESET, EOFError
  if attempt == 0
    attempt += 1
    retry
  end
end

- (Object) post_file(params)



90
91
92
93
94
# File 'lib/facebooker/service.rb', line 90

def post_file(params)
  service_url = url(params.delete(:base))
  result = post_multipart_form(service_url, params)
  Parser.parse(params[:method], result)
end

- (Object) post_form(url, params)



78
79
80
# File 'lib/facebooker/service.rb', line 78

def post_form(url,params)
  active_service.post_form(url,params)
end

- (Object) post_multipart_form(url, params)



82
83
84
# File 'lib/facebooker/service.rb', line 82

def post_multipart_form(url,params)
  active_service.post_multipart_form(url,params)
end