Class: Sweetie::BitbucketStatiHelper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • config (String) (defaults to: '_config.yml')

    The config file in which the changes should be written

  • user (String) (defaults to: '')

    The name of the user for which the updates should be fetched



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

Parameters:

  • value

    the value to set the attribute config to.



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

Parameters:

  • value

    the value to set the attribute user to.



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"

Parameters:

  • name (String)

    A string containing the name of the repository

  • updated (String)

    A string containing the date of the last change of the repo

Returns:

  • (String)

    Return a string in the form “set :<name> :<last_updated>”



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"

Parameters:

  • name (String)

    A string containing the name of the repository

  • updated (String)

    A string containing the date of the last change of the repo

Returns:

  • (String)

    Return a string in the form “set :<name> :<last_updated>”



125
126
127
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 125

def entry_text_middleman(name, updated)
  "set :#{name}, #{updated}"
end

#get_repositoriesObject

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"}

Parameters:

  • json_repositories (String)

    : A json object of the bitbucket API response

Returns:

  • (Hash)

    Returns a hash of the form => <last_updated>



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 = parse_timestamp(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"}}

Parameters:

  • json (String)

    : A JSON format from the bitbucket API response

Returns:

  • Returns Parse JSON file as a hash 2017-06-18



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:

parse_timestamp("2011-04-20 11:31:39")
# => 2011-04-20

Parameters:

  • timestamp (String)

    A string in the form 2011-04-20 11:31:39

Returns:

  • (String)

    A string in the format “yyyy-mm-dd”



110
111
112
113
# File 'lib/sweetie/bitbucket_stati_helper.rb', line 110

def parse_timestamp(timestamp)
  regex = Regexp.new(/(\d+)-(\d+)-(\d+)/)
  regex.match(timestamp)[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

Parameters:

  • repositories (Hash)
    • A hash in the form => <last_updated

Returns:

  • (nil)

    Writes the of the repository hash specified _config.yml by the given @config variable



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