Class: Dozenscli::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dozenscli/api.rb

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dozenscli/api.rb', line 7

def initialize
  @base_url = 'http://dozens.jp'
  home = File.expand_path('~')
  conf = IniFile.load("#{home}/.dozenscli.conf")

  dozens_id = conf['profile']['dozens_id']
  api_key   = conf['profile']['api_key']

  if dozens_id.nil? || api_key.nil?
    abort("\x1b[31;01m[ERROR]\x1b[39;49;00m Configuration file is missing some required information")
  end

  @headers = {
    'X-Auth-User'  => dozens_id,
    'X-Auth-Key'   => api_key,
    'Host'         => 'dozens.jp',
    'Content-Type' => 'application/json'
  }

  uri = URI.parse("#{@base_url}/api/authorize.json")
  res = Net::HTTP.start(uri.host, uri.port) do |http|
    http.get(uri.path, @headers)
  end

  @auth_token = JSON.parse(res.body)['auth_token']
end

Instance Method Details

#create_record(params) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/dozenscli/api.rb', line 80

def create_record(params)
  uri = URI.parse("#{@base_url}/api/record/create.json")
  begin
    JSON.parse(params)
  rescue JSON::ParserError => e
    abort("\x1b[31;01m[ERROR]\x1b[39;49;00m #{e.message}")
  else
    post_data(uri, params)
  end
end

#create_zone(zone_name) ⇒ Object



64
65
66
67
68
# File 'lib/dozenscli/api.rb', line 64

def create_zone(zone_name)
  uri = URI.parse("#{@base_url}/api/zone/create.json")
  data = JSON.generate({"name" => zone_name})
  post_data(uri, data)
end

#delete_data(uri) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/dozenscli/api.rb', line 51

def delete_data(uri)
  @headers['X-Auth-Token'] = @auth_token

  http = Net::HTTP.new(uri.host, uri.port)
  res = http.delete(uri.request_uri, @headers)
  JSON.parse res.body
end

#delete_record(record_id) ⇒ Object



91
92
93
94
# File 'lib/dozenscli/api.rb', line 91

def delete_record(record_id)
  uri = URI.parse("#{@base_url}/api/record/delete/#{record_id}.json")
  delete_data(uri)
end

#delete_zone(zone_id) ⇒ Object



70
71
72
73
# File 'lib/dozenscli/api.rb', line 70

def delete_zone(zone_id)
  uri = URI.parse("#{@base_url}/api/zone/delete/#{zone_id}.json")
  delete_data(uri)
end

#get_data(uri) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/dozenscli/api.rb', line 34

def get_data(uri)
  @headers['X-Auth-Token'] = @auth_token

  res = Net::HTTP.start(uri.host, uri.port) do |http|
    http.get(uri.path, @headers)
  end
  JSON.parse res.body
end

#get_record(zone_name) ⇒ Object



75
76
77
78
# File 'lib/dozenscli/api.rb', line 75

def get_record(zone_name)
  uri = URI.parse("#{@base_url}/api/record/#{zone_name}.json")
  get_data(uri)
end

#get_zoneObject



59
60
61
62
# File 'lib/dozenscli/api.rb', line 59

def get_zone
  uri = URI.parse("#{@base_url}/api/zone.json")
  get_data(uri)
end

#post_data(uri, data) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dozenscli/api.rb', line 43

def post_data(uri, data)
  @headers['X-Auth-Token'] = @auth_token

  http = Net::HTTP.new(uri.host, uri.port)
  res = http.post(uri.request_uri, data, @headers)
  JSON.parse res.body
end

#update_record(record_id, params) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/dozenscli/api.rb', line 96

def update_record(record_id, params)
  uri = URI.parse("#{@base_url}/api/record/update/#{record_id}.json")
  begin
    JSON.parse(params)
  rescue JSON::ParserError => e
    abort("\x1b[31;01m[ERROR]\x1b[39;49;00m #{e.message}")
  else
    post_data(uri, params)
  end
end