Module: EventMachine::Instagram::Server

Includes:
HttpServer
Defined in:
lib/em-instagram/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subscriberObject

Returns the value of attribute subscriber.



9
10
11
# File 'lib/em-instagram/server.rb', line 9

def subscriber
  @subscriber
end

Instance Method Details

#initializeObject



11
12
13
14
15
# File 'lib/em-instagram/server.rb', line 11

def initialize
  @updates = EventMachine::Queue.new
  process_update
  super
end

#process_http_requestObject



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
44
# File 'lib/em-instagram/server.rb', line 17

def process_http_request
  response = EventMachine::DelegatedHttpResponse.new(self)
  case @http_request_method
  when 'GET'
    params = CGI::parse(@http_query_string)
    response.status = 200
    response.content = params['hub.challenge']
  when 'POST'
    if valid_instagram_response?
      begin
        @updates.push(*JSON.load(@http_post_content))
        response.status = 202
        response.content = 'Accepted'
      rescue JSON::ParserError
        response.status = 400
        response.content = "Invalid JSON"
      end
    else
      response.status = 401
      response.content = "Invalid Request"
    end
  else
    response.status = 405
    response.content = 'Method Not Allowed'
  end

  response.send_response
end

#process_updateObject



46
47
48
# File 'lib/em-instagram/server.rb', line 46

def process_update
  @updates.pop { |data| self.subscriber.receive_notification data; EventMachine::next_tick { process_update } }
end

#valid_instagram_response?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/em-instagram/server.rb', line 50

def valid_instagram_response?
  return false if @http_headers.to_s.size == 0
  sig_arr = @http_headers.split("\x00").select do |header|
    header.include?("X-Hub-Signature".downcase)
  end
  sig = if sig_arr.size > 0
    sig_arr.first.split(":").last.strip
  else
    nil
  end
  digest = OpenSSL::Digest::Digest.new('sha1')
  verify_signature = OpenSSL::HMAC.hexdigest(digest, self.subscriber.client_secret, @http_post_content)
  sig && sig == verify_signature
end