-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ProfileImage generator to generate profile pictures (#55)
- Loading branch information
Showing
7 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |