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

#initialize(api_base, api_path, api_key) ⇒ Service

Returns a new instance of Service.



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

.active_serviceObject



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

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

.active_service=(new_service) ⇒ Object



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

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

.process_asyncObject



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

def self.process_async
  active_service.process
end

.with_async(&proc) ⇒ Object

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



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

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

.with_service(service) ⇒ Object



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

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

#active_serviceObject



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

def active_service
  self.class.active_service
end

#post(params) ⇒ Object

TODO: support ssl



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

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

#post_file(params) ⇒ Object



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

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

#post_form(url, params) ⇒ Object



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

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

#post_multipart_form(url, params) ⇒ Object



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

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