habitica_cli 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbb422d94f5bab18d8ee01b611d5722b31809ec8
4
- data.tar.gz: dfef128671329117f2b75d43f05fd1f839cb9e75
3
+ metadata.gz: 75e78755c72d1f2d71fb5182d80d5494be646b21
4
+ data.tar.gz: 6bad26ae765114f02b088120c237b815a6e3c1dc
5
5
  SHA512:
6
- metadata.gz: c9f1f8db03e4c756ec5be6bd0b2a94ce8d7b791a551899ba61708018b7df98a1e313ca26f1f035e8678b9cebb2b7c855a49df4e113077112b3d8fa6218fcc8cc
7
- data.tar.gz: 96f988ef139e80ab147858f5c16a8cf921447f3b19fa23c5465129586712ac353e3025948eb222c190cfb4b0666c0627d775f70b68e0ccdf780af9a5fa399113
6
+ metadata.gz: c9ad679bdbb9c424940928f5029540cdb36e3ce76cca681ff2d4cdcdfb349cc2f5e14d03de4b119b351832c04da3c48c00d10ddac0569fdccc73a424d8b5e37c
7
+ data.tar.gz: d390df0596dedb534f7629fe58265a8ea07ea8abb90645fe8979b015fbc8a289d722086a186d755a5740d409a76796c02c50577c678ecc1b5b53b91d468c0394
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: required
2
+ dist: precise
3
+ language: ruby
4
+ rvm:
5
+ - 2.2.2
6
+ before_install:
7
+ - gem update bundler
8
+ notifications:
9
+ email:
10
+ on_success: never
11
+ on_failure: always
data/README.md CHANGED
@@ -1,22 +1,11 @@
1
- # habitica-cli
1
+ habitica-cli [![Build Status](https://travis-ci.org/NickTomlin/habitica-cli-ruby.png?branch=master)](https://travis-ci.org/NickTomlin/habitica-cli-ruby) [![Gem Version](https://badge.fury.io/rb/habitica_cli.svg)](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
- Add this line to your application's Gemfile:
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
 
@@ -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
- @connection = create_connection(user_id, api_token)
35
+ @user_id = user_id
36
+ @api_token = api_token
8
37
  end
9
38
 
10
- def get(url)
11
- @connection.get(url)
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
- @connection.post(url, body)
46
+ connection.post(url, body)
16
47
  end
17
48
 
18
49
  def put(url, body)
19
- @connection.put(url, body)
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 create_connection(user_id, api_token)
33
- Faraday.new(
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
- faraday.response :json, content_type: /\bjson$/
40
- faraday.response :logger if @debug
66
+ faraday.use HabiticaResponseMiddleware
67
+ faraday.response :json, content_type: /\bjson$/
68
+ faraday.response :logger if @debug
41
69
 
42
- faraday.adapter Faraday.default_adapter
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('user/tasks', type: type, text: text)
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
@@ -3,7 +3,7 @@ module HabiticaCli
3
3
  # to slim down output
4
4
  module Commands
5
5
  def self.clear(env)
6
- env.api.post('user/tasks/clear-completed')
6
+ env.api.post('tasks/clear-completed')
7
7
  if response.success?
8
8
  puts 'Tasks cleared'
9
9
  else
@@ -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("user/tasks/#{item['id']}/up")
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,7 +3,7 @@ module HabiticaCli
3
3
  module Commands
4
4
  def self.list(env, type = nil)
5
5
  validate_type(type) if type
6
- response = env.api.get('user/tasks')
6
+ response = env.api.get('tasks/user')
7
7
 
8
8
  if response.success?
9
9
  display(env, response.body, type)
@@ -26,7 +26,8 @@ module HabiticaCli
26
26
  )
27
27
  end
28
28
 
29
- def self.display(env, raw_tasks, type)
29
+ def self.display(env, body, type)
30
+ raw_tasks = body['data']
30
31
  tasks = cache_tasks(env, raw_tasks, type)
31
32
  puts type.capitalize unless type.nil?
32
33
  tasks.each do |item|
@@ -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
- response = env.api.get('user')
7
- if response.success?
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(body)
16
- stats = body['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
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  module HabiticaCli
2
4
  # At the moment this holds _all_ tasks
3
5
  # until we can figure out a better way to split
@@ -1,3 +1,3 @@
1
1
  module HabiticaCli
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
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/v2/test')
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.1
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-01-27 00:00:00.000000000 Z
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: