Class: Campfiyah::Adapters::HTTP

Inherits:
Adapter
  • Object
show all
Defined in:
lib/campfiyah/adapters/http.rb

Instance Attribute Summary

Attributes inherited from Adapter

#subdomain, #token

Instance Method Summary collapse

Methods inherited from Adapter

#initialize

Constructor Details

This class inherits a constructor from Campfiyah::Adapters::Adapter

Instance Method Details

#connectionObject



4
5
6
# File 'lib/campfiyah/adapters/http.rb', line 4

def connection
  @connection ||= connection!
end

#connection!Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/campfiyah/adapters/http.rb', line 8

def connection!
  endpoint = "https://#{subdomain}.campfirenow.com"
  Faraday.new endpoint do |conn|
    conn.request  :json
    conn.response :json, :content_type => /\bjson$/

    conn.basic_auth token, 'X'
    conn.adapter Faraday.default_adapter
  end
end

#message(room_id, message) ⇒ Object



38
39
40
41
42
43
# File 'lib/campfiyah/adapters/http.rb', line 38

def message(room_id, message)
  response = connection.post("/room/#{room_id}/speak.json") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = %{{"message":{"body":#{quote(message)}}}}
  end
end

#roomsObject



19
20
21
22
23
24
25
26
27
# File 'lib/campfiyah/adapters/http.rb', line 19

def rooms
  rooms = [ ]
  response = connection.get("/rooms.json")
  if response.status == 200
    rooms = response.body["rooms"]
  end

  rooms.sort { |a,b| b['updated_at'] <=> a['updated_at'] }
end

#user_by_id(id) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/campfiyah/adapters/http.rb', line 29

def user_by_id(id)
  response = connection.get("/users/#{id}.json")
  if response.status == 200
    response.body["user"]
  else
    { }
  end
end