habitica_client 0.0.7 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc30d20c07e3c6080844554c2cc94e3ff7c4edd7
4
- data.tar.gz: 77b5d94876785015859720c6362f7034cc617e27
3
+ metadata.gz: fc2e42de48e261575d3cec43ced4290ac9759fce
4
+ data.tar.gz: 14cd60f8f5885c64b2dd5cbc4653d0c502f4eca6
5
5
  SHA512:
6
- metadata.gz: 36ed2def94d025fe54261204a2fa6f170b35a7c029295d85599625b617ac3ba1694066450d8cd0036cc84cbde61ab42e53b3d0ad67ec5939a2f764741b6a17ba
7
- data.tar.gz: d08db6ac1a0ddfffc9c0ba0a5cc5d2db5eac2311597280d22803d42b5f7308508eaf963b427211d171950030d49d0ee1612d80fee20bf4942b74c6de128ce5b5
6
+ metadata.gz: 08aba3ec6e1018ffcb0911554231d606d0904e93c7f504a45c4159f9311508a408f2dced29d1a16a132b0c39e6c15a6b9a90b2e8021e7bb495ffc2325dbb91d9
7
+ data.tar.gz: 455fc761bc9b115d1a856174a20372ff8f6fe24e7b073eaa3a4a1368a4bca1fd9b757a020c42270891e3b393ae9c1ff0a7a3421aa2e1f0318d8e3d49c5798a8e
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.2
1
+ 2.2.2
data/README.md CHANGED
@@ -35,12 +35,12 @@ habit.user.stats.hp
35
35
  # 50
36
36
 
37
37
  # Get tasks
38
- habit.user.tasks.each do |task|
38
+ habit.tasks.each do |task|
39
39
  puts task.text
40
40
  end
41
41
 
42
42
  # Create task
43
- task = habit.user.tasks.create(
43
+ task = habit.tasks.create(
44
44
  text: 'Do the dishes',
45
45
  type: 'todo'
46
46
  )
@@ -74,7 +74,7 @@ puts stats.player_class
74
74
 
75
75
  ### Task
76
76
  ```ruby
77
- stats = habitica_client.user.tasks
77
+ stats = habitica_client.tasks
78
78
 
79
79
  tasks.each do |task|
80
80
  puts task.daily?
@@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'byebug', '~> 5.0'
27
27
  spec.add_development_dependency 'yard', '~> 0.8'
28
28
  spec.add_development_dependency 'redcarpet', '~> 3.3'
29
+ spec.add_development_dependency 'awesome_print', '~> 1.7'
29
30
  spec.add_runtime_dependency 'httparty', '~> 0.13'
30
31
  end
@@ -12,6 +12,18 @@ class HabiticaClient
12
12
  @client = client
13
13
  end
14
14
 
15
+ def data
16
+ @data ||= client.class.get(endpoint)['data']
17
+ end
18
+
19
+ def endpoint
20
+ self.class.instance_variable_get(:@endpoint)
21
+ end
22
+
23
+ def self.endpoint(url)
24
+ instance_variable_set(:@endpoint, url)
25
+ end
26
+
15
27
  end
16
28
 
17
29
  end
@@ -11,14 +11,14 @@ class HabiticaClient
11
11
 
12
12
  include HTTParty
13
13
 
14
- base_uri 'https://habitrpg.com:443/api/v2/'
14
+ base_uri 'https://habitrpg.com:443/api/v3/'
15
15
 
16
16
  def initialize(user_id, api_token)
17
17
  @user_id = user_id
18
18
  @api_token = api_token
19
19
 
20
20
  self.class.headers('User-Agent' => 'habitapi-rpg',
21
- 'Content-Type' => 'application/json',
21
+ 'Content-Type' => 'application/json',
22
22
  'x-api-key' => @api_token,
23
23
  'x-api-user' => @user_id)
24
24
  end
@@ -74,9 +74,11 @@ class HabiticaClient
74
74
  url,
75
75
  body: to_json)
76
76
 
77
- fail ServerError, "#{response['err']}" unless response.ok?
77
+ unless response.response.code =~ /2\d{2}/
78
+ raise ServerError, response['err']
79
+ end
78
80
 
79
- response.parsed_response
81
+ response.parsed_response['data']
80
82
  end
81
83
 
82
84
  def post
@@ -1,4 +1,6 @@
1
- class HabiticaClient::User::Task
1
+ # rubocop:disable Style/UnneededInterpolation
2
+
3
+ class HabiticaClient::Task
2
4
 
3
5
  module DateAccessor
4
6
 
@@ -1,4 +1,4 @@
1
- class HabiticaClient::User::Task
1
+ class HabiticaClient::Task
2
2
 
3
3
  module Status
4
4
 
@@ -1,4 +1,4 @@
1
- class HabiticaClient::User::Task
1
+ class HabiticaClient::Task
2
2
 
3
3
  module Type
4
4
 
@@ -0,0 +1,45 @@
1
+ require 'ostruct'
2
+ require 'forwardable'
3
+ require 'time'
4
+ require 'hashup'
5
+ require 'habitica_client/restful'
6
+
7
+ class HabiticaClient
8
+
9
+ # A user task, which can be a habit, daily, or todo.
10
+ class Task < HabiticaClient::Restful
11
+
12
+ require 'habitica_client/task/date_accessor'
13
+ require 'habitica_client/task/status'
14
+ require 'habitica_client/task/type'
15
+
16
+ extend DateAccessor
17
+ include Status
18
+ include Type
19
+ extend Hashup
20
+
21
+ endpoint '/tasks'
22
+
23
+ attr_accessor :user_id, :reminders, :id, :text, :notes, :value, :priority,
24
+ :attribute, :type, :tags, :checklist, :value, :priority,
25
+ :challenge, :down, :up, :history, :streak, :frequency,
26
+ :history, :completed, :every_x, :repeat, :collapse_checklist
27
+
28
+ date_accessor :created_at, :updated_at, :start_date, :date
29
+
30
+ hashup :id, :text, :notes, :value, :priority, :attribute, :type, :tags,
31
+ :checklist, :value, :priority, :challenge, :down, :up, :history,
32
+ :streak, :frequency, :history, :completed, :every_x, :repeat,
33
+ :collapse_checklist, :created_at, :updated_at, :start_date, :date
34
+
35
+ private
36
+
37
+ def url
38
+ return "#{endpoint}/#{id}" unless new?
39
+
40
+ "#{endpoint}/user/#{id}"
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -1,4 +1,4 @@
1
- class HabiticaClient::User::Tasks
1
+ class HabiticaClient::Tasks
2
2
 
3
3
  module Status
4
4
 
@@ -1,4 +1,4 @@
1
- class HabiticaClient::User::Tasks
1
+ class HabiticaClient::Tasks
2
2
 
3
3
  module Types
4
4
 
@@ -1,22 +1,24 @@
1
- class HabiticaClient::User
1
+ class HabiticaClient
2
2
 
3
3
  # An enumerable object of all the user's tasks.
4
4
  #
5
5
  # Also contains the endpoint for creating new tasks.
6
6
  class Tasks < HabiticaClient::ApiBase
7
7
 
8
- require 'habitica_client/user/tasks/types'
9
- require 'habitica_client/user/tasks/status'
10
- require 'habitica_client/user/task'
8
+ require 'habitica_client/tasks/types'
9
+ require 'habitica_client/tasks/status'
10
+ require 'habitica_client/task'
11
11
 
12
12
  include Enumerable
13
13
  include Types
14
14
  include Status
15
15
 
16
+ endpoint '/tasks/user'
17
+
16
18
  # Iterate over user tasks
17
- def each(&block)
18
- tasks.each do |task|
19
- block.call Task.parse(client, task)
19
+ def each
20
+ data.each do |task|
21
+ yield Task.parse(client, task)
20
22
  end
21
23
  end
22
24
 
@@ -27,12 +29,6 @@ class HabiticaClient::User
27
29
  Task.new(client, attributes).save
28
30
  end
29
31
 
30
- private
31
-
32
- def tasks
33
- @tasks ||= client.class.get('/user/tasks')
34
- end
35
-
36
32
  end
37
33
 
38
34
  end
@@ -4,11 +4,18 @@ require 'hashup'
4
4
 
5
5
  class HabiticaClient::User
6
6
 
7
- class Stats < HabiticaClient::ApiBase
7
+ module StatsProperty
8
+ def stats
9
+ @stats ||= Stats.new(data['stats'])
10
+ end
11
+ end
8
12
 
13
+ class Stats
9
14
  extend Forwardable
10
15
  extend Hashup
11
16
 
17
+ attr_reader :stats
18
+
12
19
  def_delegators :stats, :training, :buffs, :per, :int, :con, :str,
13
20
  :points, :lvl, :gp, :exp, :mp, :hp, :player_class
14
21
 
@@ -20,16 +27,9 @@ class HabiticaClient::User
20
27
  :gp, :exp, :mp, :hp, :player_class, :to_next_level,
21
28
  :max_health, :max_mp
22
29
 
23
- def self.parse(stats)
24
- stats.tap do |s|
25
- s['player_class'] = s['class']
26
- end
27
- end
28
-
29
- private
30
-
31
- def stats
32
- @stats ||= OpenStruct.new(Stats.parse(client.class.get('/user')['stats']))
30
+ def initialize(api_data)
31
+ api_data['player_class'] = api_data['class'] # 'class' is reserved
32
+ @stats = OpenStruct.new(api_data)
33
33
  end
34
34
 
35
35
  end
@@ -5,21 +5,12 @@ class HabiticaClient
5
5
  # The authed user.
6
6
  class User < HabiticaClient::ApiBase
7
7
 
8
+ endpoint '/user/'
9
+
8
10
  require 'habitica_client/user/stats'
9
- require 'habitica_client/user/tasks'
10
11
 
11
- # Returns user's stats.
12
- #
13
- # @return [HabiticaClient::Stats]
14
12
  def stats
15
- @stats ||= Stats.new(client)
16
- end
17
-
18
- # Returns user's tasks.
19
- #
20
- # @return [HabiticaClient::Tasks]
21
- def tasks
22
- @tasks ||= Tasks.new(client)
13
+ @stats ||= Stats.new(data['stats'])
23
14
  end
24
15
 
25
16
  end
@@ -1,3 +1,3 @@
1
1
  class HabiticaClient
2
- VERSION = '0.0.7'
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -3,6 +3,7 @@ class HabiticaClient
3
3
  require 'habitica_client/version'
4
4
  require 'habitica_client/client'
5
5
  require 'habitica_client/user'
6
+ require 'habitica_client/tasks'
6
7
 
7
8
  attr_accessor :client
8
9
 
@@ -14,4 +15,8 @@ class HabiticaClient
14
15
  @user ||= User.new(client)
15
16
  end
16
17
 
18
+ def tasks
19
+ @tasks ||= Tasks.new(client)
20
+ end
21
+
17
22
  end
data/lib/hashup.rb CHANGED
@@ -4,7 +4,7 @@ module Hashup
4
4
  define_method(:to_h) do
5
5
 
6
6
  kv = attributes.map { |k| [k, send(k)] }
7
- .delete_if { |_k, v| v.nil? }
7
+ .delete_if { |_k, v| v.nil? }
8
8
 
9
9
  Hash[kv]
10
10
 
@@ -3,15 +3,30 @@ require 'spec_helper'
3
3
  properties = [:id, :text, :notes, :value, :checklist, :priority,
4
4
  :attribute, :tags]
5
5
 
6
- date_properties = [:date_completed, :date_created]
6
+ date_properties = [:created_at, :updated_at]
7
7
 
8
8
  describe 'Task', vcr: { cassette_name: 'task' } do
9
9
 
10
10
  let(:habitrpg) { HabiticaClient.new(USER_ID, API_TOKEN) }
11
- let(:tasks) { habitrpg.user.tasks }
11
+ let(:tasks) { habitrpg.tasks }
12
12
  let(:todo) { tasks.todos.last }
13
13
  let(:habit) { tasks.habits.last }
14
14
  let(:daily) { tasks.dailies.last }
15
+ dummy_tasks = nil
16
+
17
+ before do
18
+ dummy_tasks = {
19
+ todo: tasks.create(text: 'Testing 123', type: 'todo'),
20
+ habit: tasks.create(text: 'Testing 123', type: 'habit'),
21
+ daily: tasks.create(text: 'Testing 123', type: 'daily')
22
+ }
23
+ end
24
+
25
+ after do
26
+ # Don't leave tasks in my todo list!
27
+ dummy_tasks.each { |_k, t| t.delete }
28
+
29
+ end
15
30
 
16
31
  describe '#completed?' do
17
32
  it 'is the completed status' do
@@ -94,7 +109,7 @@ describe 'Task', vcr: { cassette_name: 'task' } do
94
109
 
95
110
  expect(task.text).to eq(updated_text)
96
111
 
97
- task.delete # Don't leave tasks in my todo list!
112
+ task.delete # Don't leave tasks in my todo list!
98
113
  end
99
114
 
100
115
  end
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe 'Tasks', vcr: { cassette_name: 'tasks' } do
4
4
 
5
5
  let(:habitrpg) { HabiticaClient.new(USER_ID, API_TOKEN) }
6
- let(:tasks) { habitrpg.user.tasks }
6
+ let(:tasks) { habitrpg.tasks }
7
7
 
8
8
  describe '.each' do
9
9
 
10
10
  it 'can be iterated' do
11
11
  tasks.each do |task|
12
- expect(task).to be_a(HabiticaClient::User::Task)
12
+ expect(task).to be_a(HabiticaClient::Task)
13
13
  end
14
14
  end
15
15
 
@@ -72,7 +72,7 @@ describe 'Tasks', vcr: { cassette_name: 'tasks' } do
72
72
  it 'can create a new task' do
73
73
  task = tasks.create(text: 'Testing 123', type: 'todo')
74
74
  expect(task.id).not_to be_nil
75
- task.delete # Don't leave tasks in my todo list!
75
+ task.delete # Don't leave tasks in my todo list!
76
76
  end
77
77
 
78
78
  end
@@ -9,24 +9,4 @@ describe 'User', :vcr do
9
9
  expect(user).to be_a(HabiticaClient::User)
10
10
  end
11
11
 
12
- describe '#stats' do
13
-
14
- subject { user.stats }
15
-
16
- it 'returns Stats' do
17
- expect(subject).to be_a(HabiticaClient::User::Stats)
18
- end
19
-
20
- end
21
-
22
- describe '#tasks' do
23
-
24
- subject { user.tasks }
25
-
26
- it 'returns Tasks' do
27
- expect(subject).to be_a(HabiticaClient::User::Tasks)
28
- end
29
-
30
- end
31
-
32
12
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative '../lib/habitica_client'
2
2
 
3
+ require 'awesome_print'
3
4
  require 'byebug'
4
5
  require 'vcr'
5
6
  require 'dotenv'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: habitica_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Mckellar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2016-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: '3.3'
167
+ - !ruby/object:Gem::Dependency
168
+ name: awesome_print
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.7'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.7'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: httparty
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -199,21 +213,20 @@ files:
199
213
  - lib/habitica_client/api_base.rb
200
214
  - lib/habitica_client/client.rb
201
215
  - lib/habitica_client/restful.rb
216
+ - lib/habitica_client/task.rb
217
+ - lib/habitica_client/task/date_accessor.rb
218
+ - lib/habitica_client/task/status.rb
219
+ - lib/habitica_client/task/type.rb
220
+ - lib/habitica_client/tasks.rb
221
+ - lib/habitica_client/tasks/status.rb
222
+ - lib/habitica_client/tasks/types.rb
202
223
  - lib/habitica_client/user.rb
203
224
  - lib/habitica_client/user/stats.rb
204
- - lib/habitica_client/user/task.rb
205
- - lib/habitica_client/user/task/date_accessor.rb
206
- - lib/habitica_client/user/task/status.rb
207
- - lib/habitica_client/user/task/type.rb
208
- - lib/habitica_client/user/tasks.rb
209
- - lib/habitica_client/user/tasks/status.rb
210
- - lib/habitica_client/user/tasks/types.rb
211
225
  - lib/habitica_client/version.rb
212
226
  - lib/hashup.rb
213
- - spec/cassettes/.keep
227
+ - spec/habit_client/task_spec.rb
228
+ - spec/habit_client/tasks_spec.rb
214
229
  - spec/habit_client/user/stats_spec.rb
215
- - spec/habit_client/user/task_spec.rb
216
- - spec/habit_client/user/tasks_spec.rb
217
230
  - spec/habit_client/user_spec.rb
218
231
  - spec/habit_client_spec.rb
219
232
  - spec/spec_helper.rb
@@ -238,15 +251,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
251
  version: '0'
239
252
  requirements: []
240
253
  rubyforge_project:
241
- rubygems_version: 2.2.2
254
+ rubygems_version: 2.4.5
242
255
  signing_key:
243
256
  specification_version: 4
244
257
  summary: Another Habitica client
245
258
  test_files:
246
- - spec/cassettes/.keep
259
+ - spec/habit_client/task_spec.rb
260
+ - spec/habit_client/tasks_spec.rb
247
261
  - spec/habit_client/user/stats_spec.rb
248
- - spec/habit_client/user/task_spec.rb
249
- - spec/habit_client/user/tasks_spec.rb
250
262
  - spec/habit_client/user_spec.rb
251
263
  - spec/habit_client_spec.rb
252
264
  - spec/spec_helper.rb
@@ -1,43 +0,0 @@
1
- require 'ostruct'
2
- require 'forwardable'
3
- require 'time'
4
- require 'hashup'
5
- require 'habitica_client/restful'
6
-
7
- class HabiticaClient::User
8
-
9
- # A user task, which can be a habit, daily, or todo.
10
- class Task < HabiticaClient::Restful
11
-
12
- require 'habitica_client/user/task/date_accessor'
13
- require 'habitica_client/user/task/status'
14
- require 'habitica_client/user/task/type'
15
-
16
- extend DateAccessor
17
- include Status
18
- include Type
19
- extend Hashup
20
-
21
- attr_accessor :id, :text, :notes, :value, :priority, :attribute,
22
- :type, :tags, :checklist, :value, :priority,
23
- :challenge, :down, :up, :history, :streak,
24
- :frequency, :history, :completed, :every_x, :repeat,
25
- :collapse_checklist
26
-
27
- date_accessor :date_created, :date_completed, :start_date, :date
28
-
29
- hashup :id, :text, :notes, :value, :priority, :attribute, :type,
30
- :tags, :checklist, :value, :priority, :challenge, :down,
31
- :up, :history, :streak, :frequency, :history, :completed,
32
- :every_x, :repeat, :collapse_checklist, :date_created,
33
- :date_completed, :start_date, :date
34
-
35
- private
36
-
37
- def endpoint
38
- '/user/tasks'
39
- end
40
-
41
- end
42
-
43
- end
data/spec/cassettes/.keep DELETED
File without changes