Class: Sweetie::Bitbucket

Inherits:
Object
  • Object
show all
Extended by:
Helper
Defined in:
lib/sweetie/bitbucket.rb

Constant Summary collapse

@@config =
"_config.yml"

Class Method Summary collapse

Methods included from Helper

check_config_and_directory_file, harvest, output_count, perform_global_search, perform_search_for_single_page, traverse, write_config

Class Method Details

.bitbucket(user) ⇒ Object

Wrapper to start all the other methods

Parameters:

  • a (user)

    valid bitbucket user



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sweetie/bitbucket.rb', line 23

def bitbucket(user)
  # json represetnation of user repositories
  json_repositories = get_repositories_json(user)

  # get the names of the repositories
  repositories_names = read_repositories(json_repositories)

  # get the hash with repository-name and last changes
  repositories_changes_hash = repositories_changes(user, repositories_names)

  # write the repository information in the _config.yml
  write_repository_changes(repositories_changes_hash)
end

.configObject

getter for class variable @@config



12
13
14
# File 'lib/sweetie/bitbucket.rb', line 12

def config
  @@config
end

.config=(config) ⇒ Object

setter for the class variable @@config



17
18
19
# File 'lib/sweetie/bitbucket.rb', line 17

def config=(config)
  @@config = config
end

.create_entry(repository_name, last_change) ⇒ Object

Create a string representation of a repository entry

Parameters:

  • string (repository_name)

    contains the name of the repository

  • string (last_change)

    contains the date of the change

Returns:

  • a string in the form “<reponame>: <date>”



134
135
136
# File 'lib/sweetie/bitbucket.rb', line 134

def create_entry(repository_name, last_change)
  "#{repository_name}: #{last_change}"
end

.get_repositories_json(user) ⇒ Object

Get the json representation of each repository of the user

Parameters:

  • a (user)

    valid bitbucket user

Returns:

  • json representation of all bitbucket repositories



92
93
94
# File 'lib/sweetie/bitbucket.rb', line 92

def get_repositories_json(user)
  `curl -s https://api.bitbucket.org/1.0/users/#{user}/`
end

.parse_json(file) ⇒ Object

Parse JSON file to format be read by ruby



77
78
79
# File 'lib/sweetie/bitbucket.rb', line 77

def parse_json(file)
  JSON.parse file
end

.parse_timestamp(timestamp) ⇒ "yyyy-mm-dd"

Parse timestamp to display only the date (“2011-04-20 11:31:39” will be converted to “2011-04-20”)

Parameters:

  • a (timestamp)

    time display as a string in the ‘Time.now’ format

Returns:

  • ("yyyy-mm-dd")

    yyyy-mm-dd“



99
100
101
102
# File 'lib/sweetie/bitbucket.rb', line 99

def parse_timestamp(timestamp)
  regex = Regexp.new(/(\d+)-(\d+)-(\d+)/)
  regex.match(timestamp)[0]
end

.read_repositories(json_repositories) ⇒ Object

names of all repositories of a user

Parameters:

  • repositories (json_repositories)

    in json format

Returns:

  • array with the names of all repositories of the user



40
41
42
43
44
45
46
47
48
49
# File 'lib/sweetie/bitbucket.rb', line 40

def read_repositories(json_repositories)
  repository_hash = parse_json(json_repositories)
  repositories_names = []

  repository_hash["repositories"].each do |repo|
    repositories_names << repo["slug"]
  end

  repositories_names
end

.repositories_changes(user, repositories) ⇒ Object

Get the last changes for each repository and the name

Parameters:

  • a (user)

    valid bitbucket user

  • array (repositories)

    with names of all repositories of a user

Returns:

  • hash with all the repositories of the user and the last changes



55
56
57
58
59
60
61
62
63
# File 'lib/sweetie/bitbucket.rb', line 55

def repositories_changes(user, repositories)
  repositories_lastmodifications = {}
  repositories.each do |repository|
    changeset = repository_changeset(user, repository)
    repositories_lastmodifications.merge(repository_last_modification(repository, changeset))
  end

  repositories_lastmodifications
end

.repository_changeset(user, repository) ⇒ Object

Get the changeset of a repository

Parameters:

  • a (user)

    valid bitbucket user

  • a (repository)

    repository of a user

Returns:

  • information about the repository in json



85
86
87
# File 'lib/sweetie/bitbucket.rb', line 85

def repository_changeset(user, repository)
  `curl -s https://api.bitbucket.org/1.0/repositories/#{user}/#{repository}/changesets/`
end

.repository_last_modification(repository_name, repository_json) ⇒ Object

Get the last modification of a repository

Parameters:

  • a (repository_name)

    valid repository name

  • json (repository_json)

    representation of the repository



68
69
70
71
72
73
74
# File 'lib/sweetie/bitbucket.rb', line 68

def repository_last_modification(repository_name, repository_json)
  changeset_parsed = parse_json(repository_json)
  timestamp = changeset_parsed["changesets"].last["timestamp"]

  # create the hash
  {repository_name => parse_timestamp(timestamp)}
end

.write_repository_changes(repositories) ⇒ Object

Writes the changes into the _config.yml

Parameters:

  • a (repositories)

    hash with the following scheme => “2011-10-26”, “pmwiki” => “2011-10-26”



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sweetie/bitbucket.rb', line 106

def write_repository_changes(repositories)
  repositories.each do |repository, last_change|
    file = File.open(@@config)
    text = ""
    match = false
    while line = file.gets
      if line.match(/#{repository}/)
        match = true
        # create string and replace this line with the new changes
        text << create_entry(repository, last_change) + "\n"
      else
        text << line
      end
    end

    # append the repository if it is not in there
    text << create_entry(repository, last_change) unless match

    file.close

    write_config(@@config, text)
  end
end