Class: Sweetie::Bitbucket
- Inherits:
-
Object
- Object
- Sweetie::Bitbucket
- Extended by:
- Helper
- Defined in:
- lib/sweetie/bitbucket.rb
Constant Summary collapse
- @@config =
"_config.yml"
Class Method Summary collapse
-
.bitbucket(user) ⇒ Object
Wrapper to start all the other methods.
-
.config ⇒ Object
getter for class variable @@config.
-
.config=(config) ⇒ Object
setter for the class variable @@config.
-
.create_entry(repository_name, last_change) ⇒ Object
Create a string representation of a repository entry.
-
.get_repositories_json(user) ⇒ Object
Get the json representation of each repository of the user.
-
.parse_json(file) ⇒ Object
Parse JSON file to format be read by ruby.
-
.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”).
-
.read_repositories(json_repositories) ⇒ Object
names of all repositories of a user.
-
.repositories_changes(user, repositories) ⇒ Object
Get the last changes for each repository and the name.
-
.repository_changeset(user, repository) ⇒ Object
Get the changeset of a repository.
-
.repository_last_modification(repository_name, repository_json) ⇒ Object
Get the last modification of a repository.
-
.write_repository_changes(repositories) ⇒ Object
Writes the changes into the _config.yml.
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
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 |
.config ⇒ Object
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
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
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”)
99 100 101 102 |
# File 'lib/sweetie/bitbucket.rb', line 99 def () regex = Regexp.new(/(\d+)-(\d+)-(\d+)/) regex.match()[0] end |
.read_repositories(json_repositories) ⇒ Object
names of all repositories of a 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
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
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
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) = changeset_parsed["changesets"].last["timestamp"] # create the hash {repository_name => ()} end |
.write_repository_changes(repositories) ⇒ Object
Writes the changes into the _config.yml
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 |