Class: Sweetie::BitbucketStatiHelper
- Inherits:
-
Object
- Object
- Sweetie::BitbucketStatiHelper
- Includes:
- Helper
- Defined in:
- lib/sweetie/bitbucket_stati_helper.rb
Overview
The class which do the actuall work to get and write the repositories updates in the config file
Instance Attribute Summary collapse
-
#config ⇒ Object
writeonly
Sets the attribute config.
-
#user ⇒ Object
writeonly
Sets the attribute user.
Instance Method Summary collapse
-
#entry_text_jekyll(name, updated) ⇒ String
Create a string representation of a repository entry in jekyll style.
-
#entry_text_middleman(name, updated) ⇒ String
Create a string representation of a repository entry in middleman style.
-
#get_repositories ⇒ Object
Fire up curl request to bitbucket.
-
#get_repositories_changes(json_repositories) ⇒ Hash
Grab for each repository the recent update.
-
#initialize(config = '_config.yml', user = '') ⇒ nil
constructor
A basic initialize method.
-
#parse_json(json) ⇒ Object
Wrapper for calling the json_parsing from JSON library.
-
#parse_timestamp(timestamp) ⇒ String
Parse a timestamp.
-
#write_repository_changes(repositories) ⇒ nil
Parse a hash and write its key/value pairs in a config file for jekyll and middleman projects.
Methods included from Helper
#check_directory_and_config_file, #harvest, #output_count, #perform_global_search, #perform_search_for_single_page, #traverse, #write_config
Constructor Details
#initialize(config = '_config.yml', user = '') ⇒ nil
A basic initialize method.
17 18 19 20 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 17 def initialize(config = '_config.yml', user = '') @config = config @user = user end |
Instance Attribute Details
#config=(value) ⇒ Object (writeonly)
Sets the attribute config
10 11 12 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 10 def config=(value) @config = value end |
#user=(value) ⇒ Object (writeonly)
Sets the attribute user
10 11 12 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 10 def user=(value) @user = value end |
Instance Method Details
#entry_text_jekyll(name, updated) ⇒ String
Create a string representation of a repository entry in jekyll style
Example:
entry_text_middleman({"pmwiki" => "2011-10-26"}
# => "pmwiki: 2011-10-26"
139 140 141 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 139 def entry_text_jekyll(name, updated) "#{name}: #{updated}" end |
#entry_text_middleman(name, updated) ⇒ String
Create a string representation of a repository entry in middleman style
Example:
entry_text_middleman({"pmwiki" => "2011-10-26"}
# => "set :pmwiki 2011-10-26"
125 126 127 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 125 def entry_text_middleman(name, updated) "set :#{name}, #{updated}" end |
#get_repositories ⇒ Object
Fire up curl request to bitbucket
Example:
bitbucket('wikimatze')
# =>
{
"pagelen":10,
"values":[
{
"scm":"git",
"website":"",
"has_wiki":false,
"name":"pmwiki-linkicons-recipe",
"links":{
"watchers":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/watchers"
},
"branches":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/refs/branches"
},
"tags":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/refs/tags"
},
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/commits"
},
"clone":[
{
"href":"https://bitbucket.org/wikimatze/pmwiki-linkicons-recipe.git",
"name":"https"
},
{
"href":"ssh://[email protected]/wikimatze/pmwiki-linkicons-recipe.git",
"name":"ssh"
}
],
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe"
},
"html":{
"href":"https://bitbucket.org/wikimatze/pmwiki-linkicons-recipe"
},
"avatar":{
"href":"https://bitbucket.org/wikimatze/pmwiki-linkicons-recipe/avatar/32/"
},
"hooks":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/hooks"
},
"forks":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/forks"
},
"downloads":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/downloads"
},
"pullrequests":{
"href":"https://api.bitbucket.org/2.0/repositories/wikimatze/pmwiki-linkicons-recipe/pullrequests"
}
},
"fork_policy":"allow_forks",
"uuid":"{182b19dc-334d-40d7-a65d-a85aaeb46f31}",
"language":"",
"created_on":"2011-10-07T21:01:39.266805+00:00",
"mainbranch":{
"type":"branch",
"name":"master"
},
"full_name":"wikimatze/pmwiki-linkicons-recipe",
"has_issues":false,
"owner":{
"username":"wikimatze",
"display_name":"Matthias Günther",
"type":"user",
"uuid":"{5eab05fd-a541-43c5-a0e1-5bc9b2e2005f}",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/wikimatze"
},
"html":{
"href":"https://bitbucket.org/wikimatze/"
},
"avatar":{
"href":"https://bitbucket.org/account/wikimatze/avatar/32/"
}
}
},
"updated_on":"2017-02-26T07:09:52.048642+00:00",
"size":431470,
"type":"repository",
"slug":"pmwiki-linkicons-recipe",
"is_private":false,
"description":""
},
...
],
"page":1,
"size":9
}
242 243 244 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 242 def get_repositories `curl -s https://api.bitbucket.org/2.0/repositories/#{@user}/` end |
#get_repositories_changes(json_repositories) ⇒ Hash
Grab for each repository the recent update
Example:
get_repositories_changes(wikimatze_json)
# => {"pmwiki-headlineimage-recipe"=>"2011-10-29", "pmwiki-linkicons-recipe"=>"2011-10-29"}
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 32 def get_repositories_changes(json_repositories) repository_hash = parse_json(json_repositories) repositories_changsets = {} repository_hash['values'].each do |repository| repository_name = repository['name'] repository_last_updated = (repository['updated_on']) repositories_changsets.merge!(repository_name => repository_last_updated) end repositories_changsets end |
#parse_json(json) ⇒ Object
Wrapper for calling the json_parsing from JSON library
Example:
json = {
"user": {
"username": "wikimatze",
"first_name": "Matthias",
"last_name": "Guenther",
"resource_uri": "/1.0/users/wikimatze"
}
}
parse_json(json)
=> {"user"=>{"username"=>"wikimatze", "first_name"=>"Matthias", "last_name"=>"Guenther", "resource_uri"=>"/1.0/users/wikimatze"}}
63 64 65 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 63 def parse_json(json) JSON.parse(json) end |
#parse_timestamp(timestamp) ⇒ String
Parse a timestamp
Example:
("2011-04-20 11:31:39")
# => 2011-04-20
110 111 112 113 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 110 def () regex = Regexp.new(/(\d+)-(\d+)-(\d+)/) regex.match()[0] end |
#write_repository_changes(repositories) ⇒ nil
Parse a hash and write its key/value pairs in a config file for jekyll and middleman projects
Example:
write_repository_changes({"svn" => "2011-10-26", "pmwiki" => "2011-10-26"})
# => svn: 2011-10-26\npmwiki: 2011-10-26
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 76 def write_repository_changes(repositories) repositories.each do |name, last_updated| file = File.open(@config) text = '' project = 'middleman' project = 'jekyll' if File.extname(file) =~ /.yml/ name = name.to_s.tr('-', '_') while line = file.gets text << if line =~ /#{name}/ && project == 'middleman' entry_text_middleman(name, last_updated) + "\n" elsif line =~ /#{name}/ && project == 'jekyll' entry_text_jekyll(name, last_updated) + "\n" else line end end file.close write_config(@config, text) end end |