schedule-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.byebug_history +13 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +44 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +88 -0
- data/README.md +35 -0
- data/Rakefile +4 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/schedule +5 -0
- data/lib/schedule/availability_range.rb +59 -0
- data/lib/schedule/calendar.rb +120 -0
- data/lib/schedule/cli.rb +71 -0
- data/lib/schedule/config.rb +48 -0
- data/lib/schedule/version.rb +5 -0
- data/lib/schedule.rb +4 -0
- data/schedule-cli.gemspec +35 -0
- metadata +187 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 42782ff951ba926c55abecb022b485b9714a73a10b88d8edaf76fbc756cde429
         | 
| 4 | 
            +
              data.tar.gz: b8e2f32fca289565b1852c6575da834936e24ce0be208610f5a504f19cb607ba
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: da46ba9b924e8315b27a49d896dc744f3237e4c7389cecbbbfdb97116a84235f61e2c944f99c9df5cb4d5459a215829f2ed4b0f1933cd3169cefe9d0b2b95cbb
         | 
| 7 | 
            +
              data.tar.gz: ef0a550eb9c6805ddc53cbfd874b7993d80f29c25fa1cba91cc6b7c71f2d465f29c108543efa04e40d53a895c0ae3f186f6e8dff8760ab43351050766ffb4589
         | 
    
        data/.byebug_history
    ADDED
    
    
    
        data/.gitignore
    ADDED
    
    
    
        data/.rubocop.yml
    ADDED
    
    | @@ -0,0 +1,44 @@ | |
| 1 | 
            +
            AllCops:
         | 
| 2 | 
            +
              TargetRubyVersion: 2.5.1
         | 
| 3 | 
            +
              Exclude:
         | 
| 4 | 
            +
                - bin/*
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Metrics/LineLength:
         | 
| 7 | 
            +
              Max: 120
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Metrics/BlockLength:
         | 
| 10 | 
            +
              ExcludedMethods:
         | 
| 11 | 
            +
                - class_methods
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            Metrics/MethodLength:
         | 
| 14 | 
            +
              Max: 25
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            Metrics/ClassLength:
         | 
| 17 | 
            +
              Max: 1500
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Metrics/AbcSize:
         | 
| 20 | 
            +
              Enabled: false
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            Metrics/CyclomaticComplexity:
         | 
| 23 | 
            +
              Enabled: false
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            Style/SymbolArray:
         | 
| 26 | 
            +
              Enabled: false
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            Style/Documentation:
         | 
| 29 | 
            +
              Enabled: false
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            Style/TrailingCommaInHashLiteral:
         | 
| 32 | 
            +
              EnforcedStyleForMultiline: comma
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            Style/TrailingCommaInArguments:
         | 
| 35 | 
            +
              EnforcedStyleForMultiline: comma
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            Style/TrailingCommaInArrayLiteral:
         | 
| 38 | 
            +
              EnforcedStyleForMultiline: comma
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            Lint/ParenthesesAsGroupedExpression:
         | 
| 41 | 
            +
              Enabled: false
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            Layout/EndOfLine:
         | 
| 44 | 
            +
              EnforcedStyle: lf
         | 
    
        data/Gemfile
    ADDED
    
    
    
        data/Gemfile.lock
    ADDED
    
    | @@ -0,0 +1,88 @@ | |
| 1 | 
            +
            PATH
         | 
| 2 | 
            +
              remote: .
         | 
| 3 | 
            +
              specs:
         | 
| 4 | 
            +
                schedule-cli (0.1.0)
         | 
| 5 | 
            +
                  activesupport (~> 5.2)
         | 
| 6 | 
            +
                  chronic
         | 
| 7 | 
            +
                  google-api-client (~> 0.11)
         | 
| 8 | 
            +
                  launchy
         | 
| 9 | 
            +
                  nokogiri
         | 
| 10 | 
            +
                  thor
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            GEM
         | 
| 13 | 
            +
              remote: https://rubygems.org/
         | 
| 14 | 
            +
              specs:
         | 
| 15 | 
            +
                activesupport (5.2.0)
         | 
| 16 | 
            +
                  concurrent-ruby (~> 1.0, >= 1.0.2)
         | 
| 17 | 
            +
                  i18n (>= 0.7, < 2)
         | 
| 18 | 
            +
                  minitest (~> 5.1)
         | 
| 19 | 
            +
                  tzinfo (~> 1.1)
         | 
| 20 | 
            +
                addressable (2.5.2)
         | 
| 21 | 
            +
                  public_suffix (>= 2.0.2, < 4.0)
         | 
| 22 | 
            +
                byebug (10.0.2)
         | 
| 23 | 
            +
                chronic (0.10.2)
         | 
| 24 | 
            +
                concurrent-ruby (1.0.5)
         | 
| 25 | 
            +
                declarative (0.0.10)
         | 
| 26 | 
            +
                declarative-option (0.1.0)
         | 
| 27 | 
            +
                faraday (0.15.2)
         | 
| 28 | 
            +
                  multipart-post (>= 1.2, < 3)
         | 
| 29 | 
            +
                google-api-client (0.23.4)
         | 
| 30 | 
            +
                  addressable (~> 2.5, >= 2.5.1)
         | 
| 31 | 
            +
                  googleauth (>= 0.5, < 0.7.0)
         | 
| 32 | 
            +
                  httpclient (>= 2.8.1, < 3.0)
         | 
| 33 | 
            +
                  mime-types (~> 3.0)
         | 
| 34 | 
            +
                  representable (~> 3.0)
         | 
| 35 | 
            +
                  retriable (>= 2.0, < 4.0)
         | 
| 36 | 
            +
                googleauth (0.6.3)
         | 
| 37 | 
            +
                  faraday (~> 0.12)
         | 
| 38 | 
            +
                  jwt (>= 1.4, < 3.0)
         | 
| 39 | 
            +
                  memoist (~> 0.12)
         | 
| 40 | 
            +
                  multi_json (~> 1.11)
         | 
| 41 | 
            +
                  os (>= 0.9, < 2.0)
         | 
| 42 | 
            +
                  signet (~> 0.7)
         | 
| 43 | 
            +
                httpclient (2.8.3)
         | 
| 44 | 
            +
                i18n (1.0.1)
         | 
| 45 | 
            +
                  concurrent-ruby (~> 1.0)
         | 
| 46 | 
            +
                jwt (2.1.0)
         | 
| 47 | 
            +
                launchy (2.4.3)
         | 
| 48 | 
            +
                  addressable (~> 2.3)
         | 
| 49 | 
            +
                memoist (0.16.0)
         | 
| 50 | 
            +
                mime-types (3.1)
         | 
| 51 | 
            +
                  mime-types-data (~> 3.2015)
         | 
| 52 | 
            +
                mime-types-data (3.2016.0521)
         | 
| 53 | 
            +
                mini_portile2 (2.3.0)
         | 
| 54 | 
            +
                minitest (5.11.3)
         | 
| 55 | 
            +
                multi_json (1.13.1)
         | 
| 56 | 
            +
                multipart-post (2.0.0)
         | 
| 57 | 
            +
                nokogiri (1.8.4)
         | 
| 58 | 
            +
                  mini_portile2 (~> 2.3.0)
         | 
| 59 | 
            +
                os (1.0.0)
         | 
| 60 | 
            +
                public_suffix (3.0.2)
         | 
| 61 | 
            +
                rake (10.5.0)
         | 
| 62 | 
            +
                representable (3.0.4)
         | 
| 63 | 
            +
                  declarative (< 0.1.0)
         | 
| 64 | 
            +
                  declarative-option (< 0.2.0)
         | 
| 65 | 
            +
                  uber (< 0.2.0)
         | 
| 66 | 
            +
                retriable (3.1.2)
         | 
| 67 | 
            +
                signet (0.8.1)
         | 
| 68 | 
            +
                  addressable (~> 2.3)
         | 
| 69 | 
            +
                  faraday (~> 0.9)
         | 
| 70 | 
            +
                  jwt (>= 1.5, < 3.0)
         | 
| 71 | 
            +
                  multi_json (~> 1.10)
         | 
| 72 | 
            +
                thor (0.20.0)
         | 
| 73 | 
            +
                thread_safe (0.3.6)
         | 
| 74 | 
            +
                tzinfo (1.2.5)
         | 
| 75 | 
            +
                  thread_safe (~> 0.1)
         | 
| 76 | 
            +
                uber (0.1.0)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            PLATFORMS
         | 
| 79 | 
            +
              ruby
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            DEPENDENCIES
         | 
| 82 | 
            +
              bundler (~> 1.16)
         | 
| 83 | 
            +
              byebug
         | 
| 84 | 
            +
              rake (~> 10.0)
         | 
| 85 | 
            +
              schedule-cli!
         | 
| 86 | 
            +
             | 
| 87 | 
            +
            BUNDLED WITH
         | 
| 88 | 
            +
               1.16.2
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            # Schedule
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/schedule`. To experiment with that code, run `bin/console` for an interactive prompt.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            TODO: Delete this and the text above, and describe your gem
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ## Installation
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Add this line to your application's Gemfile:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            ```ruby
         | 
| 12 | 
            +
            gem 'schedule'
         | 
| 13 | 
            +
            ```
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            And then execute:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                $ bundle
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Or install it yourself as:
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                $ gem install schedule
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ## Usage
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            TODO: Write usage instructions here
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            ## Development
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            ## Contributing
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schedule.
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/console
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "bundler/setup"
         | 
| 4 | 
            +
            require "schedule"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # You can add fixtures and/or initialization code here to make experimenting
         | 
| 7 | 
            +
            # with your gem easier. You can also use a different console, if you like.
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # (If you use this, don't forget to add pry to your Gemfile!)
         | 
| 10 | 
            +
            # require "pry"
         | 
| 11 | 
            +
            # Pry.start
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            require "irb"
         | 
| 14 | 
            +
            IRB.start(__FILE__)
         | 
    
        data/bin/setup
    ADDED
    
    
    
        data/exe/schedule
    ADDED
    
    
| @@ -0,0 +1,59 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'active_support/core_ext/numeric/time'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Schedule
         | 
| 6 | 
            +
              class AvailabilityRange
         | 
| 7 | 
            +
                def initialize(start, end_, day_start:, day_end:, granularity: 5.minutes)
         | 
| 8 | 
            +
                  @start = start
         | 
| 9 | 
            +
                  @granularity = granularity
         | 
| 10 | 
            +
                  @slots = Array.new(n_slots(start, end_)) do |i|
         | 
| 11 | 
            +
                    dt = slot_to_datetime(i)
         | 
| 12 | 
            +
                    (dt.hour > day_start || (dt.hour == day_start && !dt.minute.zero?)) && (dt.hour < day_end || (dt.hour == day_end && dt.minute.zero?))
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def mark!(start, end_)
         | 
| 17 | 
            +
                  slot_range(start, end_).each do |i|
         | 
| 18 | 
            +
                    @slots[i] = false
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def free_ranges(align_to:, duration:)
         | 
| 23 | 
            +
                  ranges = []
         | 
| 24 | 
            +
                  current_free = false
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  @slots.each_with_index do |free, i|
         | 
| 27 | 
            +
                    if current_free
         | 
| 28 | 
            +
                      next if free
         | 
| 29 | 
            +
                      ranges.last << i
         | 
| 30 | 
            +
                      current_free = false
         | 
| 31 | 
            +
                    else
         | 
| 32 | 
            +
                      next unless free
         | 
| 33 | 
            +
                      ranges << [i - 1]
         | 
| 34 | 
            +
                      current_free = true
         | 
| 35 | 
            +
                    end
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                  ranges.map do |(s, e)|
         | 
| 39 | 
            +
                    start = Time.at((slot_to_datetime(s).to_time.to_f / align_to.minutes).ceil * align_to.minutes)
         | 
| 40 | 
            +
                    end_ = Time.at((slot_to_datetime(e).to_time.to_f / align_to.minutes).floor * align_to.minutes)
         | 
| 41 | 
            +
                    (end_ - start) < duration.minutes ? nil : [start, end_]
         | 
| 42 | 
            +
                  end.compact
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                private
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                def slot_range(start, end_)
         | 
| 48 | 
            +
                  n_slots(@start, start)..n_slots(@start, end_)
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def n_slots(start, end_)
         | 
| 52 | 
            +
                  ((end_.to_time - start.to_time) / @granularity.to_f).ceil
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                def slot_to_datetime(slot)
         | 
| 56 | 
            +
                  (@start.to_time + (slot * @granularity)).to_datetime
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
            end
         | 
| @@ -0,0 +1,120 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'active_support/core_ext/date'
         | 
| 4 | 
            +
            require 'active_support/core_ext/numeric/time'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            require 'launchy'
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            require 'google/apis/calendar_v3'
         | 
| 9 | 
            +
            require 'googleauth'
         | 
| 10 | 
            +
            require 'googleauth/stores/file_token_store'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            require 'schedule/availability_range'
         | 
| 13 | 
            +
            require 'schedule/config'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            module Schedule
         | 
| 16 | 
            +
              class Calendar
         | 
| 17 | 
            +
                CLIENT_ID = '913320886860-hml2g9fmellq668ljv4od42jntkmuefd.apps.googleusercontent.com'
         | 
| 18 | 
            +
                TOKENS_PATH = Config.file('tokens.yml')
         | 
| 19 | 
            +
                SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
         | 
| 20 | 
            +
                OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def initialize(options)
         | 
| 23 | 
            +
                  @service = Google::Apis::CalendarV3::CalendarService.new.tap do |service|
         | 
| 24 | 
            +
                    service.authorization = authorize!
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                  @options = options
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def free_slots
         | 
| 30 | 
            +
                  center = Date.today + @options[:day_offset].days
         | 
| 31 | 
            +
                  (start, end_), events = get_events_in_range(center, @options[:day_buffer])
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  range = AvailabilityRange.new(
         | 
| 34 | 
            +
                    start.beginning_of_day,
         | 
| 35 | 
            +
                    end_.end_of_day,
         | 
| 36 | 
            +
                    day_start: @options[:day][:start],
         | 
| 37 | 
            +
                    day_end: @options[:day][:end],
         | 
| 38 | 
            +
                  )
         | 
| 39 | 
            +
                  events.each do |event|
         | 
| 40 | 
            +
                    range.mark!(
         | 
| 41 | 
            +
                      parse_event_datetime(event.start, date_transformer: :beginning_of_day),
         | 
| 42 | 
            +
                      parse_event_datetime(event.end, date_transformer: :end_of_day),
         | 
| 43 | 
            +
                    )
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                  range.free_ranges(**@options.slice(:duration, :align_to))
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                private
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                def parse_event_datetime(datetime, date_transformer:)
         | 
| 51 | 
            +
                  if datetime.date_time.present?
         | 
| 52 | 
            +
                    datetime.date_time
         | 
| 53 | 
            +
                  else
         | 
| 54 | 
            +
                    Date.parse(datetime.date).send(date_transformer).to_datetime
         | 
| 55 | 
            +
                  end
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                def get_events_in_range(center, buffer)
         | 
| 59 | 
            +
                  is_weekend = center.saturday? || center.sunday?
         | 
| 60 | 
            +
                  events = []
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  back_current = center
         | 
| 63 | 
            +
                  buffer_back = 0
         | 
| 64 | 
            +
                  while buffer_back < buffer
         | 
| 65 | 
            +
                    back_current -= 1.day
         | 
| 66 | 
            +
                    if !is_weekend && (back_current.saturday? || back_current.sunday?)
         | 
| 67 | 
            +
                      events << Google::Apis::CalendarV3::Event.new(
         | 
| 68 | 
            +
                        start: Google::Apis::CalendarV3::EventDateTime.new(date: back_current.to_s),
         | 
| 69 | 
            +
                        end: Google::Apis::CalendarV3::EventDateTime.new(date: back_current.to_s),
         | 
| 70 | 
            +
                      )
         | 
| 71 | 
            +
                    else
         | 
| 72 | 
            +
                      events.concat get_events_for_date(back_current)
         | 
| 73 | 
            +
                      buffer_back += 1
         | 
| 74 | 
            +
                    end
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                  forward_current = center
         | 
| 78 | 
            +
                  buffer_forward = 0
         | 
| 79 | 
            +
                  while buffer_forward < buffer
         | 
| 80 | 
            +
                    forward_current += 1.day
         | 
| 81 | 
            +
                    if !is_weekend && (forward_current.saturday? || forward_current.sunday?)
         | 
| 82 | 
            +
                      events << Google::Apis::CalendarV3::Event.new(
         | 
| 83 | 
            +
                        start: Google::Apis::CalendarV3::EventDateTime.new(date: forward_current.to_s),
         | 
| 84 | 
            +
                        end: Google::Apis::CalendarV3::EventDateTime.new(date: forward_current.to_s),
         | 
| 85 | 
            +
                      )
         | 
| 86 | 
            +
                    else
         | 
| 87 | 
            +
                      events.concat get_events_for_date(forward_current)
         | 
| 88 | 
            +
                      buffer_forward += 1
         | 
| 89 | 
            +
                    end
         | 
| 90 | 
            +
                  end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                  [[back_current, forward_current], events.uniq]
         | 
| 93 | 
            +
                end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                def get_events_for_date(date)
         | 
| 96 | 
            +
                  @options[:calendars].flat_map do |calendar|
         | 
| 97 | 
            +
                    @service.list_events(
         | 
| 98 | 
            +
                      calendar,
         | 
| 99 | 
            +
                      single_events: true,
         | 
| 100 | 
            +
                      time_min: date.beginning_of_day.iso8601,
         | 
| 101 | 
            +
                      time_max: date.end_of_day.iso8601,
         | 
| 102 | 
            +
                    ).items
         | 
| 103 | 
            +
                  end
         | 
| 104 | 
            +
                end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                def authorize!
         | 
| 107 | 
            +
                  token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKENS_PATH)
         | 
| 108 | 
            +
                  client_id = Google::Auth::ClientId.new(CLIENT_ID, '')
         | 
| 109 | 
            +
                  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
         | 
| 110 | 
            +
                  authorizer.get_credentials(:default) || begin
         | 
| 111 | 
            +
                    url = authorizer.get_authorization_url(base_url: OOB_URI)
         | 
| 112 | 
            +
                    puts 'Redirecting to Google for authorization...'
         | 
| 113 | 
            +
                    Launchy.open(url) { puts "Open this URL in your browser:\n#{url}\n\n" }
         | 
| 114 | 
            +
                    print 'Code: '
         | 
| 115 | 
            +
                    code = gets
         | 
| 116 | 
            +
                    authorizer.get_and_store_credentials_from_code(user_id: :default, code: code, base_url: OOB_URI)
         | 
| 117 | 
            +
                  end
         | 
| 118 | 
            +
                end
         | 
| 119 | 
            +
              end
         | 
| 120 | 
            +
            end
         | 
    
        data/lib/schedule/cli.rb
    ADDED
    
    | @@ -0,0 +1,71 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'thor'
         | 
| 4 | 
            +
            require 'nokogiri'
         | 
| 5 | 
            +
            require 'chronic'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            require 'schedule/calendar'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            module Schedule
         | 
| 10 | 
            +
              class Cli < Thor
         | 
| 11 | 
            +
                package_name 'Schedule'
         | 
| 12 | 
            +
                default_task :availability
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def initialize(*args, **kwargs)
         | 
| 15 | 
            +
                  Config.setup!
         | 
| 16 | 
            +
                  super
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                desc 'availability type', 'print availability table'
         | 
| 20 | 
            +
                method_option :html, type: :boolean, default: false
         | 
| 21 | 
            +
                method_option :debug, type: :boolean, default: false
         | 
| 22 | 
            +
                def availability(type = nil, *offset_string)
         | 
| 23 | 
            +
                  config = Config.values.slice(:calendars, :day).merge(Config.value(:defaults))
         | 
| 24 | 
            +
                  if /\A\d+\z/.match(type)
         | 
| 25 | 
            +
                    config[:duration] = type.to_i
         | 
| 26 | 
            +
                  else
         | 
| 27 | 
            +
                    config.merge!(Config.value(:events, type&.to_sym) || {})
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                  if offset_string.present?
         | 
| 30 | 
            +
                    config[:day_offset] = parse_offset offset_string.join(' ')
         | 
| 31 | 
            +
                  elsif config[:day_offset].is_a?(String)
         | 
| 32 | 
            +
                    config[:day_offset] = parse_offset config[:day_offset]
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
                  puts "#{config}" if options.debug?
         | 
| 35 | 
            +
                  slots = Schedule::Calendar.new(config).free_slots
         | 
| 36 | 
            +
                  print_with_options format_free_slots(slots)
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                desc 'config', 'open config file in default editor'
         | 
| 40 | 
            +
                def config
         | 
| 41 | 
            +
                  system("${EDITOR:-${VISUAL:-vi}} '#{Config.config_file}'")
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                private
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def parse_offset(string)
         | 
| 47 | 
            +
                  (Chronic.parse(string, context: :future).to_date - Date.today).to_i
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                def print_with_options(string)
         | 
| 51 | 
            +
                  formatted = options.html? ? string : plain_text(string)
         | 
| 52 | 
            +
                  puts formatted
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                def plain_text(html)
         | 
| 56 | 
            +
                  document = Nokogiri::HTML.parse(html)
         | 
| 57 | 
            +
                  document.css('br').each { |node| node.replace("\n") }
         | 
| 58 | 
            +
                  document.text
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                def format_free_slots(slots)
         | 
| 62 | 
            +
                  dates = Hash.new { |h, k| h[k] = [] }
         | 
| 63 | 
            +
                  slots.each do |(s, e)|
         | 
| 64 | 
            +
                    dates[s.to_date] << "#{s.strftime('%-l:%M %p')} to #{e.strftime('%-l:%M %p')}"
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
                  dates.map do |date, times|
         | 
| 67 | 
            +
                    "<u><b>#{date.strftime('%A %B %-e')}</b></u><br/>#{times.join('<br/>')}"
         | 
| 68 | 
            +
                  end.join('<br/>')
         | 
| 69 | 
            +
                end
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
            end
         | 
| @@ -0,0 +1,48 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'fileutils'
         | 
| 4 | 
            +
            require 'yaml'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module Schedule
         | 
| 7 | 
            +
              class Config
         | 
| 8 | 
            +
                CONFIG_DIR = "#{Dir.home}/.schedule-cli/"
         | 
| 9 | 
            +
                DEFAULT_CONFIG = {
         | 
| 10 | 
            +
                  calendars: ['primary'],
         | 
| 11 | 
            +
                  day: {
         | 
| 12 | 
            +
                    start: 8,
         | 
| 13 | 
            +
                    end: 18,
         | 
| 14 | 
            +
                  },
         | 
| 15 | 
            +
                  defaults: {
         | 
| 16 | 
            +
                    day_offset: 'next weekday',
         | 
| 17 | 
            +
                    day_buffer: 1,
         | 
| 18 | 
            +
                    duration: 30,
         | 
| 19 | 
            +
                    align_to: 15,
         | 
| 20 | 
            +
                  },
         | 
| 21 | 
            +
                  events: {
         | 
| 22 | 
            +
                    call: { duration: 30 },
         | 
| 23 | 
            +
                    coffee: { duration: 60 },
         | 
| 24 | 
            +
                  },
         | 
| 25 | 
            +
                }.freeze
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def self.file(*args)
         | 
| 28 | 
            +
                  File.join(CONFIG_DIR, *args)
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                def self.config_file
         | 
| 32 | 
            +
                  file('config.yml')
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                def self.setup!
         | 
| 36 | 
            +
                  FileUtils.mkdir_p CONFIG_DIR
         | 
| 37 | 
            +
                  File.write(config_file, YAML.dump(DEFAULT_CONFIG)) unless File.file?(config_file)
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def self.value(*args)
         | 
| 41 | 
            +
                  values.dig *args
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                def self.values
         | 
| 45 | 
            +
                  @values ||= YAML.load(File.read(config_file)) # rubocop:disable Security/YAMLLoad
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
            end
         | 
    
        data/lib/schedule.rb
    ADDED
    
    
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            lib = File.expand_path('lib', __dir__)
         | 
| 4 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 5 | 
            +
            require 'schedule/version'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Gem::Specification.new do |spec|
         | 
| 8 | 
            +
              spec.name          = 'schedule-cli'
         | 
| 9 | 
            +
              spec.version       = Schedule::VERSION
         | 
| 10 | 
            +
              spec.authors       = ['Yasyf Mohamedali']
         | 
| 11 | 
            +
              spec.email         = ['yasyf@meetkaruna.com']
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              spec.summary       = 'CLI to generare summary of calendar openings.'
         | 
| 14 | 
            +
              spec.homepage      = 'https://github.com/karuna-health/schedule-cli'
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              # Specify which files should be added to the gem when it is released.
         | 
| 17 | 
            +
              # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
         | 
| 18 | 
            +
              spec.files = Dir.chdir(File.expand_path(__dir__)) do
         | 
| 19 | 
            +
                `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
              spec.bindir        = 'exe'
         | 
| 22 | 
            +
              spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         | 
| 23 | 
            +
              spec.require_paths = ['lib']
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              spec.add_development_dependency 'bundler', '~> 1.16'
         | 
| 26 | 
            +
              spec.add_development_dependency 'byebug'
         | 
| 27 | 
            +
              spec.add_development_dependency 'rake', '~> 10.0'
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              spec.add_dependency 'activesupport', '~> 5.2'
         | 
| 30 | 
            +
              spec.add_dependency 'google-api-client', '~> 0.11'
         | 
| 31 | 
            +
              spec.add_dependency 'launchy'
         | 
| 32 | 
            +
              spec.add_dependency 'nokogiri'
         | 
| 33 | 
            +
              spec.add_dependency 'thor'
         | 
| 34 | 
            +
              spec.add_dependency 'chronic'
         | 
| 35 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,187 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: schedule-cli
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Yasyf Mohamedali
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: exe
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2018-08-03 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: bundler
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '1.16'
         | 
| 20 | 
            +
              type: :development
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '1.16'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: byebug
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rake
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - "~>"
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '10.0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - "~>"
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '10.0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: activesupport
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - "~>"
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '5.2'
         | 
| 62 | 
            +
              type: :runtime
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - "~>"
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '5.2'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: google-api-client
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - "~>"
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0.11'
         | 
| 76 | 
            +
              type: :runtime
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - "~>"
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0.11'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: launchy
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :runtime
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: nokogiri
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - ">="
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :runtime
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ">="
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: thor
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - ">="
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :runtime
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - ">="
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: chronic
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - ">="
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0'
         | 
| 132 | 
            +
              type: :runtime
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - ">="
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0'
         | 
| 139 | 
            +
            description: 
         | 
| 140 | 
            +
            email:
         | 
| 141 | 
            +
            - yasyf@meetkaruna.com
         | 
| 142 | 
            +
            executables:
         | 
| 143 | 
            +
            - schedule
         | 
| 144 | 
            +
            extensions: []
         | 
| 145 | 
            +
            extra_rdoc_files: []
         | 
| 146 | 
            +
            files:
         | 
| 147 | 
            +
            - ".byebug_history"
         | 
| 148 | 
            +
            - ".gitignore"
         | 
| 149 | 
            +
            - ".rubocop.yml"
         | 
| 150 | 
            +
            - Gemfile
         | 
| 151 | 
            +
            - Gemfile.lock
         | 
| 152 | 
            +
            - README.md
         | 
| 153 | 
            +
            - Rakefile
         | 
| 154 | 
            +
            - bin/console
         | 
| 155 | 
            +
            - bin/setup
         | 
| 156 | 
            +
            - exe/schedule
         | 
| 157 | 
            +
            - lib/schedule.rb
         | 
| 158 | 
            +
            - lib/schedule/availability_range.rb
         | 
| 159 | 
            +
            - lib/schedule/calendar.rb
         | 
| 160 | 
            +
            - lib/schedule/cli.rb
         | 
| 161 | 
            +
            - lib/schedule/config.rb
         | 
| 162 | 
            +
            - lib/schedule/version.rb
         | 
| 163 | 
            +
            - schedule-cli.gemspec
         | 
| 164 | 
            +
            homepage: https://github.com/karuna-health/schedule-cli
         | 
| 165 | 
            +
            licenses: []
         | 
| 166 | 
            +
            metadata: {}
         | 
| 167 | 
            +
            post_install_message: 
         | 
| 168 | 
            +
            rdoc_options: []
         | 
| 169 | 
            +
            require_paths:
         | 
| 170 | 
            +
            - lib
         | 
| 171 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 172 | 
            +
              requirements:
         | 
| 173 | 
            +
              - - ">="
         | 
| 174 | 
            +
                - !ruby/object:Gem::Version
         | 
| 175 | 
            +
                  version: '0'
         | 
| 176 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 177 | 
            +
              requirements:
         | 
| 178 | 
            +
              - - ">="
         | 
| 179 | 
            +
                - !ruby/object:Gem::Version
         | 
| 180 | 
            +
                  version: '0'
         | 
| 181 | 
            +
            requirements: []
         | 
| 182 | 
            +
            rubyforge_project: 
         | 
| 183 | 
            +
            rubygems_version: 2.7.6
         | 
| 184 | 
            +
            signing_key: 
         | 
| 185 | 
            +
            specification_version: 4
         | 
| 186 | 
            +
            summary: CLI to generare summary of calendar openings.
         | 
| 187 | 
            +
            test_files: []
         |