Skip to content

Commit

Permalink
Adding ProfileImage generator to generate profile pictures (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr authored May 3, 2021
1 parent e35c676 commit f2768c9
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
5 changes: 5 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ func (f Faker) LoremFlickr() LoremFlickr {
return LoremFlickr{&f}
}

// ProfileImage returns a fake ProfileImage instance for Faker
func (f Faker) ProfileImage() ProfileImage {
return ProfileImage{&f}
}

// New returns a new instance of Faker instance with a random seed
func New() (f Faker) {
seed := rand.NewSource(time.Now().Unix())
Expand Down
6 changes: 3 additions & 3 deletions loremflickr.go → lorem_flickr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"strconv"
)

const BASE_URL = "https://loremflickr.com"
const LOREM_FLICKR_BASE_URL = "https://loremflickr.com"

// LoremFlickr is a faker struct for LoremFlickr
type LoremFlickr struct {
faker *Faker
}

// Image generates a *os.File with a random image using the loremflickre.com service
// Image generates a *os.File with a random image using the loremflickr.com service
func (lf LoremFlickr) Image(width, height int, categories []string, prefix string, categoriesStrict bool) *os.File {

url := BASE_URL
url := LOREM_FLICKR_BASE_URL

switch prefix {
case "g":
Expand Down
2 changes: 1 addition & 1 deletion loremflickr_test.go → lorem_flickr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestImageLorem(t *testing.T) {
func TestLoremFlickrImage(t *testing.T) {
f := New()
value := f.LoremFlickr().Image(300, 200, []string{}, "", false)
Expect(t, fmt.Sprintf("%T", value), "*os.File")
Expand Down
6 changes: 6 additions & 0 deletions person.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package faker

import (
"fmt"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -246,3 +247,8 @@ func (p Person) Contact() ContactInfo {
Email: p.Faker.Internet().Email(),
}
}

// Image return the person profile image
func (p Person) Image() *os.File {
return p.Faker.ProfileImage().Image()
}
8 changes: 8 additions & 0 deletions person_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faker

import (
"fmt"
"strings"
"testing"
)
Expand Down Expand Up @@ -97,3 +98,10 @@ func TestContact(t *testing.T) {
Expect(t, true, len(contact.Phone) > 0)
Expect(t, true, len(contact.Email) > 0)
}

func TestPersonImage(t *testing.T) {
p := New().Person()
image := p.Image()
Expect(t, fmt.Sprintf("%T", image), "*os.File")
Expect(t, strings.HasSuffix(image.Name(), ".jfif"), true, image.Name())
}
34 changes: 34 additions & 0 deletions profile_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package faker

import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
)

const PROFILE_IMAGE_BASE_URL = "https://thispersondoesnotexist.com/image"

// ProfileImage is a faker struct for ProfileImage
type ProfileImage struct {
faker *Faker
}

// Image generates a *os.File with a random profile image using the thispersondoesnotexist.com service
func (pi ProfileImage) Image() *os.File {
resp, err := http.Get(PROFILE_IMAGE_BASE_URL)
if err != nil {
log.Println("Error while requesting", PROFILE_IMAGE_BASE_URL, ":", err)
}

defer resp.Body.Close()

f, err := ioutil.TempFile(os.TempDir(), "profil-picture-img-*.jfif")
if err != nil {
panic(err)
}

io.Copy(f, resp.Body)
return f
}
14 changes: 14 additions & 0 deletions profile_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package faker

import (
"fmt"
"strings"
"testing"
)

func TestProfileImage(t *testing.T) {
f := New()
value := f.ProfileImage().Image()
Expect(t, fmt.Sprintf("%T", value), "*os.File")
Expect(t, strings.HasSuffix(value.Name(), ".jfif"), true, value.Name())
}

0 comments on commit f2768c9

Please sign in to comment.