habitica_cli 0.0.1 → 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 +4 -4
- data/.travis.yml +11 -0
- data/README.md +3 -14
- data/lib/habitica_cli/api.rb +53 -15
- data/lib/habitica_cli/commands/add.rb +1 -1
- data/lib/habitica_cli/commands/clear.rb +1 -1
- data/lib/habitica_cli/commands/do.rb +1 -1
- data/lib/habitica_cli/commands/list.rb +1 -1
- data/lib/habitica_cli/commands/shared.rb +2 -1
- data/lib/habitica_cli/commands/status.rb +16 -9
- data/lib/habitica_cli/main.rb +2 -0
- data/lib/habitica_cli/version.rb +1 -1
- data/spec/api_spec.rb +1 -1
- metadata +4 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 75e78755c72d1f2d71fb5182d80d5494be646b21
         | 
| 4 | 
            +
              data.tar.gz: 6bad26ae765114f02b088120c237b815a6e3c1dc
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: c9ad679bdbb9c424940928f5029540cdb36e3ce76cca681ff2d4cdcdfb349cc2f5e14d03de4b119b351832c04da3c48c00d10ddac0569fdccc73a424d8b5e37c
         | 
| 7 | 
            +
              data.tar.gz: d390df0596dedb534f7629fe58265a8ea07ea8abb90645fe8979b015fbc8a289d722086a186d755a5740d409a76796c02c50577c678ecc1b5b53b91d468c0394
         | 
    
        data/.travis.yml
    ADDED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -1,22 +1,11 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            habitica-cli [](https://travis-ci.org/NickTomlin/habitica-cli-ruby) [](https://badge.fury.io/rb/habitica_cli)
         | 
| 2 | 
            +
            ===
         | 
| 2 3 |  | 
| 3 4 | 
             
            A command line interface for habitica
         | 
| 4 5 |  | 
| 5 6 | 
             
            ## Installation
         | 
| 6 7 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
            ```ruby
         | 
| 10 | 
            -
            gem 'habitica-cli'
         | 
| 11 | 
            -
            ```
         | 
| 12 | 
            -
             | 
| 13 | 
            -
            And then execute:
         | 
| 14 | 
            -
             | 
| 15 | 
            -
                $ bundle
         | 
| 16 | 
            -
             | 
| 17 | 
            -
            Or install it yourself as:
         | 
| 18 | 
            -
             | 
| 19 | 
            -
                $ gem install habitica-cli
         | 
| 8 | 
            +
                $ gem install habitica_cli
         | 
| 20 9 |  | 
| 21 10 | 
             
            ## Usage
         | 
| 22 11 |  | 
    
        data/lib/habitica_cli/api.rb
    CHANGED
    
    | @@ -1,22 +1,53 @@ | |
| 1 1 | 
             
            module HabiticaCli
         | 
| 2 2 | 
             
              # responsible for communicating with habit at a low level
         | 
| 3 3 | 
             
              class Api
         | 
| 4 | 
            +
                # A simple wrapper for API errors
         | 
| 5 | 
            +
                class ApiError < StandardError
         | 
| 6 | 
            +
                  def initialize(response)
         | 
| 7 | 
            +
                    @response = response
         | 
| 8 | 
            +
                    set_backtrace([])
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  def to_s
         | 
| 12 | 
            +
                    errors = @response.body['errors'] || []
         | 
| 13 | 
            +
                    path = @response.url.path
         | 
| 14 | 
            +
                    error_messages = errors.map { |e| e['message'] }
         | 
| 15 | 
            +
                    %(
         | 
| 16 | 
            +
            Habitica Error (#{path}): #{@response.body['message']}
         | 
| 17 | 
            +
            ====
         | 
| 18 | 
            +
            #{error_messages.join("\n")}
         | 
| 19 | 
            +
            )
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                # generic handling for habitica responses
         | 
| 24 | 
            +
                class HabiticaResponseMiddleware < Faraday::Middleware
         | 
| 25 | 
            +
                  def call(request_env)
         | 
| 26 | 
            +
                    @app.call(request_env).on_complete do |response_env|
         | 
| 27 | 
            +
                      fail ApiError.new(response_env) unless response_env.success? # rubocop:disable Style/RaiseArgs, Style/LineLength
         | 
| 28 | 
            +
                      response_env
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 4 33 | 
             
                def initialize(user_id, api_token)
         | 
| 5 34 | 
             
                  @debug = ENV['DEBUG_HABITICA'] == 'true'
         | 
| 6 | 
            -
             | 
| 7 | 
            -
                  @ | 
| 35 | 
            +
                  @user_id = user_id
         | 
| 36 | 
            +
                  @api_token = api_token
         | 
| 8 37 | 
             
                end
         | 
| 9 38 |  | 
| 10 | 
            -
                def get(url)
         | 
| 11 | 
            -
                   | 
| 39 | 
            +
                def get(url, query = nil)
         | 
| 40 | 
            +
                  connection do |faraday|
         | 
| 41 | 
            +
                    faraday.request :url_encoded
         | 
| 42 | 
            +
                  end.get(url, query)
         | 
| 12 43 | 
             
                end
         | 
| 13 44 |  | 
| 14 45 | 
             
                def post(url, body = nil)
         | 
| 15 | 
            -
                   | 
| 46 | 
            +
                  connection.post(url, body)
         | 
| 16 47 | 
             
                end
         | 
| 17 48 |  | 
| 18 49 | 
             
                def put(url, body)
         | 
| 19 | 
            -
                   | 
| 50 | 
            +
                  connection.put(url, body)
         | 
| 20 51 | 
             
                end
         | 
| 21 52 |  | 
| 22 53 | 
             
                private
         | 
| @@ -29,17 +60,24 @@ module HabiticaCli | |
| 29 60 | 
             
                  }
         | 
| 30 61 | 
             
                end
         | 
| 31 62 |  | 
| 32 | 
            -
                def  | 
| 33 | 
            -
                   | 
| 34 | 
            -
                    url: 'https://habitica.com/api/v2/',
         | 
| 35 | 
            -
                    headers: default_headers(user_id, api_token)
         | 
| 36 | 
            -
                  ) do |faraday|
         | 
| 37 | 
            -
                    faraday.request :json
         | 
| 63 | 
            +
                def configure_defaults(faraday)
         | 
| 64 | 
            +
                  faraday.request :json
         | 
| 38 65 |  | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 66 | 
            +
                  faraday.use HabiticaResponseMiddleware
         | 
| 67 | 
            +
                  faraday.response :json, content_type: /\bjson$/
         | 
| 68 | 
            +
                  faraday.response :logger if @debug
         | 
| 41 69 |  | 
| 42 | 
            -
             | 
| 70 | 
            +
                  faraday.adapter Faraday.default_adapter
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                def connection
         | 
| 74 | 
            +
                  Faraday.new(
         | 
| 75 | 
            +
                    url: 'https://habitica.com/api/v3/',
         | 
| 76 | 
            +
                    headers: default_headers(@user_id, @api_token)
         | 
| 77 | 
            +
                  ) do |faraday|
         | 
| 78 | 
            +
                    configure_defaults(faraday)
         | 
| 79 | 
            +
                    yield faraday if block_given?
         | 
| 80 | 
            +
                    faraday
         | 
| 43 81 | 
             
                  end
         | 
| 44 82 | 
             
                end
         | 
| 45 83 | 
             
              end
         | 
| @@ -4,7 +4,7 @@ module HabiticaCli | |
| 4 4 | 
             
              module Commands
         | 
| 5 5 | 
             
                def self.add(env, type, text)
         | 
| 6 6 | 
             
                  validate_type(type)
         | 
| 7 | 
            -
                  response = env.api.post(' | 
| 7 | 
            +
                  response = env.api.post('tasks', type: type, text: text)
         | 
| 8 8 |  | 
| 9 9 | 
             
                  if response.success?
         | 
| 10 10 | 
             
                    task = cache_tasks(env, [response.body], type).first
         | 
| @@ -4,7 +4,7 @@ module HabiticaCli | |
| 4 4 | 
             
                def self.do(env, cache_ids)
         | 
| 5 5 | 
             
                  items = cache_ids.map { |id| env.cache.get(id) }
         | 
| 6 6 | 
             
                  items.each do |item|
         | 
| 7 | 
            -
                    response = env.api.post(" | 
| 7 | 
            +
                    response = env.api.post("tasks/#{item['id']}/score/up")
         | 
| 8 8 | 
             
                    if response.success?
         | 
| 9 9 | 
             
                      puts "Completed: #{item['text']}"
         | 
| 10 10 | 
             
                    else
         | 
| @@ -3,21 +3,28 @@ module HabiticaCli | |
| 3 3 | 
             
              # and tasks for a user as well as caching them
         | 
| 4 4 | 
             
              module Commands
         | 
| 5 5 | 
             
                def self.status(env)
         | 
| 6 | 
            -
                   | 
| 7 | 
            -
                   | 
| 8 | 
            -
                    puts status_display(response.body)
         | 
| 9 | 
            -
                    display(env, response.body['todos'] + response.body['dailys'], nil)
         | 
| 10 | 
            -
                  else
         | 
| 11 | 
            -
                    puts "Error: #{response.error}"
         | 
| 12 | 
            -
                  end
         | 
| 6 | 
            +
                  get_user_tasks(env)
         | 
| 7 | 
            +
                  get_user_info(env)
         | 
| 13 8 | 
             
                end
         | 
| 14 9 |  | 
| 15 | 
            -
                def self.status_display( | 
| 16 | 
            -
                  stats =  | 
| 10 | 
            +
                def self.status_display(data)
         | 
| 11 | 
            +
                  stats = data['stats']
         | 
| 17 12 | 
             
                  puts [
         | 
| 18 13 | 
             
                    "Gold: #{stats['gp'].round}",
         | 
| 19 14 | 
             
                    "Health: #{stats['hp'].round}/#{stats['maxHealth']}"
         | 
| 20 15 | 
             
                  ].join(' | ')
         | 
| 21 16 | 
             
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                private_class_method
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def self.get_user_info(env)
         | 
| 21 | 
            +
                  user_response = env.api.get('user')
         | 
| 22 | 
            +
                  status_display(user_response.body['data'])
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def self.get_user_tasks(env)
         | 
| 26 | 
            +
                  task_response = env.api.get('tasks/user', type: 'dailys')
         | 
| 27 | 
            +
                  display(env, task_response.body, nil)
         | 
| 28 | 
            +
                end
         | 
| 22 29 | 
             
              end
         | 
| 23 30 | 
             
            end
         | 
    
        data/lib/habitica_cli/main.rb
    CHANGED
    
    
    
        data/lib/habitica_cli/version.rb
    CHANGED
    
    
    
        data/spec/api_spec.rb
    CHANGED
    
    | @@ -11,7 +11,7 @@ RSpec.describe HabiticaCli::Api do | |
| 11 11 |  | 
| 12 12 | 
             
                api.get('test')
         | 
| 13 13 |  | 
| 14 | 
            -
                expect(WebMock).to have_requested(:get, 'https://habitica.com/api/ | 
| 14 | 
            +
                expect(WebMock).to have_requested(:get, 'https://habitica.com/api/v3/test')
         | 
| 15 15 | 
             
                  .with(headers: { :'X-Api-Key' => 'test_key', :'X-Api-User' => 'test_user' }) # rubocop:disable Metrics/LineLength
         | 
| 16 16 | 
             
              end
         | 
| 17 17 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: habitica_cli
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0 | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Nick Tomlin
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016- | 
| 11 | 
            +
            date: 2016-06-10 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: faraday
         | 
| @@ -153,6 +153,7 @@ files: | |
| 153 153 | 
             
            - ".gitignore"
         | 
| 154 154 | 
             
            - ".rspec"
         | 
| 155 155 | 
             
            - ".ruby-version"
         | 
| 156 | 
            +
            - ".travis.yml"
         | 
| 156 157 | 
             
            - Gemfile
         | 
| 157 158 | 
             
            - LICENSE.txt
         | 
| 158 159 | 
             
            - README.md
         | 
| @@ -206,3 +207,4 @@ test_files: | |
| 206 207 | 
             
            - spec/habitica_cli_spec.rb
         | 
| 207 208 | 
             
            - spec/spec_helper.rb
         | 
| 208 209 | 
             
            - spec/support/.gitkeep
         | 
| 210 | 
            +
            has_rdoc: 
         |