nbs 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +50 -1
  3. data/lib/nbs/version.rb +1 -1
  4. data/lib/nbs.rb +43 -26
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc373bcc737ec2ab1a1f998d9634de3a192493db
4
- data.tar.gz: 8a2ed621f77860cbf49abd2f403b25f67d914bae
3
+ metadata.gz: 0ea55a957b2a59467dab8a568e576cb39e0800d8
4
+ data.tar.gz: f8d837eb05a9a80a7037cd6653c6e35342073cae
5
5
  SHA512:
6
- metadata.gz: 5852df57f8d0c8f990be7c7aae9116885f53b3e12de6844f2eb203dabb232457685ae65717406266719cd30d26c00237ba98c5633a6010af1f57acf0df976f53
7
- data.tar.gz: f7e2d4f494a4361e8b36a2c0aae9bc4698d51304ffb004df7d783a6b7df66b10969d6f6af16a2770d710ea7d03c8894724b605a4bf36f93b4bc43ecb3efb51a7
6
+ metadata.gz: 42496492938c9f415693e45ff7cca1c741d9c9e7ec89e53e78757c3e463c788cdbaf7ecdc0b17504ac7868a80b995701835f3cca86e8eb32f2a90d5ed296e513
7
+ data.tar.gz: 12be612e9c9e7f964cbb57d0921e10b5db28af77c4dc0db4169c1fd817555ea837d0f22aececd699e06bc34887864f83d63a7734d89f7f9c6f544246d995e1ca
data/README.md CHANGED
@@ -55,7 +55,7 @@ results in
55
55
  ]
56
56
  ```
57
57
 
58
- ### Metrics
58
+ ### Metrics and Profiles
59
59
  instance based on nbs id
60
60
  ```
61
61
  default_options = {
@@ -106,6 +106,55 @@ available metrics via gem (more available through api)
106
106
 
107
107
  Typically results go as far back as 3 months
108
108
 
109
+ `nbs.artist_profiles` returns
110
+
111
+ ```
112
+ [
113
+ [0] {
114
+ :nbs_profle_id => "9946716",
115
+ :url => "http://www.facebook.com/therealrunthejewels"
116
+ },
117
+ [1] {
118
+ :nbs_profle_id => "9946718",
119
+ :url => "http://twitter.com/runjewels"
120
+ },
121
+ [2] {
122
+ :nbs_profle_id => "9946719",
123
+ :url => "http://www.youtube.com/channel/UCeveumRTn2o--9j1Xz2KUCQ"
124
+ },
125
+ [3] {
126
+ :nbs_profle_id => "16209803",
127
+ :url => "http://instagram.com/runthejewels"
128
+ }
129
+ ]
130
+ ```
131
+
132
+ ### Metric
133
+ ```
134
+ Nbs.metric('9946716', {})
135
+ default_options = { start: (Time.now - 3.day).to_i,
136
+ end: Time.now.to_i,
137
+ metric: 'fans' }
138
+ ```
139
+ returns metrics for a single profile. eg:
140
+ ```
141
+ [
142
+ [0] {
143
+ :time => 2017-07-09 00:00:00 UTC,
144
+ :volume => 64608
145
+ },
146
+ [1] {
147
+ :time => 2017-07-10 00:00:00 UTC,
148
+ :volume => 64737
149
+ },
150
+ [2] {
151
+ :time => 2017-07-11 00:00:00 UTC,
152
+ :volume => 64903
153
+ }
154
+ ]
155
+ ```
156
+
157
+
109
158
  ## Development
110
159
 
111
160
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/nbs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Nbs
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/nbs.rb CHANGED
@@ -6,56 +6,73 @@ require 'active_support/all'
6
6
  class Nbs
7
7
  include HTTParty
8
8
  extend Configuration
9
+ attr_accessor :all_metrics_raw
9
10
 
11
+ SERVICES = %w(facebook youtube twitter instagram)
10
12
  # define_setting :access_key
11
13
  # define_setting :least_favorite_liquid, "seltzer water"
12
14
 
13
- def initialize(_nbs_id=356, _options = {}) # kanye west
15
+ def initialize(_nbs_id=1076707, _options = {}) # run the jewels
14
16
  @nbs_id = "#{_nbs_id}.json"
15
- @options = {from: (Time.now - 2.day).to_i, to: Time.now.to_i}.merge(_options)
17
+ @options = {start: (Time.now - 7.day).to_i, end: Time.now.to_i, metric: 'fans'}.merge(_options)
16
18
  end
17
19
 
18
- def all_metrics
20
+ def metrics
19
21
  @all_metrics_raw ||= self.class.post("/metrics/artist/#{@nbs_id}", @options).parsed_response
20
- if @tidied.blank?
21
- @tidied = {}
22
+ if @metrics.blank?
23
+ @metrics = {}
22
24
  @all_metrics_raw.each do |am|
23
- @tidied[am["Service"]["name"].downcase] = am["Metric"] if am["Metric"].present?
25
+ service = am["Service"]["name"].downcase
26
+ _metrics = am["Metric"]['fans']
27
+ if _metrics.present? && SERVICES.any? { |s| s == service }
28
+ @metrics[service.to_sym] ||= []
29
+ @metrics[service.to_sym] << { url: am['Profile']['url'], data: self.class.tidy_metrics(_metrics)}
30
+ end
24
31
  end
25
32
  end
26
- @tidied
33
+ @metrics
27
34
  end
28
35
 
29
- def metrics
30
- if @metrics.blank?
31
- @metrics = {}
32
- @metrics[:facebook] = tidy_metrics(all_metrics['facebook']['fans']) if all_metrics['facebook']['fans'].present?
33
- @metrics[:twitter] = tidy_metrics(all_metrics['twitter']['fans']) if all_metrics['twitter']['fans'].present?
34
- @metrics[:youtube] = tidy_metrics(all_metrics['youtube']['fans']) if all_metrics['youtube']['fans'].present?
35
- @metrics[:instagram] = tidy_metrics(all_metrics['instagram']['fans']) if all_metrics['instagram']['fans'].present?
36
+ def artist_profiles
37
+ if @artist_profiles.blank?
38
+ @artist_profiles = []
39
+ response = self.class.get("/profiles/artist/#{@nbs_id}", @options).parsed_response
40
+ response.each do |id, data|
41
+ next unless SERVICES.any? { |s| s == data['name'].downcase }
42
+ @artist_profiles << {nbs_profle_id: id, url: data['url']}
43
+ end
36
44
  end
37
- @metrics
45
+ @artist_profiles
38
46
  end
39
47
 
40
- def self.artist_search(_name)
41
- if @name != _name
42
- @name = _name
43
- result = get("/artists/search.json", { query: {q: "#{_name}"}}).parsed_response
44
- @artist_search = []
45
- result.each do |id, values|
46
- @artist_search << {id: id, name: values["name"], music_brainz_id: values["music_brainz_id"]}
48
+ def self.artist_search(_names)
49
+ artist_search = []
50
+ [_names].flatten.each do |name|
51
+ result = get("/artists/search.json", { query: {q: "#{name}"}})
52
+ if result.code == 200
53
+ result.parsed_response.each do |id, values|
54
+ artist_search << {nbs_id: id, name: values["name"], music_brainz_id: values["music_brainz_id"]}
55
+ end
47
56
  end
48
57
  end
49
- @artist_search
58
+ artist_search.uniq
59
+ end
60
+
61
+ def self.metric(_nbs_profile_id=16209803, _options={}) #run the jewels instagram profile
62
+ options = {query: {start: (Time.now - 3.day).to_i, end: Time.now.to_i, metric: 'fans'}.merge(_options)}
63
+ response = post("/metrics/profile/#{_nbs_profile_id}.json", options)
64
+ if response.code == 200
65
+ tidy_metrics(response.parsed_response['fans'])
66
+ else
67
+ []
68
+ end
50
69
  end
51
70
 
52
71
  def self.setup(_key)
53
72
  base_uri "#{_key}.api3.nextbigsound.com"
54
73
  end
55
74
 
56
- private
57
-
58
- def tidy_metrics(_arry)
75
+ def self.tidy_metrics(_arry)
59
76
  _arry.map do |day, value|
60
77
  {time: Time.at(day.to_i.days.to_i).utc, volume: value}
61
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Georgiou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-06 00:00:00.000000000 Z
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler