0

i'm trying to save external values from BitBucket to my Rails app and then return them in view(i'm new with databases), so if somebody can help me with that, i'll really appreciate it.

I returned API call from BitBucket in my controller so its works well.. Then i create model bitbucket name:string message:text date:integer

So now i place this into bitbucket.rb model:

class Bitbucket < ActiveRecord::Base
    def saveData
        require 'bitbucket_rest_api'   
          bitbucket = BitBucket.new login:'...', password:'...'

          repo = bitbucket.repos.commits.list '...', '...'
          data = repo["values"]

            data.each do |r|
                r["author"]["user"]["display_name"]
                r["message"]
                DateTime.strptime(r["date"], '%Y-%m-%dT%H:%M:%S%z').strftime("%D %r")
            end
    end
end

This is the first call what i want to save into DB then i would like to save every new commit into database.

Thanks a lot.

1 Answer 1

2
  1. You should name methods in Ruby like this: save_data, not saveData to anything else.

  2. Your method should be class-method since you are not using any object inside of it.

  3. You can create database entry with create or new, save chain.

This might help:

class Bitbucket < ActiveRecord::Base
  def self.save_data
    require 'bitbucket_rest_api'   
    bitbucket = BitBucket.new login:'...', password:'...'
    repo = bitbucket.repos.commits.list '...', '...'
    repo["values"].each do |r|
      create(
        name: r["author"]["user"]["display_name"],
        message: r["message"],
        date: DateTime.strptime(r["date"], '%Y-%m-%dT%H:%M:%S%z').strftime("%D %r")
      )
    end
  end
end
Sign up to request clarification or add additional context in comments.

11 Comments

And Controller gonna be like @commits = Bitbucket.all ? Then in view <%= @bitbucket.name %> etc.. ?
@LiborZahrádka @commits.name For example.
Yeah sorry wrong variable.. i did a mistake.. ok thanks a lot
bitbucket.rb:9: syntax error, unexpected tIDENTIFIER, expecting ')' message: r["message"] and bitbucket.rb:10: syntax error, unexpected ':', expecting keyword_end date: DateTime.strptime(r["date"],... bitbucket.rb:11: syntax error, unexpected ')', expecting keyword_end Im getting this error right now
@LiborZahrádka Updated my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.