PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test_scripts/bitbucket.rb

https://bitbucket.org/seanhealy/codecalender
Ruby | 76 lines | 52 code | 17 blank | 7 comment | 13 complexity | d945b5769fb330b73ea95502914435c9 MD5 | raw file
  1. #!/usr/bin/ruby
  2. require 'date'
  3. require 'uri'
  4. require 'net/https'
  5. require 'rubygems'
  6. require 'json'
  7. require 'pp'
  8. HOST = 'https://api.bitbucket.org'
  9. OTHER_HOST = 'https://www.bitbucket.org'
  10. USER_PATH = '/1.0/users/'
  11. USER = 'gamblore'
  12. $now_is_the_time = Time.now
  13. def fetch_json_from_string(url_string)
  14. print "Fetching #{url_string}\n\n"
  15. url = URI.parse(url_string)
  16. http_session = Net::HTTP.new(url.host, url.port)
  17. req = Net::HTTP::Get.new(url.path)
  18. http_session.use_ssl = true if url.port == 443
  19. res = http_session.start { |http|
  20. http.request(req)
  21. }
  22. results = JSON.parse( res.body )
  23. return results
  24. end
  25. def repository_active_today(repo)
  26. results = fetch_json_from_string(HOST + repo["resource_uri"].sub(/\/api/, '') + '/changesets/')
  27. latest = results["changesets"].each { |cs|
  28. if ( cs["author"] == USER ) then
  29. =begin
  30. print "Our users changeset\n==========\n"
  31. pp cs
  32. print "==========\n"
  33. =end
  34. time_of_changeset = Date.parse(cs["timestamp"])
  35. if (time_of_changeset.day == $now_is_the_time.day and
  36. time_of_changeset.month == $now_is_the_time.month and
  37. time_of_changeset.year == $now_is_the_time.year) then
  38. return true
  39. end
  40. if (time_of_changeset.day < $now_is_the_time.day and
  41. time_of_changeset.month == $now_is_the_time.month and
  42. time_of_changeset.year == $now_is_the_time.year) then
  43. return nil
  44. end
  45. end
  46. }
  47. return nil
  48. end
  49. def pull_user(user)
  50. results = fetch_json_from_string(HOST + USER_PATH + user)
  51. #pp results
  52. results["repositories"].each { |repo|
  53. print "Checking if #{repo["name"]} is active for user #{USER}\n"
  54. if (repository_active_today(repo) ) then
  55. print "-> #{repo["name"]} is active for user #{USER}."
  56. end
  57. }
  58. end
  59. pull_user(USER)