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



24
25
26
# File 'lib/IgApi/http.rb', line 24

def self.__obj
  @@obj
end

.__obj=(value) ⇒ Object



20
21
22
# File 'lib/IgApi/http.rb', line 20

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

.compute_hash(data) ⇒ Object



16
17
18
# File 'lib/IgApi/http.rb', line 16

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

.create_md5(data) ⇒ Object



42
43
44
# File 'lib/IgApi/http.rb', line 42

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

.generate_device_idObject



46
47
48
49
# File 'lib/IgApi/http.rb', line 46

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

.generate_rank_token(pk) ⇒ Object



99
100
101
# File 'lib/IgApi/http.rb', line 99

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

.generate_signature(data) ⇒ Object



51
52
53
54
# File 'lib/IgApi/http.rb', line 51

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

.generate_uuidObject



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

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



28
29
30
31
32
# File 'lib/IgApi/http.rb', line 28

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

Instance Method Details

#execObject



66
67
68
# File 'lib/IgApi/http.rb', line 66

def exec
  http @data
end

#get(url) ⇒ Object



70
71
72
73
# File 'lib/IgApi/http.rb', line 70

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

#http(args) ⇒ Object



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

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



56
57
58
59
# File 'lib/IgApi/http.rb', line 56

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

#with(data) ⇒ Object



61
62
63
64
# File 'lib/IgApi/http.rb', line 61

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