class GitPunch::Song

Attributes

attributes[RW]
is_fork[RW]
is_fork?[RW]
original_song_id[RW]
original_user[RW]

Public Class Methods

fork(with_options={}) click to toggle source
# File lib/git_punch/song.rb, line 15
def self.fork with_options={}
  new with_options.merge \
    is_fork: true,
    original_user: User.from_origin.name
end
new(with_options={}) click to toggle source
# File lib/git_punch/song.rb, line 7
def initialize with_options={}
  @attributes = with_options.reduce({}) do |attrs,opt,val|
    send "#{opt}=", val if respond_to? opt
    attrs.merge opt => val
  end

end

Public Instance Methods

errors() click to toggle source
# File lib/git_punch/song.rb, line 30
def errors
  @errors ||= ClientErrors.new
end
valid?() click to toggle source
# File lib/git_punch/song.rb, line 21
def valid?
  if is_fork? && original_user.present?
    fork_url = "/songs/#{original_song_id}/fork"
    create_repository_at fork_url
  else
    create_repository_at "/songs"
  end
end

Private Instance Methods

create_repository_at(url) click to toggle source
# File lib/git_punch/song.rb, line 35
def create_repository_at url
  request = note_punch.post url, attrbutes
  response = JSON.parse request.body
  status = request.status

  unless status == '200'
    errors << response['errors']
  end

  errors.add JSON.parse(response.body)['errors'] \
    unless response.status == '200'
end
note_punch() click to toggle source
# File lib/git_punch/song.rb, line 48
def note_punch
  @client ||= Client.new
end