Class: EventMachine::Instagram

Inherits:
Object
  • Object
show all
Includes:
Media, Subscriptions
Defined in:
lib/em-instagram.rb,
lib/em-instagram/server.rb,
lib/em-instagram/request.rb,
lib/em-instagram/version.rb,
lib/em-instagram/api/media.rb,
lib/em-instagram/api/subscriptions.rb

Defined Under Namespace

Modules: Media, Server, Subscriptions Classes: Request

Constant Summary collapse

BASE_URI =
'https://api.instagram.com'
PORT =
443
VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Media

#fetch_geography, #fetch_tag, #media, #media_by_geography, #media_by_tag

Methods included from Subscriptions

#subscribe, #subscribe_next, #subscribe_to, #subscriptions, #unsubscribe

Constructor Details

#initialize(options = nil) ⇒ Instagram

Returns a new instance of Instagram.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/em-instagram.rb', line 17

def initialize(options = nil)
  @host, @port, @server = [options[:host], options[:port], options[:server]]
  @subscription_queue = EventMachine::Queue.new
  @update_queue = EventMachine::Queue.new
  @callback_url = options[:callback_url]
  @default_stream = Proc.new{|update| self.fetch update}
  self.logger = options[:logger]
  @default_params = {:client_id => options[:client_id], :client_secret => options[:client_secret]}
  update

end

Instance Attribute Details

#callback_urlObject (readonly)

Returns the value of attribute callback_url.



15
16
17
# File 'lib/em-instagram.rb', line 15

def callback_url
  @callback_url
end

#default_paramsObject (readonly)

Returns the value of attribute default_params.



15
16
17
# File 'lib/em-instagram.rb', line 15

def default_params
  @default_params
end

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/em-instagram.rb', line 15

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/em-instagram.rb', line 15

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/em-instagram.rb', line 15

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



15
16
17
# File 'lib/em-instagram.rb', line 15

def server
  @server
end

#subscription_queueObject (readonly)

Returns the value of attribute subscription_queue.



15
16
17
# File 'lib/em-instagram.rb', line 15

def subscription_queue
  @subscription_queue
end

#update_callbackObject (readonly)

Returns the value of attribute update_callback.



15
16
17
# File 'lib/em-instagram.rb', line 15

def update_callback
  @update_callback
end

Instance Method Details

#fetch(update) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/em-instagram.rb', line 29

def fetch(update)
  case update['object']
  when 'geography'
    fetch_geography update['object_id']
  when 'tag'
    fetch_tag update['object_id']
  end
end

#on_update(&block) ⇒ Object



52
53
54
# File 'lib/em-instagram.rb', line 52

def on_update(&block)
  @update_callback = block
end

#receive_notification(data) ⇒ Object



68
69
70
71
# File 'lib/em-instagram.rb', line 68

def receive_notification(data)
  stream_set = @streams.nil? ? [@default_stream] : @streams
  stream_set.each { |stream| stream.call(data) }
end

#request(method, path, options = {}) ⇒ Object



73
74
75
76
# File 'lib/em-instagram.rb', line 73

def request(method, path, options = {})
  url = URI.join(BASE_URI, path).to_s
  Request.new EventMachine::HttpRequest.new(url).send(method, options)
end

#start_server(&block) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/em-instagram.rb', line 78

def start_server(&block)
  @host ||= '0.0.0.0'
  @port ||= 8080
  @server ||= Server
  EventMachine.start_server(host, port, server) do |connection|
    connection.subscriber = self
  end
end

#stream(&block) ⇒ Object



47
48
49
50
# File 'lib/em-instagram.rb', line 47

def stream(&block)
  @streams ||= []
  @streams << block
end

#update(data = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/em-instagram.rb', line 56

def update(data = nil)
  @update_queue << data if data
  @update_queue.pop do |item|
    if @update_callback
      @update_callback.call(item)
    else
      self.logger.debug(item)
    end
    EventMachine::next_tick { update }
  end
end