Skip to content

Instantly share code, notes, and snippets.

@rebolek
Created March 5, 2017 05:49
Show Gist options
  • Save rebolek/46a0e1978b864e36675442e4d4dd3c19 to your computer and use it in GitHub Desktop.
Save rebolek/46a0e1978b864e36675442e4d4dd3c19 to your computer and use it in GitHub Desktop.
testing gist desc
Red []
do %json.red
github: context [
; --- internal support functions
decode: function [data] [
first json/decode third data
]
map: function [
"Make map with reduce/no-set emulation"
data
] [
value: none
parse data [
some [
change set value set-word! (reduce ['quote value])
| skip
]
]
make map! probe reduce data
]
json-map: func [
"Return JSON object from specs"
data
] [
json/encode map data
]
; --- send
send: function [
data
"Send GET request to Github API"
/post "Send POST request"
post-data
/put
put-data
] [
type: case [
post ['POST]
put ['PUT]
true ['GET]
]
;
value: none
link: copy https://api.github.com/
args-rule: [
'? (change back tail link #"?")
some [
set value set-word! (append link rejoin [form value #"="])
set value [word! | string! | integer!] (
if word? value [value: get :value]
append link rejoin [value #"&"]
)
]
]
parse append clear [] data [
some [
args-rule
| set value [set-word! | file!] (append link dirize form value)
| set value word! (append link dirize get :value)
]
]
remove back tail link
header: compose/deep [
(type) [
Accept: "application/vnd.github.v3+json"
Authorization: (rejoin ["Basic " enbase rejoin [user #":" pass]])
]
]
if any [post put] [
insert last header [Content-Type: "application/json"]
append header json/encode any [post-data put-data]
]
decode probe write/info probe link probe header
]
; ---------------------------------
user: none
pass: none
login: func [
username
password
] [
user: username
pass: password
]
; ---------------------------------
get-user: function [
user "User's name"
] [
send [%users name]
]
get-repos: function [
user
] [
send [%users user %repos]
]
comment {
USAGE:
make-gist %my-script.red "Super thing" ; loads %my-script.red
}
make-gist: func [
data "Filename, or block of filenames"
description "Gist description"
/private "Should Gist be created as private?"
] [
unless block? data [data: reduce [data]]
files: make map! length? data
foreach value data [
files/(form value): make map! reduce [quote content: read value]
; TODO: check for file existance, read/binary ?
]
gist: make map! reduce [
quote description: "testing gist desc"
quote files: files
quote public: true
]
probe
send-github/post [%gists] gist
]
; --- end of context
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment