instagrammer 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf6b1334dcecbe5eaa59cdbe222fbe7d26d194ef7adcb3f38f69e051208da203
4
- data.tar.gz: f795b469c1611e5eb355c9db5848d671cdeeeb4455350909c812ef37f425477c
3
+ metadata.gz: c3a1ab2d4f1152a6bf11cdb8b1b6056feeeb0dad5947d57ca20892b0c70b2756
4
+ data.tar.gz: 9fbcd09a13b62e6b35610a115a3bf156b5811abf85cf2c6651973e848808c3f3
5
5
  SHA512:
6
- metadata.gz: d440948e383e41ebf587abaa96dc89dd5e72e165ff14f12062031f438a1026ea787e945b7511239bbc0d7df5d0850cae117e86bd63ff8aa5f0ed407cf541276f
7
- data.tar.gz: 1c0d923c19ebcbeb8c650ee3f11217b3ff32185c25da94b56c6993ec7ffd52397a798a26880551ac2144cf44a1a5a9768f5c593b91526a3dafea2cd0eece9f3f
6
+ metadata.gz: 43be16412153206a20a053232c664f26607c71beb84b50cd57e8214961f6bfa24ffd426929665311ad967ce1a10f33fcd6983bf09e494957f4d95b264f3763de
7
+ data.tar.gz: b72aec029920c4e24794ce657fd3b1900557bfcde326791b59adac3adb34124738e17d3fafc5d6ec3dcf44b867ddf7e67d0752f07251245f84383a0bd160ace7
data/.travis.yml CHANGED
@@ -25,7 +25,6 @@ before_script:
25
25
 
26
26
  script:
27
27
  - bundle exec rake test
28
- - bundle exec rubocop
29
28
 
30
29
  after_script:
31
30
  - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/CHANGELOG.md CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.2.0] - 2019-07-14
10
+ ### Changed
11
+ - Refactor page status internals
12
+ - Lazy load post attributes
13
+
14
+ ### Fixed
15
+ - Check page status before accessing data (closes [#2](https://github.com/richardvenneman/instagrammer/issues/2))
16
+
9
17
  ## [0.1.3] - 2019-06-20
10
18
  ### Changed
11
19
  - Setup test coverage
data/Guardfile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ignore([%r{^(bin)/*}])
4
4
 
5
- guard :minitest do
5
+ guard :minitest all_on_start: false do
6
6
  watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
7
7
  watch(%r{^test/.+_test\.rb$})
8
8
  watch(%r{^test/test_helper\.rb$}) { "test" }
data/instagrammer.gemspec CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "guard", "~> 2.15"
35
35
  spec.add_development_dependency "guard-minitest", "~> 2.4"
36
36
  spec.add_development_dependency "minitest", "~> 5.0"
37
- spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rake", "~> 12.3"
38
38
  spec.add_development_dependency "rubocop-rails_config", "~> 0.6"
39
39
  spec.add_development_dependency "simplecov", "~> 0.16"
40
40
  end
data/lib/instagrammer.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "instagrammer/version"
4
+ require "instagrammer/utils"
4
5
  require "instagrammer/config/capybara"
5
6
  require "instagrammer/post"
6
7
  require "instagrammer/user"
@@ -11,6 +12,7 @@ module Instagrammer
11
12
  class UserNotFound < StandardError; end
12
13
 
13
14
  class PrivatePost < StandardError; end
15
+ class PostInvalid < StandardError; end
14
16
  class PostNotFound < StandardError; end
15
17
 
16
18
  def self.new(username)
@@ -2,12 +2,13 @@
2
2
 
3
3
  class Instagrammer::Post
4
4
  include Capybara::DSL
5
+ include Instagrammer::Utils
5
6
 
6
7
  attr_reader :shortcode, :image_url, :image_urls
7
8
 
8
9
  def initialize(shortcode)
9
10
  @shortcode = shortcode
10
- visit_page
11
+ @data = nil
11
12
  end
12
13
 
13
14
  def inspect
@@ -17,8 +18,24 @@ class Instagrammer::Post
17
18
  "#<#{self.class.name}:#{object_id} #{attributes.map { |attr| "#{attr}:#{send(attr).inspect}" }.join(", ")}>"
18
19
  end
19
20
 
21
+ def public?
22
+ get_data unless @data
23
+ @status == :public
24
+ end
25
+
26
+ def data
27
+ get_data unless @data
28
+
29
+ case @status
30
+ when :private then raise Instagrammer::PrivatePost.new("Private post: #{@shortcode}")
31
+ when :not_found then raise Instagrammer::PostNotFound.new("Post not found: #{@shortcode}")
32
+ when :invalid then raise Instagrammer::PostInvalid.new("Post invalid: #{@shortcode}")
33
+ else @data
34
+ end
35
+ end
36
+
20
37
  def type
21
- @data["@type"] == "ImageObject" ? :photo : :video
38
+ data["@type"] == "ImageObject" ? :photo : :video
22
39
  end
23
40
 
24
41
  def photo?
@@ -30,43 +47,38 @@ class Instagrammer::Post
30
47
  end
31
48
 
32
49
  def user
33
- Instagrammer::User.new @data["author"]["alternateName"]
50
+ Instagrammer::User.new data["author"]["alternateName"]
34
51
  end
35
52
 
36
53
  def caption
37
- @data["caption"]
54
+ data["caption"]
38
55
  end
39
56
 
40
57
  def upload_date
41
- DateTime.parse @data["uploadDate"]
58
+ DateTime.parse data["uploadDate"]
42
59
  end
43
60
 
44
61
  def comment_count
45
- @data["commentCount"].to_i
62
+ data["commentCount"].to_i
46
63
  end
47
64
 
48
65
  def like_count
49
- @data["interactionStatistic"]["userInteractionCount"].to_i if photo?
66
+ data["interactionStatistic"]["userInteractionCount"].to_i if photo?
50
67
  end
51
68
 
52
69
  def watch_count
53
- @data["interactionStatistic"]["userInteractionCount"].to_i if video?
70
+ data["interactionStatistic"]["userInteractionCount"].to_i if video?
54
71
  end
55
72
 
56
73
  private
57
- def visit_page
74
+ def get_data
58
75
  visit "https://www.instagram.com/p/#{@shortcode}/"
59
- check_status
60
-
61
- @data = JSON.parse(page.first(:json_ld, visible: false).text(:all))
62
- set_image_attributes if photo?
63
- end
76
+ @status = get_page_status
64
77
 
65
- def check_status
66
- if page.has_content?("Private")
67
- raise Instagrammer::PrivatePost.new("Private post: #{@shortcode}")
68
- elsif page.has_content?("Sorry")
69
- raise Instagrammer::PostNotFound.new("Post not found: #{@shortcode}")
78
+ if @status == :public
79
+ node = page.first(:json_ld, visible: false)
80
+ @data = JSON.parse(node.text(:all))
81
+ set_image_attributes if photo?
70
82
  end
71
83
  end
72
84
 
@@ -2,11 +2,13 @@
2
2
 
3
3
  class Instagrammer::User
4
4
  include Capybara::DSL
5
+ include Instagrammer::Utils
5
6
 
6
7
  attr_reader :posts
7
8
 
8
9
  def initialize(username)
9
10
  @username = username.delete_prefix("@")
11
+ @data = nil
10
12
  @posts = []
11
13
  end
12
14
 
@@ -98,7 +100,7 @@ class Instagrammer::User
98
100
  private
99
101
  def get_data
100
102
  visit "https://www.instagram.com/#{@username}/"
101
- @status = get_account_status
103
+ @status = get_page_status
102
104
  @meta = get_metadata unless @status == :not_found
103
105
 
104
106
  if @status == :public
@@ -111,16 +113,4 @@ class Instagrammer::User
111
113
  def get_metadata
112
114
  @meta = page.first(:meta_description, visible: false)["content"].match META_RE
113
115
  end
114
-
115
- def get_account_status
116
- if page.has_content?("Private")
117
- :private
118
- elsif page.has_content?("Sorry")
119
- :not_found
120
- elsif page.find(:json_ld, visible: false)
121
- :public
122
- end
123
- rescue Capybara::ElementNotFound
124
- :invalid
125
- end
126
116
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Instagrammer::Utils
4
+ def get_page_status
5
+ if page.has_content?("Private")
6
+ :private
7
+ elsif page.has_content?("Sorry")
8
+ :not_found
9
+ elsif page.find(:json_ld, visible: false)
10
+ :public
11
+ end
12
+ rescue Capybara::ElementNotFound
13
+ :invalid
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Instagrammer
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: instagrammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Venneman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-20 00:00:00.000000000 Z
11
+ date: 2019-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '10.0'
103
+ version: '12.3'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '10.0'
110
+ version: '12.3'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rubocop-rails_config
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -159,6 +159,7 @@ files:
159
159
  - lib/instagrammer/config/capybara.rb
160
160
  - lib/instagrammer/post.rb
161
161
  - lib/instagrammer/user.rb
162
+ - lib/instagrammer/utils.rb
162
163
  - lib/instagrammer/version.rb
163
164
  homepage: https://github.com/richardvenneman/instagrammer
164
165
  licenses: