Skip to content

Commit

Permalink
Improve owner properties
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed May 6, 2020
1 parent f5a95e4 commit dce625d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
2 changes: 2 additions & 0 deletions db/migrations/20200503132444_create_table_owners.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CREATE TABLE owners (
resolver public.repo_resolver NOT NULL,
slug public.citext NOT NULL,
name TEXT,
description TEXT,
extra jsonb NOT NULL DEFAULT '{}',
shards_count INT,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
Expand Down
2 changes: 2 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ CREATE TABLE public.owners (
resolver public.repo_resolver NOT NULL,
slug public.citext NOT NULL,
name text,
description text,
extra jsonb DEFAULT '{}'::jsonb NOT NULL,
shards_count integer,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
Expand Down
42 changes: 20 additions & 22 deletions spec/service/create_owner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,32 @@ describe Service::CreateOwner do
describe ".fetch_owner_info_github" do
it "do" do
api = Shardbox::GitHubAPI.new("")
api.mock_owner_info = Hash(String, JSON::Any).from_json(<<-JSON)
{
"login": "boni",
"url": "https://github.com/boni",
"bio": "verbum domini manet in eternum",
"company": "RKK",
"createdAt": "2020-05-03T16:50:55Z",
"email": "[email protected]",
"location": "Germany",
"name": "Bonifatius",
"websiteUrl": "boni.org"
}
JSON
api.mock_owner_info = Hash(String, JSON::Any).from_json(<<-JSON)
{
"bio": "verbum domini manet in eternum",
"company": "RKK",
"createdAt": "2020-05-03T16:50:55Z",
"email": "[email protected]",
"location": "Germany",
"name": "Bonifatius",
"websiteUrl": "boni.org"
}
JSON

owner = Repo::Owner.new("github", "boni")

Service::CreateOwner.fetch_owner_info_github(owner, api)

owner.should eq Repo::Owner.new("github", "boni",
name: "Bonifatius",
description: "verbum domini manet in eternum",
email: "[email protected]",
website_url: "boni.org",
extra: {
"location" => JSON::Any.new("Germany"),
"company" => JSON::Any.new("RKK"),
"createdAt" => JSON::Any.new("2020-05-03T16:50:55Z"),
}
name: "Bonifatius",
description: "verbum domini manet in eternum",
extra: {
"location" => JSON::Any.new("Germany"),
"email" => JSON::Any.new("[email protected]"),
"website_url" => JSON::Any.new("boni.org"),
"company" => JSON::Any.new("RKK"),
"created_at" => JSON::Any.new("2020-05-03T16:50:55Z"),
}
)
end
end
Expand Down
16 changes: 9 additions & 7 deletions src/repo/owner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ class Repo::Owner
property name : String?
property shards_count : Int32?
property description : String?
property email : String?
property website_url : String?
property extra : Hash(String, JSON::Any)
property! id : Int64

def initialize(@resolver : String, @slug : String,
@name : String? = nil, @shards_count : Int32? = nil,
@description : String? = nil, @email : String? = nil,
@website_url : String? = nil, @extra : Hash(String, JSON::Any) = Hash(String, JSON::Any).new,
*, @id : Int64? = nil)
@name : String? = nil, @shards_count : Int32? = nil,
@description : String? = nil,
@extra : Hash(String, JSON::Any) = Hash(String, JSON::Any).new,
*, @id : Int64? = nil)
end

def self.from_repo_ref(repo_ref : Ref) : Owner?
Expand All @@ -22,5 +20,9 @@ class Repo::Owner
end
end

def_equals_and_hash @resolver, @slug, @name, @shards_count
def_equals_and_hash @resolver, @slug, @name, @description, @extra, @shards_count

def website_url : String
extra["website_url"]?.as_s
end
end
8 changes: 3 additions & 5 deletions src/service/create_owner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require "../repo/owner"
require "../fetchers/github_api"

struct Service::CreateOwner
property github_api : Shardbox::GitHubAPI { Shardbox::GitHubAPI.new }

def initialize(@db : ShardsDB, @repo_ref : Repo::Ref)
end

Expand Down Expand Up @@ -48,12 +50,8 @@ struct Service::CreateOwner
owner.description = value.as_s
when "name"
owner.name = value.as_s
when "email"
owner.email = value.as_s
when "websiteUrl"
owner.website_url = value.as_s
else
owner.extra[key] = value
owner.extra[key.underscore] = value
end
end
end
Expand Down

0 comments on commit dce625d

Please sign in to comment.