Class: IgApi::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/IgApi/http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__objObject



22
23
24
# File 'lib/IgApi/http.rb', line 22

def self.__obj
  @@obj
end

.__obj=(value) ⇒ Object



18
19
20
# File 'lib/IgApi/http.rb', line 18

def self.__obj=value
  @@obj = value
end

.compute_hash(data) ⇒ Object



14
15
16
# File 'lib/IgApi/http.rb', line 14

def self.compute_hash(data)
  OpenSSL::HMAC.hexdigest OpenSSL::Digest.new('sha256'), Constants::PRIVATE_KEY[:SIG_KEY], data
end

.create_md5(data) ⇒ Object



40
41
42
# File 'lib/IgApi/http.rb', line 40

def self.create_md5(data)
  Digest::MD5.hexdigest(data).to_s
end

.generate_device_idObject



44
45
46
47
# File 'lib/IgApi/http.rb', line 44

def self.generate_device_id
  timestamp = Time.now.to_i.to_s
  'android-' + create_md5(timestamp)[0..16]
end

.generate_rank_token(pk) ⇒ Object



97
98
99
# File 'lib/IgApi/http.rb', line 97

def self.generate_rank_token(pk)
  format('%s_%s', pk, IgApi::Http.generate_uuid)
end

.generate_signature(data) ⇒ Object



49
50
51
52
# File 'lib/IgApi/http.rb', line 49

def self.generate_signature(data)
  data = data.to_json
  compute_hash(data) + '.' + data
end

.generate_uuidObject



32
33
34
35
36
37
38
# File 'lib/IgApi/http.rb', line 32

def self.generate_uuid
  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.gsub(/[xy]/) do |c|
    r = (Random.rand * 16).round | 0
    v = c == 'x' ? r : (r & 0x3 | 0x8)
    c.gsub(c, v.to_s(16))
  end.downcase
end

.singletonObject



26
27
28
29
30
# File 'lib/IgApi/http.rb', line 26

def self.singleton
  @@obj = Http.new unless defined? @@obj
  
  @@obj
end

Instance Method Details

#execObject



64
65
66
# File 'lib/IgApi/http.rb', line 64

def exec
  http @data
end

#get(url) ⇒ Object



68
69
70
71
# File 'lib/IgApi/http.rb', line 68

def get(url)
  @data = {method: 'GET', url: url}
  self
end

#http(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/IgApi/http.rb', line 73

def http(args)
  args[:url] = URI.parse(args[:url])
  http = Net::HTTP.new(args[:url].host, args[:url].port,
                       ENV['INSTAGRAM_PROXY_HOST'], ENV['INSTAGRAM_PROXY_PORT'])
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = nil
  if args[:method] == 'POST'
    request = Net::HTTP::Post.new(args[:url].path)
  elsif args[:method] == 'GET'
    request = Net::HTTP::Get.new(args[:url].path + (!args[:url].nil? ? '?' + args[:url].query : ''))
  end

  request.initialize_http_header('User-Agent': args[:ua],
                                 Accept: IgApi::Constants::HEADER[:accept],
                                 'Accept-Encoding': IgApi::Constants::HEADER[:encoding],
                                 'Accept-Language': args.dig(:user)&.language,
                                 'X-IG-Capabilities': IgApi::Constants::HEADER[:capabilities],
                                 'X-IG-Connection-Type': IgApi::Constants::HEADER[:type],
                                 Cookie: args[:session])
  request.body = args.key?(:body) ? args[:body] : nil
  http.request(request)
end

#post(url, body = nil) ⇒ Object



54
55
56
57
# File 'lib/IgApi/http.rb', line 54

def post(url, body = nil)
  @data = { method: 'POST', url: url, body: body }
  self
end

#with(data) ⇒ Object



59
60
61
62
# File 'lib/IgApi/http.rb', line 59

def with(data)
  data.each { |k, v| @data[k] = v }
  self
end