-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic test and implemented primitive me2day library
- implmented OpenStruct (recursively parse dictionaries) - naive Json library (TODO: error when content has null, false, true) - need to mock out tests
- Loading branch information
0 parents
commit 90dea58
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.swp | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/python | ||
import urllib2 | ||
|
||
class OpenStruct: | ||
def __init__(self, params=None): | ||
if params: | ||
self.parse(params) | ||
|
||
def parse(self, params, recursive=True): | ||
d = OpenStruct.parse_dictionary(params, recursive) | ||
for key, value in d.iteritems(): | ||
setattr(self, key, value) | ||
|
||
@staticmethod | ||
def parse(params, recursive=True): | ||
result = OpenStruct.parse_dictionary(params, recursive) | ||
def pack_open_struct(param): | ||
o = OpenStruct() | ||
for key, value in param.iteritems(): | ||
setattr(o, key, value) | ||
return o | ||
# list of objects | ||
if isinstance(result, list): | ||
return [pack_open_struct(d) for d in result] | ||
# single object | ||
else: | ||
return pack_open_struct(result) | ||
|
||
@staticmethod | ||
def parse_dictionary(params, recursive=True): | ||
# list of objects | ||
if isinstance(params, list): | ||
return [OpenStruct.parse_dictionary(d) for d in params] | ||
# single object | ||
elif isinstance(params, dict): | ||
def nestable(value): | ||
return isinstance(value, dict) and recursive | ||
d = {} | ||
for key, value in params.iteritems(): | ||
# nest if value is dictionary | ||
if nestable(value): | ||
value = OpenStruct.parse(value, recursive) | ||
# check if value is list with dictionary inside | ||
elif isinstance(value, list): | ||
value = [ | ||
OpenStruct.parse(v, recursive) \ | ||
if nestable(v) else v \ | ||
for v in value[:] \ | ||
] | ||
d[key] = value | ||
return d | ||
else: | ||
return params | ||
|
||
|
||
class Json: | ||
@staticmethod | ||
def parse(data): | ||
data = data.replace('null', 'None') \ | ||
.replace('false', 'False') \ | ||
.replace('true', 'True') | ||
return eval(data) | ||
|
||
|
||
class Me2day: | ||
@staticmethod | ||
def posts(username, **params): | ||
url = 'http://me2day.net/api/get_posts/%s' % username | ||
posts = Me2day.fetch_resource(url, params) | ||
return posts | ||
|
||
@staticmethod | ||
def fetch_resource(url, param={}): | ||
url += '.json' | ||
if param: | ||
query = '&'.join(['='.join([k,v]) for k,v in param.items()]) | ||
url = '%s?%s' % (url, query) | ||
url = url.replace(' ', '%20') | ||
# fetch from me2day | ||
data = urllib2.urlopen(url).read() | ||
data = Json.parse(data) | ||
return OpenStruct.parse(data) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/python | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/python | ||
|
||
import unittest | ||
from me2day import Me2day | ||
from me2day import OpenStruct | ||
|
||
class ParseJsonTestCase(unittest.TestCase): | ||
pass | ||
|
||
class OpenStructTestCase(unittest.TestCase): | ||
def test_parse_dictionary_to_object(self): | ||
d = { | ||
'tags': ['woc', 'abc', 'def'], | ||
'ooo': 'xxx' | ||
} | ||
o = OpenStruct.parse(d) | ||
assert o.tags == d['tags'] | ||
assert o.ooo == d['ooo'] | ||
|
||
def test_parse_array_of_dictionary_to_object(self): | ||
ds = [ | ||
{ | ||
'tags': ['woc1', 'abc1', 'def1'], | ||
'ooo': 'xxx1' | ||
}, | ||
{ | ||
'tags': ['woc2', 'abc2', 'def2'], | ||
'ooo': 'xxx2' | ||
} | ||
] | ||
os = OpenStruct.parse(ds) | ||
assert isinstance(os, list) | ||
assert isinstance(os[0], OpenStruct) | ||
assert len(os) == 2 | ||
assert os[-1].ooo == 'xxx2' | ||
|
||
def test_parse_nested_dictionary_to_nested_object(self): | ||
d = { | ||
'tags': ['woc', 'abc', 'def'], | ||
'ooo': 'xxx', | ||
'nested': { | ||
'url': 'www.some.where/over?the=rainbow' | ||
} | ||
} | ||
o = OpenStruct.parse(d) | ||
assert isinstance(o.nested, OpenStruct) | ||
assert o.nested.url == d['nested']['url'] | ||
|
||
def test_parse_nested_list_of_dictionaries_to_nested_list_of_ojects(self): | ||
d = { | ||
'ooo': 'xxx', | ||
'tags': ['woc', 'abc', 'def'], | ||
'nested': [ | ||
{'url': 'www.some.where/over?the=rainbow'}, | ||
{'url': 'www.some.otherplace/over?the=cloud'} | ||
] | ||
} | ||
o = OpenStruct.parse(d) | ||
assert isinstance(o.nested[0], OpenStruct) | ||
assert o.nested[0].url == d['nested'][0]['url'] | ||
|
||
|
||
class Me2dayApiTestCase(unittest.TestCase): | ||
def test_get_recent_posts_by_username(self): | ||
user = 'jangxyz' | ||
posts = Me2day.posts(username=user) | ||
assert isinstance(posts, list) | ||
assert isinstance(posts[0].body, str) | ||
|
||
def test_get_recent_posts_by_username_and_tags(self): | ||
user = 'jangxyz' | ||
tag = 'woc' | ||
posts = Me2day.posts(username=user, tag=tag) | ||
assert tag in map(lambda x: x.name, posts[0].tags) | ||
|
||
def test_encode_korean_correctly(self): | ||
result = """ | ||
""" | ||
pass | ||
|
||
#class GetPageBySpringnoteApiTestCase(unittest.TestCase): | ||
# def test_get_springnote_page_by_page_id(self): | ||
# note = 'springmemo' | ||
# page_id = 3 | ||
# page = Springnote.page(note=note, page_id=page_id) | ||
# page.source | ||
# | ||
#class SetPageBySpringnoteApiTestCase(unittest.TestCase): | ||
# def test_set_springnote_page(self): | ||
# note = 'springmemo' | ||
# page_id = 3 | ||
# page = Springnote.page(note=note, page_id=page_id) | ||
# page.source = "something new" | ||
# page.save() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|