Skip to content

Commit

Permalink
Add room repository
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielrios committed Jul 18, 2018
1 parent a03d21a commit d29d47e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 7 deletions.
26 changes: 25 additions & 1 deletion lib/hipmost.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
require "hipmost/version"

module Hipmost
# Your code goes here...
class CLI
def self.run(args)
binding.pry
new(**args).run
end

def initialize(path:, rooms:)
@path = Pathname.new(path).expand_path
@outpath = @path.join("..", "out").expand_path
@rooms = rooms
end

def run
load_metadata

parse_rooms

save
end

def load_metadata
@users = UserRepository.load_from(@path)
@rooms = RoomRepository.load_from(@path)
end
end
end
51 changes: 51 additions & 0 deletions lib/hipmost/room_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'json'

module Hipmost
class RoomRepository
attr_accessor :rooms, :name_index
extend Forwardable

def_delegators :@rooms, :size, :[], :select


def load_from(path)
new(path).load
end

def initialize(path)
@path = Pathname.new(path).join("rooms.json")
@rooms = {}
@name_index = {}
end

def load(data = file_data)
json = JSON.load(data)

json.each do |room_obj|
room = room_obj["Room"]
@rooms[room["id"]] = Room.new(room)
@name_index[room["name"]] = room["id"]
end
end

def find_by_name(name)
self[name_index[name]]
end

def file_data
File.read(@path)
end

class Room
def initialize(attrs)
@id = attrs["id"]
@attrs = attrs
end
attr_reader :id, :attrs

def method_missing(method)
attrs[method.to_s]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/hipmost/user_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def inactive?
end

def method_missing(method)
attrs[method]
attrs[method.to_s]
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/hipmost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
it "has a version number" do
expect(Hipmost::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end
58 changes: 58 additions & 0 deletions spec/room_repository_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require_relative '../lib/hipmost/room_repository'

RSpec.describe Hipmost::RoomRepository do
it "loads room data" do
repo = described_class.new(".")
repo.load(data)

expect(repo.size).to eq(2)

room = repo[206455]
expect(room).to be_a(Hipmost::RoomRepository::Room)

room_by_name = repo.find_by_name(room.name)
expect(room_by_name).to eq(room)
end

let(:data) do
<<-JSON
[
{
"Room": {
"created": "2013-05-27T13:29:02+00:00",
"guest_access_url": null,
"id": 206455,
"is_archived": false,
"members": [
752160,
1002528,
844771,
340070,
1017103,
340825
],
"name": "Orbital Impact",
"owner": 340070,
"participants": [],
"privacy": "private",
"topic": "Welcome! Send this link to coworkers who need accounts: https://www.hipchat.com/invite/50371/3c380f069e21ba92e3441c57952e4ed0"
}
},
{
"Room": {
"created": "2013-05-28T14:12:28+00:00",
"guest_access_url": null,
"id": 206977,
"is_archived": false,
"members": [],
"name": "CFXWare",
"owner": 340070,
"participants": [],
"privacy": "public",
"topic": "CFXWare"
}
}
]
JSON
end
end
4 changes: 3 additions & 1 deletion spec/user_respository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

expect(repo.size).to eq(2)

expect(repo[564740]).to be_a(Hipmost::UserRepository::User)
user = repo[564740]
expect(user).to be_a(Hipmost::UserRepository::User)
expect(user.name).to eq("Jeff Burns")
end

let(:data) do
Expand Down

0 comments on commit d29d47e

Please sign in to comment.