Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Import Content #162

Closed
richranallo opened this issue Jul 31, 2016 · 2 comments
Closed

Feature Request: Import Content #162

richranallo opened this issue Jul 31, 2016 · 2 comments

Comments

@richranallo
Copy link

It would be really great if it were possible to send Markdown content to a Homebrewery URL and have it pulled into a new brew automatically. I'm working on a project that I'd love to be able to include a one-touch "Port to Homebrewery" link on so people can get nice rulebook-type formatting on their content.

It's possible right now to just download as a Markdown file that can be pasted in, but that's a few more steps for the user.

@tarper24
Copy link

tarper24 commented Aug 6, 2016

I did some testing around with Google Chrome and Postman for Chrome. I found that this already is possible with the current API: https://github.com/stolksdorf/homebrewery/blob/master/server/homebrew.api.js

Using POST you can create a new brew by calling /api

app.post('/api', function(req, res){
    var newHomebrew = new HomebrewModel(req.body);
    newHomebrew.save(function(err, obj){
        if(err){
            console.error(err, err.toString(), err.stack);
            return res.status(500).send("Error while creating new brew");
        }
        return res.json(obj);
    })
});

POST http://homebrewery.naturalcrit.com/api/

Doing so will return something similar to this:

{
  "__v": 0,
  "_id": "57a66c37eef47c110086f9c0",
  "views": 0,
  "lastViewed": "2016-08-06T23:01:11.303Z",
  "updatedAt": "2016-08-06T23:01:11.303Z",
  "createdAt": "2016-08-06T23:01:11.303Z",
  "text": "",
  "title": "",
  "editId": "B1GkHhyVt",
  "shareId": "SJ-Jr2JEY"
}

Currently everything about that brew is black, including the title. Now you'll want to pull the editID from what it returns, and use that to PUT the content into that brew.

app.put('/api/update/:id', function(req, res){
    HomebrewModel.find({editId : req.params.id}, function(err, objs){
        if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
        var resEntry = objs[0];
        resEntry.text = req.body.text;
        resEntry.title = req.body.title;
        resEntry.updatedAt = new Date();
        resEntry.save(function(err, obj){
            if(err) return res.status(500).send("Error while saving");
            return res.status(200).send(obj);
        })
    });
});
PUT http://homebrewery.naturalcrit.com/api/update/B1GkHhyVt

Headers:
Content-Type: application/json

Body:

{"text":"# Homebrewery import\n\nTesting forced import on Homebrewery","title":"Test"}

This is all from my testing, hope it helped!

print(closing);
print("~tarper24");

@stolksdorf
Copy link
Collaborator

stolksdorf commented Aug 7, 2016

It's actually just one step, you can pass your brew along with the initial POST.

Using the request library superagent

var request = require('superagent');
result.post('http://homebrewery.naturalcrit.com/api')
  .send({
    text : 'Your **brew**markdown goes here'
  })
  .end(function(err, res){
    //You can link your user to their brew by using the returned editid
    console.log(res.body.editId);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants