Completed
Push — master ( 3b0a10...0adc05 )
by Wallace
01:23
created

movies.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 78
ccs 23
cts 23
cp 1
crap 1
rs 8.9019
c 1
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A movies.js ➔ ... ➔ proto.get 0 3 1
A movies.js ➔ ... ➔ proto.setParams 0 13 3
A movies.js ➔ ... ➔ proto.findByGenre 0 3 1
A movies.js ➔ ... ➔ __.requestAPI 0 7 1
A movies.js ➔ ... ➔ proto.search 0 3 1
A movies.js ➔ ... ➔ proto.findByQuality 0 3 1
A movies.js ➔ ... ➔ proto.setEndpoint 0 11 3
A movies.js ➔ ... ➔ proto.findByRating 0 3 1
A movies.js ➔ ... ➔ proto.findByID 0 9 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Movies
3
 */
4
'use strict'
5
6 1
const request = require('request-promise')
7
8 1
const Movies = () => {
9 1
  const __ = {
10
    params: {},
11
12
    requestAPI: function (url) {
13 6
      return request({
14
        uri: url,
15
        qs: this.params,
16
        json: true
17
      })
18
    }
19
  }
20
21 1
  const proto = {
22
    endpoint: {
23
      list: 'https://yts.ag/api/v2/list_movies.json',
24
      details: 'https://yts.ag/api/v2/movie_details.json'
25
    },
26
27
    setEndpoint: function (endpoint) {
28 3
      if (typeof endpoint !== 'object') {
29 1
        throw new Error('endpoint must be an object')
30
      }
31
32 2
      for (let i in endpoint) {
33 2
        this.endpoint[i] = endpoint[i]
34
      }
35
36 2
      return this
37
    },
38
39
    setParams: function (params) {
40 6
      if (typeof params !== 'object') {
41 1
        throw new Error('params must be an object')
42
      }
43
44 5
      __.params = {}
45
46 5
      for (let i in params) {
47 7
        __.params[i] = params[i]
48
      }
49
50 5
      return this
51
    },
52
53
    get: function () {
54 5
      return __.requestAPI(this.endpoint.list)
55
    },
56
57
    findByGenre: function (genre) {
58 1
      return this.setParams({genres: genre}).get()
59
    },
60
61
    findByQuality: function (quality) {
62 1
      return this.setParams({quality: quality}).get()
63
    },
64
65
    findByRating: function (rating) {
66 1
      return this.setParams({minimum_rating: rating}).get()
67
    },
68
69
    search: function (term) {
70 1
      return this.setParams({query_term: term}).get()
71
    },
72
73
    findByID: function (id) {
74 1
      this.setParams({
75
        movie_id: id,
76
        with_images: true,
77
        with_cast: true
78
      })
79
80 1
      return __.requestAPI(this.endpoint.details)
81
    }
82
  }
83
84 1
  return Object.create(proto)
85
}
86
87
module.exports = Movies()
88