Class: GoshrineBot::GoshrineRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/goshrine_bot/goshrine_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GoshrineRequest

Returns a new instance of GoshrineRequest.



9
10
11
12
13
# File 'lib/goshrine_bot/goshrine_request.rb', line 9

def initialize(path)
  @url = "#{@@base_url}#{path}"
  @conn = EventMachine::HttpRequest.new(@url)
  @cookie_persist = CookiePersist.new
end

Class Method Details

.base_url=(url) ⇒ Object



5
6
7
# File 'lib/goshrine_bot/goshrine_request.rb', line 5

def self.base_url=(url)
  @@base_url = url
end

Instance Method Details

#do_http(verb, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/goshrine_bot/goshrine_request.rb', line 15

def do_http(verb,options)
  options[:head] = 
    {'Accept' => 'application/json', 
     'cookie' => @cookie_persist.cookie_hash.to_cookie_string}
  
  http = @conn.send verb, options
  
  defer = EM::DefaultDeferrable.new
  http.callback {
    @cookie_persist.cookies << http.response_header[EM::HttpClient::SET_COOKIE]
    defer.succeed(http)
  }
  http.errback {
    puts "failed goshrine request"
    defer.fail(http)
  }
  defer
end

#get(options = {}) ⇒ Object



38
39
40
# File 'lib/goshrine_bot/goshrine_request.rb', line 38

def get(options = {})
  do_http(:get, options)
end

#post(options = {}) ⇒ Object



34
35
36
# File 'lib/goshrine_bot/goshrine_request.rb', line 34

def post(options = {})
  do_http(:post, options)
end