Passed
Pull Request — master (#2)
by Victor Hugo
01:45
created

devto.*WebURL.UnmarshalJSON   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
1
package devto
2
3
import (
4
	"context"
5
	"net/url"
6
	"strings"
7
	"time"
8
)
9
10
//User contains information about a devto account
11
type User struct {
12
	Name            string  `json:"name,omitempty"`
13
	Username        string  `json:"username,omitempty"`
14
	TwitterUsername string  `json:"twitter_username,omitempty"`
15
	GithubUsername  string  `json:"github_username,omitempty"`
16
	WebsiteURL      *WebURL `json:"website_url,omitempty"`
17
	ProfileImage    *WebURL `json:"profile_image,omitempty"`
18
	ProfileImage90  *WebURL `json:"profile_image_90,omitempty"`
19
}
20
21
//Organization describes a company or group that
22
//publishes content to devto.
23
type Organization struct {
24
	Name           string  `json:"name,omitempty"`
25
	Username       string  `json:"username,omitempty"`
26
	Slug           string  `json:"slug,omitempty"`
27
	ProfileImage   *WebURL `json:"profile_image,omitempty"`
28
	ProfileImage90 *WebURL `json:"profile_image_90,omitempty"`
29
}
30
31
//Tags are a group of topics related to an article
32
type Tags []string
33
34
//Article contains all the information related to a single
35
//information resource from devto.
36
type Article struct {
37
	TypeOf                 string       `json:"type_of,omitempty"`
38
	ID                     uint32       `json:"id,omitempty"`
39
	Title                  string       `json:"title,omitempty"`
40
	Description            string       `json:"description,omitempty"`
41
	CoverImage             *WebURL      `json:"cover_image,omitempty"`
42
	SocialImage            *WebURL      `json:"social_image,omitempty"`
43
	PublishedAt            *time.Time   `json:"published_at,omitempty"`
44
	EditedAt               *time.Time   `json:"edited_at,omitempty"`
45
	CrossPostedAt          *time.Time   `json:"crossposted_at,omitempty"`
46
	LastCommentAt          *time.Time   `json:"last_comment_at,omitempty"`
47
	TagList                Tags         `json:"tag_list,omitempty"`
48
	Tags                   string       `json:"tags,omitempty"`
49
	Slug                   string       `json:"slug,omitempty"`
50
	Path                   *WebURL      `json:"path,omitempty"`
51
	URL                    *WebURL      `json:"url,omitempty"`
52
	CanonicalURL           *WebURL      `json:"canonical_url,omitempty"`
53
	CommentsCount          uint         `json:"comments_count,omitempty"`
54
	PositiveReactionsCount uint         `json:"positive_reactions_count,omitempty"`
55
	PublishedTimestamp     *time.Time   `json:"published_timestamp,omitempty"`
56
	User                   User         `json:"user,omitempty"`
57
	Organization           Organization `json:"organization,omitempty"`
58
	BodyHTML               string       `json:"body_html,omitempty"`
59
	BodyMarkdown           string       `json:"body_markdown,omitempty"`
60
	Published              bool         `json:"published,omitempty"`
61
}
62
63
//ArticleListOptions holds the query values to pass as
64
//query string parameter to the Articles List action.
65
type ArticleListOptions struct {
66
	Tags     string `url:"tag,omitempty"`
67
	Username string `url:"username,omitempty"`
68
	State    string `url:"state,omitempty"`
69
	Top      string `url:"top,omitempty"`
70
	Page     int    `url:"page,omitempty"`
71
}
72
73
type WebURL struct {
74
	*url.URL
75
}
76
77
//UnmarshalJSON overrides the default unmarshal behaviour
78
//for URL
79
func (s *WebURL) UnmarshalJSON(b []byte) error {
80 1
	c := string(b)
81 1
	c = strings.Trim(c, "\"")
82 1
	uri, err := url.Parse(c)
83 1
	if err != nil {
84 1
		return err
85
	}
86 1
	s.URL = uri
87 1
	return nil
88
}
89
90
//APIResource describes a RESTish endpoint
91
type APIResource interface {
92
	List(ctx context.Context, opt ArticleListOptions) ([]Article, error)
93
	Find(ctx context.Context, id uint32) (Article, error)
94
	New(ctx context.Context, a Article) (Article, error)
95
	Update(ctx context.Context, a Article) (Article, error)
96
}
97