Module: Littlstar

Defined in:
lib/littlstar.rb,
lib/littlstar/users.rb,
lib/littlstar/photos.rb,
lib/littlstar/search.rb,
lib/littlstar/videos.rb,
lib/littlstar/version.rb,
lib/littlstar/channels.rb,
lib/littlstar/hashtags.rb,
lib/littlstar/categories.rb

Defined Under Namespace

Classes: Categories, Channels, Hashtags, Photos, Search, Users, Videos

Constant Summary collapse

VERSION =
'0.1.2'
@@apikey =
''
@@api_base =
'https://littlstar.com/api/v1'

Class Method Summary collapse

Class Method Details

.api_baseObject



26
27
28
# File 'lib/littlstar.rb', line 26

def self.api_base()
  @@api_base
end

.api_base=(base) ⇒ Object



30
31
32
# File 'lib/littlstar.rb', line 30

def self.api_base=(base)
  @@api_base = base
end

.api_url(path = '', query = {}) ⇒ Object



34
35
36
# File 'lib/littlstar.rb', line 34

def self.api_url(path='', query={})
  self.api_base + path + self.query_params_from(query)
end

.apikeyObject



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

def self.apikey()
  @@apikey
end

.apikey=(key) ⇒ Object



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

def self.apikey=(key)
  @@apikey = key
end

.query_params_from(hash) ⇒ Object



38
39
40
# File 'lib/littlstar.rb', line 38

def self.query_params_from(hash)
  "?#{URI.encode(hash.map { |k,v| "#{k}=#{v}" }.join('&'))}"
end

.request(method, path, query = {}) ⇒ Object

The request() method is the heart and soul of the library. It is responsible for taking the connection details from each resource class and creating the Net::HTTP object that will ultimately send the appropriate request and return the response from the Littstar API server.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/littlstar.rb', line 46

def self.request(method, path, query={})

  # create connection uri
  uri  = URI.parse(self.api_url(path, query))

  # create request object
  req = Net::HTTP::Get.new(uri.to_s)
  req['User-Agent']   = "ruby-sdk/#{Littlstar::VERSION}",
  req['Content-Type'] = 'application/json',
  req['X-Apikey']     = self.apikey

  # send request and build response
  Net::HTTP.start(uri.host, uri.port, use_ssl: true)  do |http|
    res = http.request(req)
    JSON.parse(res.body, symbolize_names: true)
  end

end