Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Api v4 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ gem "exifr", ">=1.1.3"
gem "mail", ">=2.4.4"
gem "rest-client", ">=1.6.7"
gem "rmagick", ">=2.13.1"
gem "unicode_utils"
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
Localwiki Email Uploader
LocalWiki Email Uploader
========================

Localwiki content uploader from email using [Localwiki API] and custom API(see About Custom API).
Email sender's address required in relation to User and API Key.
Email subject is used Localwiki's page name.
If Localwiki's page doesn't exist, create page and map, and upload jpeg file.
If Localwiki's page exist, upload jpeg file, and modify page(don't modify map).
LocalWiki content uploader from email using [LocalWiki API].
Email subject is used LocalWiki's page name.
If LocalWiki's page doesn't exist, create page and map, and upload jpeg file.
If LocalWiki's page exist, upload jpeg file, and modify page(don't modify map).

[Localwiki API]: http://localwiki.readthedocs.org/en/latest/api.html "API Documentation"

## About Custom API

Custom API is available at [Our branch].
This API allow access to get API key.
Please check [commit for api].

[Our branch]: https://github.com/Georepublic/localwiki "Georepublic's master_ja branch"
[commit for api]: https://github.com/Georepublic/localwiki/commit/29cf7ad5e0d846f617e12c46a0ac5fe35652b459 "add custom API for customer"
[LocalWiki API]: http://localwiki.readthedocs.org/en/latest/api.html "API Documentation"

## Setup

Expand All @@ -43,13 +33,15 @@ If you setup in Ubuntu, see also: [Setup Email Uploader in Ubuntu]

## app_settings.rb

ex: Localwiki's instance run "http://example.com" and create user "mail" and generate api_key "xxx" and add tag "frommail".
ex: LocalWiki's instance run "http://example.com" and create user "mail" and generate api_key "xxx" and add tag "frommail".

def get_setting
return {
:base_url => 'http://example.com',
:base_url => 'http://localwiki.net/api/v4',
:user_name => 'mailuser',
:api_key => 'xxx',
# Pick a region URL here -
:region => 'http://localwiki.net/api/v4/regions/2/',
:tag_slug => 'frommail'
}
end
Expand Down
4 changes: 3 additions & 1 deletion api_settings.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

def get_setting
return {
:base_url => '<server url>',
:base_url => 'http://localwiki.net/api/v4',
:user_name => '<user name>',
# Pick a region URL here
:region => 'http://localwiki.net/api/v4/regions/2/',
:api_key => '<api key>',
:tag_slug => '<tag name>'
}
Expand Down
80 changes: 32 additions & 48 deletions localwiki_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
require 'rest_client'
require 'json'
require 'cgi'
require 'uri'

class LocalWikiClientBase

def initialize args
@base_url = args[:base_url] or raise ArgumentError, "must need :base_url"
@user_name = args[:user_name]
@api_key = args[:api_key]
end

Expand All @@ -28,9 +28,13 @@ def headers

def exist?(page_or_id)
begin
response = RestClient.get @base_url + api_path + CGI.escape(page_or_id), headers
response = RestClient.get @base_url + api_path + '?slug__icontains=' + URI.escape(page_or_id), headers
if response.code == 200
return JSON.parse(response.to_str)
data = JSON.parse(response.to_str)
if data["count"] == 0
return nil
end
return data["results"][0]
end
rescue => e
puts e
Expand All @@ -39,7 +43,7 @@ def exist?(page_or_id)
end

def create(obj)
raise RuntimeError, "must set user_name and api_key" unless can_post?
raise RuntimeError, "must set api_key" unless can_post?
puts JSON.dump(obj)
begin
response = RestClient.post @base_url + api_path, JSON.dump(obj), headers
Expand All @@ -52,11 +56,25 @@ def create(obj)
return false
end

def update(page_or_id, obj)
raise RuntimeError, "must set user_name and api_key" unless can_post?
def update(page_url, obj)
raise RuntimeError, "must set api_key" unless can_post?
puts JSON.dump(obj)
begin
response = RestClient.put @base_url + api_path + CGI.escape(page_or_id), JSON.dump(obj), headers
response = RestClient.put page_url, JSON.dump(obj), headers
if response.code == 204
return true
end
rescue => e
puts "Unable update because #{e.message}"
end
return false
end

def patch(page_url, obj)
raise RuntimeError, "must set api_key" unless can_post?
puts JSON.dump(obj)
begin
response = RestClient.patch page_url, JSON.dump(obj), headers
if response.code == 204
return true
end
Expand Down Expand Up @@ -119,35 +137,35 @@ def make_query(objs)
end

def can_post?
return false if @user_name.blank? or @api_key.blank?
return false if @api_key.blank?
return true
end

def authorization_header
return nil unless can_post?
return "ApiKey #{@user_name}:#{@api_key}"
return "Token #{@api_key}"
end

end

class LocalWikiPage < LocalWikiClientBase

def api_path
"/api/page/"
"/pages/"
end

end

class LocalWikiFile < LocalWikiClientBase

def api_path
"/api/file/"
"/files/"
end

def upload(file_path, file_name, slug)
def upload(file_path, file_name, slug, region)

begin
response = RestClient.post @base_url + api_path, {:file => File.new(file_path, 'rb'), :name => file_name, :slug => slug}, headers
response = RestClient.post @base_url + api_path, {:file => File.new(file_path, 'rb'), :name => file_name, :slug => slug, :region => region}, headers
rescue => e
puts e
end
Expand All @@ -157,41 +175,7 @@ def upload(file_path, file_name, slug)
class LocalWikiMap < LocalWikiClientBase

def api_path
"/api/map/"
end

end

# for custom api
class LocalWikiUsersWithKey < LocalWikiClientBase

def api_path
"/api/users_with_apikey/"
end

end

# for custom api
class LocalWikiApiKey < LocalWikiClientBase

def api_path
"/api/api_key/"
end

end

class LocalWikiTag < LocalWikiClientBase

def api_path
"/api/tag/"
end

end

class LocalWikiPageTags < LocalWikiClientBase

def api_path
"/api/page_tags/"
"/maps/"
end

end
Loading