Class: EventGirl::Client

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

Constant Summary collapse

VERSION =
'1.2.1'
@@api_token =

Class-wide configuration

nil
@@url =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, api_token = nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/event_girl_client.rb', line 18

def initialize(url = nil, api_token = nil)
  @url       = (url       || self.class.url)
  @api_token = (api_token || self.class.api_token).to_s
  raise ArgumentError.new('No url provided.') unless @url
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



16
17
18
# File 'lib/event_girl_client.rb', line 16

def api_token
  @api_token
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/event_girl_client.rb', line 16

def url
  @url
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



56
57
58
# File 'lib/event_girl_client.rb', line 56

def self.configure
  yield self if block_given?
end

Instance Method Details

#send!(title, content = nil) ⇒ Object

POSTs a string to the event_girl server.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/event_girl_client.rb', line 30

def send!(title, content = nil)
  uri = URI.parse(url)

  # Auto-correct missing trailing slash
  path = uri.path == '' ? '/' : uri.path

  # This is all the post request stuff.
  req = Net::HTTP::Post.new(path)

  # The request format and content type is json
  req['Accept']       = "application/json"
  req['Content-Type'] = "application/json"

  # This takes the entered api token and title. This is what is sent. It is a HASH!
  req.body = '{"api_token":"' + api_token + '","incoming_event":{"title":"' + title.to_s + '","content":"' + content.to_s + '"}}'

  # The request is sent via HTTP to the host and port. You also get a response
  # ex: 201 (it worked)
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = ssl?
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  http.request(req)
end

#send_event(*args) ⇒ Object

Deprecated.

Use #send! instead.



25
26
27
# File 'lib/event_girl_client.rb', line 25

def send_event(*args)
  send!(*args)
end