shortdiary 0.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 +7 -0
- data/lib/shortdiary.rb +180 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: f3104d6aecda1d8d6733cd29443e1ff7e9df75ed
|
|
4
|
+
data.tar.gz: d2ff92776697a45841b607e588da247d5a66a783
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2673782de719a25e1ac41b30c5b3e17a0a5b5148462f39a62904c264e7ca08edf71a44ba3f047114fadf4bbacabf45d84c96ede373096af06de6d152efbe9594
|
|
7
|
+
data.tar.gz: 693edd79a84cb909247ab61f3bdc7efca356cc411300b416360cc7628d4f09f652e3eb294c60bb743ecc67e8ac1654bfbd1592c40a1d8014014f5daefc98f2a5
|
data/lib/shortdiary.rb
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Shortdiary API Ruby bindings
|
|
2
|
+
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
# copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
# SOFTWARE.
|
|
20
|
+
|
|
21
|
+
require 'net/http'
|
|
22
|
+
require 'json'
|
|
23
|
+
require 'date'
|
|
24
|
+
|
|
25
|
+
API_ROOT = 'https://api.shortdiary.me/api/v1'
|
|
26
|
+
|
|
27
|
+
module Shortdiary
|
|
28
|
+
|
|
29
|
+
class Error < StandardError; end
|
|
30
|
+
class MissingDataError < StandardError; end
|
|
31
|
+
class ServerError < StandardError; end
|
|
32
|
+
class AuthenticationError < ServerError; end
|
|
33
|
+
|
|
34
|
+
class Post
|
|
35
|
+
attr_accessor :api, :text, :public_text, :mood, :date, :id, \
|
|
36
|
+
:location_verbose, :location_lat, :location_lon, :public
|
|
37
|
+
|
|
38
|
+
def initialize(*args)
|
|
39
|
+
# Should probably use a hash here
|
|
40
|
+
@api = args[0]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def attrs
|
|
44
|
+
instance_variables.map{|ivar| instance_variable_get ivar}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_s
|
|
48
|
+
return @text if @text.is_a?(String)
|
|
49
|
+
''
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def for_today?
|
|
53
|
+
@date == Date.today
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def save
|
|
57
|
+
if not @text or not @date or not @mood
|
|
58
|
+
raise MissingDataError
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
post_data = {
|
|
62
|
+
:date => @date.to_s,
|
|
63
|
+
:text => @text,
|
|
64
|
+
:mood => @mood,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if @id
|
|
68
|
+
new_post = @api.send_request("posts/#{@id}/", 'PUT', post_data)
|
|
69
|
+
else
|
|
70
|
+
new_post = @api.send_request("posts/", 'POST', post_data)
|
|
71
|
+
@id = new_post['id']
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class API
|
|
77
|
+
def handle_server_error(res)
|
|
78
|
+
begin
|
|
79
|
+
json_data = JSON.parse res.body
|
|
80
|
+
rescue JSON::ParserError
|
|
81
|
+
raise ServerError, res.body
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
raise AuthenticationError if res.is_a?(Net::HTTPUnauthorized)
|
|
85
|
+
raise ServerError, json_data['Error'] if json_data['Error']
|
|
86
|
+
|
|
87
|
+
# Fallback
|
|
88
|
+
raise ServerError, res.body
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Todo open one permanent connection and keep it alive
|
|
92
|
+
def send_request(api_endpoint, type = 'GET', data = {})
|
|
93
|
+
# Note: All 'real' API applications should probably use OAuth instead…
|
|
94
|
+
uri = URI("#{API_ROOT}/#{api_endpoint}")
|
|
95
|
+
|
|
96
|
+
case type
|
|
97
|
+
when 'POST' then req = Net::HTTP::Post.new(uri)
|
|
98
|
+
when 'PUT' then req = Net::HTTP::Put.new(uri)
|
|
99
|
+
else req = Net::HTTP::Get.new(uri)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
req.set_form_data(data) if type != 'GET'
|
|
103
|
+
req.basic_auth(@username, @password)
|
|
104
|
+
|
|
105
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
|
|
106
|
+
http.request(req)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
handle_server_error(res) if not res.is_a?(Net::HTTPSuccess)
|
|
110
|
+
JSON.parse res.body
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def get_request(api_endpoint)
|
|
114
|
+
send_request(api_endpoint, 'GET')
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def post_request(api_endpoint, data)
|
|
118
|
+
send_request(api_endpoint, 'POST', data)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def put_request(api_endpoint, data)
|
|
122
|
+
send_request(api_endpoint, 'PUT', data)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def initialize(*args)
|
|
126
|
+
@username = args[0]
|
|
127
|
+
@password = args[1]
|
|
128
|
+
|
|
129
|
+
# Send & discard request to check login credentials
|
|
130
|
+
get_request('public/')
|
|
131
|
+
|
|
132
|
+
nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Should probably be part of the Post class
|
|
136
|
+
def format_post(raw_post)
|
|
137
|
+
post = Post.new
|
|
138
|
+
|
|
139
|
+
# Not pretty, but it does the job.
|
|
140
|
+
raw_post.keys.select{ |key, _|
|
|
141
|
+
begin
|
|
142
|
+
post.send("#{key}=", raw_post[key])
|
|
143
|
+
rescue NoMethodError; end
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
post.date = Date.strptime(raw_post['date'], '%Y-%m-%d')
|
|
147
|
+
post.api = self
|
|
148
|
+
post
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# This doesn't really make any sense at all
|
|
152
|
+
def new_post()
|
|
153
|
+
created_post = Post.new
|
|
154
|
+
created_post.api = self
|
|
155
|
+
created_post
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def posts()
|
|
159
|
+
raw_posts = get_request('posts/')
|
|
160
|
+
api_posts = []
|
|
161
|
+
|
|
162
|
+
raw_posts.each {|raw_post|
|
|
163
|
+
api_posts << format_post(raw_post)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
api_posts
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def get_post_for(date)
|
|
170
|
+
# The API endpoint should allow filtering.
|
|
171
|
+
posts.select {|post| post.date == date }[0]
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def random_public()
|
|
175
|
+
post = get_request('public/')
|
|
176
|
+
format_post(post)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: shortdiary
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Lukas Martini
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2010-04-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Ruby bindings for the most commonly used functions of the Shortdiary
|
|
14
|
+
API
|
|
15
|
+
email: lutoma@ohai.su
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/shortdiary.rb
|
|
21
|
+
homepage: http://rubygems.org/gems/shortdiary
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
metadata: {}
|
|
25
|
+
post_install_message:
|
|
26
|
+
rdoc_options: []
|
|
27
|
+
require_paths:
|
|
28
|
+
- lib
|
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubyforge_project:
|
|
41
|
+
rubygems_version: 2.2.2
|
|
42
|
+
signing_key:
|
|
43
|
+
specification_version: 4
|
|
44
|
+
summary: Ruby bindings for the Shortdiary API
|
|
45
|
+
test_files: []
|