Cleaner and more modular code by using smarter shared URLs.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2016-12-10 21:28:36 +00:00 committed by SantiagoBragagnolo
parent 9ea0fde422
commit 22650f8444
2 changed files with 66 additions and 61 deletions

View File

@ -0,0 +1,17 @@
"
I model the information of the Zotero API. I collaborate with other objects to define repetitive API urls.
"
Class {
#name : #ZoteroAPI,
#superclass : #Object,
#category : #Zotero
}
{ #category : #'as yet unclassified' }
ZoteroAPI class >> baseURL [
"I provide the base URL where the API services are provided.
For more information visit:
"
^ 'https://api.zotero.org/'
]

View File

@ -15,27 +15,32 @@ Class {
{ #category : #querying }
ZoteroLibrary >> collections [
"I return all the public collections for a particular group or user"
groupID ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/collections') contents ].
^ NeoJSONReader fromString: (ZnEasy get: self collectionsURL) contents
]
{ #category : #utility }
ZoteroLibrary >> collectionsURL [
"I provide the common part of the collections URL for the Zotero API"
groupID ifNotNil: [ ^ ZoteroAPI baseURL,'groups/',groupID,'/collections/' ].
userID
ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/users/',userID,'/collections') contents ]
ifNil:
[ self inform: 'groupID or userID should be non-emtpy. Please fill them to get available collections'.
^ self ].
ifNotNil: [ ^ ZoteroAPI baseURL,'groups/',userID,'/collections/' ]
ifNil: [ self emptyValuesWarning ]
]
{ #category : #utility }
ZoteroLibrary >> emptyValuesWarning [
self inform: 'WARNING! groupID or userID should be non-emtpy. Please fill them to get available collections'.
^ self
]
{ #category : #querying }
ZoteroLibrary >> generalInfo [
"I return the last 25 the public collections for a particular group or user"
groupID ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID) contents ].
userID
ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/users/',userID) contents ]
ifNil:
[ self inform: 'groupID or userID should be non-emtpy. Please fill them to get the general information'.
^ self ].
^ NeoJSONReader fromString: (ZnEasy get: self groupURL) contents.
]
{ #category : #accessing }
@ -44,77 +49,60 @@ ZoteroLibrary >> groupID [
]
{ #category : #accessing }
ZoteroLibrary >> groupID: anObject [
groupID := anObject
ZoteroLibrary >> groupID: aString [
groupID := aString
]
{ #category : #utility }
ZoteroLibrary >> groupURL [
"I provide the common part of the group URL for the Zotero API"
groupID ifNotNil: [ ^ ZoteroAPI baseURL,'groups/',groupID,'/' ].
userID
ifNotNil: [ ^ ZoteroAPI baseURL,'groups/',userID,'/' ]
ifNil: [ self emptyValuesWarning ]
]
{ #category : #querying }
ZoteroLibrary >> items [
ZoteroLibrary >> lastItems [
"I return the last 25 items of the public collections for a particular group or user"
groupID ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/items') contents ].
userID
ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/users/',userID,'/items') contents ]
ifNil:
[ self inform: 'groupID or userID should be non-emtpy. Please fill them to get related items'.
^ self ].
^ NeoJSONReader fromString: (ZnEasy get: self groupURL,'items') contents
]
{ #category : #querying }
ZoteroLibrary >> subcollection: keyString [
"I give the general info for a subcollection, given is alphanumeric key"
^ NeoJSONReader fromString: (ZnEasy get: (self subcollectionURL:keyString)) contents.
groupID ifNotNil:
[ ^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/collections/',keyString) contents ].
userID
ifNotNil: [
^ NeoJSONReader fromString: (ZnEasy get: 'https://api.zotero.org/users/',userID,'/collections/',keyString) contents ]
ifNil: [ self inform: 'groupID or userID should be not nil. Please fill them to get a proper answer.'.
^ self ]
]
{ #category : #querying }
ZoteroLibrary >> subcollectionAsBibTeX: keyString [
"I give all the items for a subcollection, given is alphanumeric key, excluding notes and attachments, exported as bibtex"
groupID ifNotNil:
[ ^ (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/collections/',keyString,'/items?itemType=-note%20 || attachment &format=bibtex') contents utf8Decoded].
userID
ifNotNil: [
^ ZnEasy get: 'https://api.zotero.org/users/',userID,'/collections/',keyString,'/items?itemType=-note || attachment%20&format=bibtex' contents utf8Decoded]
ifNil: [ self inform: 'groupID or userID should be not nil. Please fill them to get a proper answer.'.
^ self ]
^ (ZnEasy get: (self subcollectionURL: keyString,'/items?itemType=-note || attachment &format=bibtex')) contents utf8Decoded.
]
{ #category : #querying }
ZoteroLibrary >> subcollectionItems: keyString [
"I give all the items for a subcollection, given is alphanumeric key, including notes, attachments"
groupID ifNotNil:
[ ^ NeoJSONReader
fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/collections/',keyString,'/items') contents ].
userID
ifNotNil: [
^ NeoJSONReader fromString:
(ZnEasy get: 'https://api.zotero.org/users/',userID,'/collections/',keyString,'/items') contents ]
ifNil: [ self inform: 'groupID or userID should be not nil. Please fill them to get a proper answer.'.
^ self ]
^ NeoJSONReader fromString: (ZnEasy get: (self subcollectionURL: keyString,'/items')) contents.
]
{ #category : #querying }
ZoteroLibrary >> subcollectionParentItems: keyString [
"I give all the items for a subcollection, given is alphanumeric key, excluding notes and attachments"
^ NeoJSONReader fromString: (ZnEasy get: (self subcollectionURL:keyString,'/items?itemType=-note || attachment')) contents.
]
groupID ifNotNil:
[ ^ NeoJSONReader
fromString: (ZnEasy get: 'https://api.zotero.org/groups/',groupID,'/collections/',keyString,'/items?itemType=-note || attachment') contents ].
userID
ifNotNil: [
^ NeoJSONReader fromString:
(ZnEasy get: 'https://api.zotero.org/users/',userID,'/collections/',keyString,'/items?itemType=-note || attachment') contents ]
ifNil: [ self inform: 'groupID or userID should be not nil. Please fill them to get a proper answer.'.
^ self ]
{ #category : #utility }
ZoteroLibrary >> subcollectionURL: keyString [
"I provide the common part of the subcollection URL for the Zotero API, given its key string"
^ self collectionsURL,keyString
]
{ #category : #accessing }
@ -123,6 +111,6 @@ ZoteroLibrary >> userID [
]
{ #category : #accessing }
ZoteroLibrary >> userID: anObject [
userID := anObject
ZoteroLibrary >> userID: aString [
userID := aString
]