Skip to content

Commit

Permalink
add users recent_observation_fields endpoint; fix lint warnings; incr…
Browse files Browse the repository at this point in the history
…ease test coverage
  • Loading branch information
pleary committed Feb 6, 2024
1 parent 9cbfcb1 commit 9951a05
Show file tree
Hide file tree
Showing 29 changed files with 907 additions and 151 deletions.
112 changes: 72 additions & 40 deletions build/inaturalistjs.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/endpoints/computervision.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const computervision = class computervision {
return iNaturalistAPI.upload( "computervision/score_image", params, options )
.then( response => {
response.results = response.results.map( r => (
Object.assign( { }, r, { taxon: new Taxon( r.taxon ) } )
{ ...r, taxon: new Taxon( r.taxon ) }
) );
if ( response.common_ancestor ) {
response.common_ancestor.taxon = new Taxon( response.common_ancestor.taxon );
Expand All @@ -19,12 +19,12 @@ const computervision = class computervision {
}

static score_observation( params, opts = { } ) { // eslint-disable-line camelcase
const options = Object.assign( { }, opts );
const options = { ...opts };
options.useAuth = true;
return iNaturalistAPI.get( "computervision/score_observation/:id", params, options )
.then( response => {
response.results = response.results.map( r => (
Object.assign( { }, r, { taxon: new Taxon( r.taxon ) } )
{ ...r, taxon: new Taxon( r.taxon ) }
) );
if ( response.common_ancestor ) {
response.common_ancestor.taxon = new Taxon( response.common_ancestor.taxon );
Expand Down
8 changes: 6 additions & 2 deletions lib/endpoints/controlled_terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ const ControlledTerm = require( "../models/controlled_term" );

const typifyResponse = response => {
const typifiedResponse = ControlledTerm.typifyResultsResponse( response );
if ( !typifiedResponse.results ) {
return typifiedResponse;
}

for ( let i = 0; i < typifiedResponse.results.length; i += 1 ) {
if ( typifiedResponse.results[i] && !typifiedResponse.results[i].values ) {
if ( typifiedResponse.results[i] && typifiedResponse.results[i].values ) {
typifiedResponse.results[i].values = typifiedResponse.results[i].values.map(
v => ( new ControlledTerm( v ) )
);
Expand All @@ -17,7 +21,7 @@ const controlledTerms = class controlledTerms { // eslint-disable-line camelcase
static for_taxon( params, opts = { } ) { // eslint-disable-line camelcase
if ( iNaturalistAPI.apiURL && iNaturalistAPI.apiURL.match( /\/v2/ ) ) {
const taxonIds = params.taxon_id.toString( ).split( "," ).join( "," );
const newParams = Object.assign( {}, params );
const newParams = { ...params };
delete newParams.taxon_id;
return iNaturalistAPI.get( `controlled_terms/for_taxon/${taxonIds}`, newParams, opts ).then( typifyResponse );
}
Expand Down
20 changes: 10 additions & 10 deletions lib/endpoints/identifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ const identifications = class identifications {
return iNaturalistAPI.delete( "identifications/:id", params, options );
}

static similar_species( params, opts ) { // eslint-disable-line camelcase
const options = Object.assign( { }, opts || { } );
static similar_species( params, opts = { } ) { // eslint-disable-line camelcase
const options = { ...opts };
options.useAuth = true;
return iNaturalistAPI.get( "identifications/similar_species", params, options )
.then( response => {
if ( response.results ) {
response.results = response.results.map( r => (
Object.assign( { }, r, { taxon: new Taxon( r.taxon ) } )
{ ...r, taxon: new Taxon( r.taxon ) }
) );
}
return response;
} );
}

static recent_taxa( params, opts ) { // eslint-disable-line camelcase
const options = Object.assign( { }, opts || { } );
static recent_taxa( params, opts = { } ) { // eslint-disable-line camelcase
const options = { ...opts };
options.useAuth = true;
return iNaturalistAPI.get( "identifications/recent_taxa", params, options )
.then( response => {
if ( response.results ) {
response.results = response.results.map( res => {
const r = Object.assign( { }, res );
const r = { ...res };
r.taxon = new Taxon( r.taxon );
r.identification = new Identification( r.identification );
if ( r.identification?.observation?.identifications ) {
Expand All @@ -54,14 +54,14 @@ const identifications = class identifications {
} );
}

static recent_taxa_revisited( params, opts ) { // eslint-disable-line camelcase
const options = Object.assign( { }, opts || { } );
static recent_taxa_revisited( params, opts = { } ) { // eslint-disable-line camelcase
const options = { ...opts };
options.useAuth = true;
return iNaturalistAPI.get( "identifications/recent_taxa_revisited", params, options )
.then( response => {
if ( response.results ) {
response.results = response.results.map( res => {
const r = Object.assign( { }, res );
const r = { ...res };
r.taxon = new Taxon( r.taxon );
r.identification = new Identification( r.identification );
if ( r.identification?.observation?.identifications ) {
Expand All @@ -80,7 +80,7 @@ const identifications = class identifications {
.then( response => {
if ( response.results ) {
response.results = response.results.map( r => (
Object.assign( { }, r, { user: new User( r.user ) } )
{ ...r, user: new User( r.user ) }
) );
}
return response;
Expand Down
21 changes: 12 additions & 9 deletions lib/endpoints/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ const messages = class messages {
.then( Message.typifyInstanceResponse );
}

static search( params, options ) {
const opts = Object.assign( {}, options, {
static search( params, options = { } ) {
const opts = {
...options,
useWriteApi: true,
useAuth: true
} );
};
return iNaturalistAPI.get( "messages", params, opts )
.then( Message.typifyResultsResponse );
}

static fetch( params, options ) {
const opts = Object.assign( {}, options, {
static fetch( params, options = { } ) {
const opts = {
...options,
useWriteApi: true,
useAuth: true
} );
};
return iNaturalistAPI.get( "messages/:id", params, opts )
.then( Message.typifyResultsResponse );
}
Expand All @@ -29,11 +31,12 @@ const messages = class messages {
return iNaturalistAPI.delete( "messages/:id", params, options );
}

static unread( params, options ) {
const opts = Object.assign( {}, options, {
static unread( params, options = { } ) {
const opts = {
...options,
useWriteApi: true,
useAuth: true
} );
};
return iNaturalistAPI.get( "messages/count", params, opts );
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/endpoints/observation_photos.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const observationPhotos = class observationPhotos {
return iNaturalistAPI.upload( "observation_photos", params, options );
}

static update( params, opts ) {
const options = Object.assign( { }, opts );
static update( params, opts = { } ) {
const options = { ...opts };

if ( iNaturalistAPI.apiURL && iNaturalistAPI.apiURL.match( /\/v2/ ) ) {
return iNaturalistAPI.put( "observation_photos/:id", params, options );
Expand Down
4 changes: 2 additions & 2 deletions lib/endpoints/observation_sounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const observationSounds = class observationSounds {
return iNaturalistAPI.upload( "observation_sounds", params, options );
}

static update( params, opts ) {
const options = Object.assign( { }, opts );
static update( params, opts = { } ) {
const options = { ...opts };

if ( iNaturalistAPI.apiURL && iNaturalistAPI.apiURL.match( /\/v2/ ) ) {
return iNaturalistAPI.put( "observation_sounds/:id", params, options );
Expand Down
1 change: 0 additions & 1 deletion lib/endpoints/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const observations = class observations {
}

static unfave( params, options ) {
// return observations.unvote( params, options );
if ( !iNaturalistAPI.apiURL || !iNaturalistAPI.apiURL.match( /\/v2/ ) ) {
return observations.unvote( params, options );
}
Expand Down
16 changes: 11 additions & 5 deletions lib/endpoints/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const projects = class projects {

static update( params, options ) {
return iNaturalistAPI
.upload( "projects/:id", params, Object.assign( { }, options, { method: "put" } ) )
.upload( "projects/:id", params, { ...options, method: "put" } )
.then( Project.typifyInstanceResponse );
}

Expand Down Expand Up @@ -65,8 +65,11 @@ const projects = class projects {
}

static subscriptions( params, options ) {
return iNaturalistAPI.get( "projects/:id/subscriptions", params,
iNaturalistAPI.optionsUseAuth( options ) );
return iNaturalistAPI.get(
"projects/:id/subscriptions",
params,
iNaturalistAPI.optionsUseAuth( options )
);
}

static followers( params, options ) {
Expand All @@ -78,8 +81,11 @@ const projects = class projects {
}

static membership( params, options ) {
return iNaturalistAPI.get( "projects/:id/membership", params,
iNaturalistAPI.optionsUseAuth( options ) );
return iNaturalistAPI.get(
"projects/:id/membership",
params,
iNaturalistAPI.optionsUseAuth( options )
);
}

static feature( params, options ) {
Expand Down
6 changes: 6 additions & 0 deletions lib/endpoints/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ const users = class users {
...opts, useAuth: true
} );
}

static recent_observation_fields( params, opts = { } ) {
return iNaturalistAPI.get( "users/recent_observation_fields", params, {
...opts, useAuth: true
} );
}
};

module.exports = users;
4 changes: 2 additions & 2 deletions lib/models/authorized_application.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Model = require( "./model" );

const AuthorizedApplication = class AuthorizedApplication extends Model {
static typifyInstanceResponse( response ) {
return super.typifyInstanceResponse( response, AuthorizedApplication );
static typifyResultsResponse( response ) {
return super.typifyResultsResponse( response, AuthorizedApplication );
}
};

Expand Down
7 changes: 3 additions & 4 deletions lib/models/conservation_status.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const Model = require( "./model" );

const ConservationStatus = class ConservationStatus extends Model {
static typifyInstanceResponse( response ) {
return super.typifyInstanceResponse( response, ConservationStatus );
}

iucnStatus( ) {
switch ( this.iucn ) {
case 0:
Expand Down Expand Up @@ -53,6 +49,9 @@ const ConservationStatus = class ConservationStatus extends Model {
case "Norma Oficial 059":
return this.normaStatus( );
default:
if ( !this.status ) {
return this.status;
}
switch ( this.status.toLowerCase( ) ) {
case "se":
case "fe":
Expand Down
4 changes: 2 additions & 2 deletions lib/models/provider_authorization.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Model = require( "./model" );

const ProviderAuthorization = class ProviderAuthorization extends Model {
static typifyInstanceResponse( response ) {
return super.typifyInstanceResponse( response, ProviderAuthorization );
static typifyResultsResponse( response ) {
return super.typifyResultsResponse( response, ProviderAuthorization );
}
};

Expand Down
16 changes: 16 additions & 0 deletions lib/test_helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const iNaturalistAPI = require( "./inaturalist_api" );

const testHelper = class testHelper {
static mockResponse( uri ) {
return {
Expand Down Expand Up @@ -161,6 +163,20 @@ const testHelper = class testHelper {
]
} );
}

static v1ToV2( ) {
iNaturalistAPI.setConfig( {
apiURL: iNaturalistAPI.apiURL.replace( "/v1", "/v2" ),
writeApiURL: iNaturalistAPI.apiURL.replace( "/v1", "/v2" )
} );
}

static v2ToV1( ) {
iNaturalistAPI.setConfig( {
apiURL: iNaturalistAPI.apiURL.replace( "/v2", "/v1" ),
writeApiURL: iNaturalistAPI.apiURL.replace( "/v2", "/v1" )
} );
}
};

module.exports = testHelper;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inaturalistjs",
"version": "2.9.0",
"version": "2.10.0",
"description": "inaturalistjs",
"author": "iNaturalist",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions test/endpoints/authorized_applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe( "AuthorizedApplications", ( ) => {
} );
authorizedApplications.search( ).then( r => {
nockScope.done( );
expect( r.results[0].constructor.name ).to.eq( "AuthorizedApplication" );
expect( r.results[0].application.id ).to.eq( stub.application.id );
expect( r.results[0].application.name ).to.eq( stub.application.name );
expect( r.results[0].created_at ).to.eq( stub.created_at );
Expand Down
Loading

0 comments on commit 9951a05

Please sign in to comment.